Galera, boa noite. Estou com um problema na integração do Spring + Struts 2 e não sei o que pode estar havendo. O problema é que o Spring não está injetando os DAOs e cai em NullPointerException. Estou usando o @Resource, mas acho que está faltando alguma configuração em algum lugar… rs rs rs Alguém pode me dar uma luz?
E o problema não é DAO, porque já fiz vários tests e tudo funciona.
@Service("userAccountDAO")
public class UserAccountDAOImpl
extends HibernateDAOServiceImpl<UserAccount, Integer>
implements UserAccountDAO {
@Override
public UserAccount findByName(String userName) {
Criteria criteria = getSession().createCriteria(UserAccount.class);
criteria.add(Restrictions.like("name", userName));
return (UserAccount) criteria.uniqueResult();
}
}
Minha action está assim:
public class LoginAction extends ActionSupport implements SessionAware,
ServletRequestAware, ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
private Map<String, Object> session;
private String userName;
private String userPassword;
@Resource
private UserAccountDAO userAccountDAO;
public LoginAction() {}
public String loginValidate() {
UserAccount userAccount = new UserAccount();
// Quando chamo o DAO na linha abaixo,
// ocorre NullPointerException
List<UserAccount> list = userAccountDAO.findAll();
// Assim também da erro
// UserAccount userAccount = userAccountDAO.findById(new Integer(1));
if (userAccount != null) {
session.put("userAccount", userAccount);
return SUCCESS;
}
return ERROR;
}
// Getters and setters suprimidos
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Autenticacao / Login -->
<filter>
<filter-name>login</filter-name>
<filter-class>com.eliasjunior.tkt.webapp.login.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>/pages/*</url-pattern>
</filter-mapping>
</web-app>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.eliasjunior.tkt.*" />
<!-- Postgres -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/tkt" />
<property name="username" value="postgres" />
<property name="password" value="123456" />
</bean>
<bean id="hibernateProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<!-- validate | update | create | create-drop -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.c3p0.minPoolSize">5</prop>
<prop key="hibernate.c3p0.maxPoolSize">20</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.max_statement">50</prop>
</props>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="hibernateProperties" ref="hibernateProperties" />
<property name="annotatedClasses">
<list>
<!-- com.eliasjunior.tkt.model - Basic Model Component -->
<value>com.eliasjunior.tkt.model.Client</value>
<value>com.eliasjunior.tkt.model.Project</value>
<value>com.eliasjunior.tkt.model.ProjectModel</value>
<value>com.eliasjunior.tkt.model.ProjectType</value>
<value>com.eliasjunior.tkt.model.Task</value>
<value>com.eliasjunior.tkt.model.TaskType</value>
<value>com.eliasjunior.tkt.model.UserAccount</value>
<!-- com.eliasjunior.tkt.webapp.login - WebApp Component -->
<value>com.eliasjunior.tkt.webapp.login.LoginAction</value>
<value>com.eliasjunior.tkt.webapp.login.LoginFilter</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>