Olás!
Bom estou com uma dificuldade ao utilizar o spring para fazer dependency injection em uma aplicação web JSF.
meu applicationContext.xml:
<bean id="xiFluxosDao" class="br.com.ogaiht.nxmi.dao.JpaXiFluxosDao">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="xiFluxosMgr" class="br.com.ogaiht.nxmi.business.XiFluxosMgrImpl">
<property name="xiFluxosDao" ref="xiFluxosDao"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="false"/>
<property name="databasePlatform" value="oracle.toplink.essentials.platform.database.MySQL4Platform"/>
</bean>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/x"/>
<property name="username" value="user"/>
<property name="password" value="senha"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
Desta maneira o código abaixo, de teste funciona corretamente, quando busco o applicationContext utilizando o método getConfigLocations():
public class XiFluxosMgrImplTest extends AbstractJpaTests {
XiFluxosMgr xiFluxosMgr;
public void setXiFluxosMgr(XiFluxosMgr xiFluxosMgr) {
this.xiFluxosMgr = xiFluxosMgr;
}
@Override
protected String[] getConfigLocations() {
return new String[] {"classpath:web/WEB-INF/applicationContext.xml"};
}
@Override
protected void onSetUpInTransaction() throws Exception {
}
public void testFindByCodIdentFluxoExists() {
XiFluxos fluxo = xiFluxosMgr.searchFluxoByPk("F01");
assertNotNull(fluxo);
}
...
Porém a dependency injection nao funciona quando rodo na web, o managed bean abaixo, deveria ter a propriedade xiFluxosMgr já com os templates jpa … mas esta vindo null.
public class CadastroControl {
private XiFluxosMgr xiFluxosMgr;
public void CadastroControl(){
}
public void setXiFluxosMgr(XiFluxosMgr xiFluxosMgr) {
this.xiFluxosMgr = xiFluxosMgr;
}
public String searchFluxoByCodIdentFluxo(){
try{
XiFluxos fluxo = xiFluxosMgr.searchFluxoByPk("F01");
...
...
Finalmente segue meu web.xml:
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
<init-param>
<param-name>maxFileSize</param-name>
<param-value>20m</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/welcomeJSF.jsp</welcome-file>
</welcome-file-list>
</web-app>
Enfim, este é meu problema, ja tentei de varias maneiras arrrumar mas não consegui. Nem sei como pesquisar isso no google.
Se alguem ja sabe o motivo do problema gostaria de saber.
Fico no aguardo,
Att. Thiago