有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java@Autowired未设置字段>NullPointerException

我想集成Struts 2、Hibernate和Spring。我用Hibernate、Spring、Spring MVC做了一个上一个示例项目。而且效果很好。现在我想用Struts 2取代Spring MVC。我有以下问题。我有一个带有字段@Autowired private UserService userService的LoginAction类。UserService类包含一个字段UserDAO字段,该字段也被注释为Autowired。UserDAO包含也带注释的SessionFactory。我相信struts confiration是对的。和applicationContext。xml也是正确的。因为当我在这个文件中做一些更改时,我得到了一个例外。Tomcat启动时没有错误,并显示登录页面。但当我试图调用UserService类的方法时,我得到了一个NPE,因为这个对象为null。为什么Spring没有设置这个对象?我的代码: 应用程序上下文。xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<tx:annotation-driven transaction-manager="transactionManager" />

<context:annotation-config />

<context:component-scan base-package="com"/>

<bean id="userDAO" class="com.example.dao.impl.hibernate.HibernateUserDAO" />
<bean id="userService" class="com.example.service.impl.UserServiceImpl" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:~/database" />
    <property name="username" value="user" />
    <property name="password" value="user" />
</bean>
<bean name="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.example.entity.User</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.H2Dialect
            hibernate.show_sql=true
        </value>
    </property>
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

网络。xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>/WEB-INF/pages/login.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
        /WEB-INF/applicationContext.xml
      </param-value>
</context-param> 
</web-app>

登录类:

@Controller
public class LoginAction extends ActionSupport {
private String login;
private String password;
@Autowired
private UserService userService;

@Override
public String execute() throws Exception {
    User user = null;
    try {
        user = userService.findByLogin(login);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (user != null) {
        return user.getPassword().equals(password) ? SUCCESS : ERROR;
    }
    return ERROR;
}
// getters and setters
}

UserService实现类:

@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO;

@Override
@Transactional
public User findByLogin(String login) throws SQLException {
    return userDAO.findByLogin(login);
} }

和UserDAO impl类:

@Repository
public class HibernateUserDAO implements UserDAO {
@Autowired
public SessionFactory sessionFactory;
@Override
public User findByLogin(String login) throws SQLException {
    return sessionFactory.getCurrentSession().createCriteria(User.class)
                .add(Restrictions.eq("login", login)).list().get(0);
} }

Struts confiration很好,因为它反应正确。为什么春天没有开荒?有什么想法吗

<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
    <action name="login" class="com.example.web.action.LoginAction">
        <result name="success">/WEB-INF/pages/cabinet.jsp</result>
        <result name="error">/WEB-INF/pages/login.jsp</result>
        <result name="input">/WEB-INF/pages/login.jsp</result>
    </action>
</package>


共 (0) 个答案