| Author |
Message |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 04/01/2006 14:50:28
|
RubemAzenha
Joined: 30/06/2005 23:12:02
Messages: 472
Location: São Paulo, SP
Offline
|
Olá pessoal...
Sobrou um tempinho aqui e como tava estudando um pouco de Spring hoje resolvi fazer uma integração do Mentawai com o Spring...
Foi um filtro.
SpringFilter.java
Code:
package org.mentawai.filter;
import java.util.Iterator;
import java.util.List;
import org.mentawai.core.Filter;
import org.mentawai.core.Input;
import org.mentawai.core.InvocationChain;
import org.springframework.beans.factory.BeanFactory;
/**
*
* A integration of <a href="http://www.springframework.org">Spring Framework</a>
* with Mentawai Web Framework. <br>
* This filter injects in the Input of the filtered action the beans which the
* name is defined in a given List and instanciated by a
* org.springframework.beans.factory.BeanFactory.
*
* @author Rubem Azenha (rubem.azenha@gmail.com)
*/
public class SpringFilter implements Filter {
/**
* The org.springframework.beans.factory.BeanFactory used to instanciate the
* beans.
*/
private BeanFactory beanFactory;
/**
* A List with the name of the beans that have to be instanciated
*/
private List beans;
public SpringFilter(BeanFactory beanFactory, List beans) {
this.beanFactory = beanFactory;
this.beans = beans;
}
/**
* Use the beanFactory attribute to instanciate all the beans named in the
* List beans and injects the beans into the action's Input.
*/
public String filter(InvocationChain chain) throws Exception {
Input input = chain.getAction().getInput();
for (Iterator iterator = beans.iterator(); iterator.hasNext();) {
String beanName = (String) iterator.next();
Object bean = beanFactory.getBean(beanName);
input.setValue(beanName, bean);
}
return chain.invoke();
}
/**
* Does nothing by default.
*/
public void destroy() {
}
}
public class ApplicationManager extends org.mentawai.core.ApplicationManager {
//...
private BeanFactory beanFactory = null;
//...
public void init() {
//...
beanFactory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
}
public void loadActions() {
ActionConfig ac = new ActionConfig(MinhaAction.class);
//...
List beanNames = new LinkedList();
beanNames.add("nomeDoBeanASerInstanciado");
ac.addFilter(new SpringFilter(beanFactory, beanNames ));
}
}
O que vocês acham?
|
Mentawai Developer |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 05/01/2006 01:33:56
|
joao.vitor
Joined: 05/01/2006 00:38:54
Messages: 2
Location: Belo Horizonte, MG
Offline
|
Oi Rubem, tudo bem?
Gostei da idéia da integração do Spring com o MentaWay.
Acho que seria mais simples utilizar o método getBeanDefinitionNames() presente na interface ListableBeanFactory e colocar todos os beans declarados no contexto do spring no input do mentaway.
Coisa assim (comentei o codigo):
Code:
package org.mentawai.filter;
import java.util.Iterator;
import java.util.List;
import org.mentawai.core.Filter;
import org.mentawai.core.Input;
import org.mentawai.core.InvocationChain;
import org.springframework.beans.factory.ListableBeanFactory;
/**
*
* A integration of <a href="http://www.springframework.org">Spring Framework</a>
* with Mentawai Web Framework. <br>
* This filter injects in the Input of the filtered action the beans which the
* name is defined in a given List and instanciated by a
* org.springframework.beans.factory.ListableBeanFactory.
*
* @author Rubem Azenha (rubem.azenha@gmail.com)
*/
public class SpringFilter implements Filter {
/**
* The org.springframework.beans.factory.ListableBeanFactory used to instanciate the beans.
*/
private ListableBeanFactory listableBeanFactory;
/**
* A List with the name of the beans that have to be instanciated
*/
private List beans;
/**
* @param listableBeanFactory BeanFactory witch will have all registered beans
* passed as input to the action input
*/
public SpringFilter(ListableBeanFactory listableBeanFactory) {
this(listableBeanFactory, null);
}
/**
* @param listableBeanFactory BeanFactory witch will have all beans
* passed as input to the action input
* @param beans list of string (shouldnt it be generified??) that
* contains strings with of spring controlled beans.
* With null <b>all</b> spring beans registered will
* be passed as input to the action input
*/
public SpringFilter(ListableBeanFactory listableBeanFactory, List beans) {
this.listableBeanFactory = listableBeanFactory;
this.beans = beans;
}
/**
* Use the ListableBeanFactory attribute to instanciate all the beans named in the
* List beans and injects the beans into the action's Input.
*/
public String filter(InvocationChain chain) throws Exception {
Input input = chain.getAction().getInput();
if(ListableBeanFactory!=null) {
if(beans!null) {
//the developer want to pass what bean he will need
for (Iterator iterator = beans.iterator(); iterator.hasNext();) {
String beanName = (String) iterator.next();
Object bean = listableBeanFactory.getBean(beanName);
input.setValue(beanName, bean);
}
} else {
//the developer doesn't want to pass what bean he will need
//we will put all beans definitions in the input
String[] beanArray = listableBeanFactory.getBeanDefinitionNames();
int beanArraySize = beanArray.size();
for(int i = 0;i<beanArraySize;i++){
String beanName = beanArray[i];
Object bean = listableBeanFactory.getBean(beanName);
input.setValue(beanName, bean);
}
}
}
return chain.invoke();
}
/**
* Does nothing by default.
*/
public void destroy() {
}
}
A integração a partir do ApplicationManager ficaria mais simples:
Code:
public class ApplicationManager extends org.mentawai.core.ApplicationManager {
//...
private ListableBeanFactory listableBeanFactory = null;
//...
public void init() {
//...
listableBeanFactory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
}
public void loadActions() {
ActionConfig ac = new ActionConfig(MinhaAction.class);
ac.addFilter(new SpringFilter(listableBeanFactory));
}
}
Isso não é muito recomendável em uma aplicação web:
Code:
listableBeanFactory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
O melhor seria obter um ApplicationContext ou um WebApplicationContext ao invés de um BeanFactory.
Para obter esse ApplicationContext deve-se seguir esses passos.
|
Any fool can write code that a computer can understand.
Good programmers write code that humans can understand. (Fowler) |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 05/01/2006 10:04:45
|
joao.vitor
Joined: 05/01/2006 00:38:54
Messages: 2
Location: Belo Horizonte, MG
Offline
|
Olhando o post do saoj aqui.
Vi que dá para pegar o ServletContext a partir do AplicationContext do mentawai realmente ficaria melhor essa integração.
O springfilter pode ficar igual.
A classe que herdaria do ApplicationManager deve implementar o método init com o context o outro está deprecated.
E obter o ApplicationContext do Spring a partir do ServletContext.
Code:
public class ApplicationManager extends org.mentawai.core.ApplicationManager {
//...
private org.springframework.web.context.WebApplicationContext wac = null;
//...
public void init(org.mentawai.core.Context application) {
ServletContext sc = null;
if (application instanceof ApplicationContext) {
ApplicationContext ac = (ApplicationContext) application;
sc = ac.getServletContext();
}
wac = org.springframework.web.context.WebApplicationContextUtils.getWebApplicationContext(sc);
}
public void loadActions() {
ActionConfig ac = new ActionConfig(MinhaAction.class);
ac.addFilter(new SpringFilter(wac));
}
}
Assim fica melhor ainda.
|
Any fool can write code that a computer can understand.
Good programmers write code that humans can understand. (Fowler) |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 05/01/2006 13:03:16
|
RubemAzenha
Joined: 30/06/2005 23:12:02
Messages: 472
Location: São Paulo, SP
Offline
|
Mas aí cada vez que a action fosse executada ele ia colocar todos os beans declarados na Input e com certeza você não vai precisar de todos os beans. Vai gerar um baita de um overhead.
|
Mentawai Developer |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 05/01/2006 13:36:59
|
fabio.patricio
Joined: 05/01/2006 13:36:09
Messages: 1
Offline
|
Acabei respondendo la no GUJ mesmo.
http://www.guj.com.br/posts/list/15/37480.java
]['s
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 01/05/2006 09:19:44
|
Marcos Silva Pereira
Joined: 10/08/2005 10:22:46
Messages: 16
Offline
|
http://www.javafree.org/dependencias/mentawai/menta-spring.zip
Eu fiz isso antes da semana santa, mas esqueci de postar e lembrei por causa de um email do Sergio na lista do RSJug. Nada demais, apenas um filter para fazer autowiring - via Spring - das dependencias de uma action.
Para ver como funciona criei tanto um test case quanto uma especie de hello word. O download ficou maior porque coloquei todas os jars juntos, em um projeto do Eclipse, mas tambem criei um build file para o ant caso alguem não use Eclipse.
valeuz...
|
Marcos Silva Pereira
Comunidade Blastêmica |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 20/05/2009 15:30:34
|
sergio_java
Joined: 16/02/2009 11:37:33
Messages: 7
Offline
|
Alguem aqui ja implementou alguma coisa como um CRUD simples? Eu tava tentando fazer um crud , estou tentando usar um service injetado atraves do spring. Mas na hora de salvar da erro de null pointer. Alguem tem algum exemplo ja pronto, de integração spring+menta+hibernate? Se não ... alguem me da uma luz ae de como fazer rsrsrs valew!!!!
|
|
|
 |
|
|