0

我正在尝试使用 wicket + hibernate + spring 编写 Web 应用程序。和所有人一样,我想使用 @Autowired 注释。首先,这是我的 applicationContext.xml:

<beans default-autowire="autodetect"
       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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">


<context:annotation-config/>
<bean id="placeholderConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="false"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreResourceNotFound" value="false"/>
    <property name="locations">
        <list>
            <value>classpath*:/application.properties</value>
        </list>
    </property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
//connection...
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<!-- setup transaction manager  -->
<bean id="txManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>
<!-- hibernate session factory -->
<bean id="sessionFactory"       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
 //some hibernate stuff
</bean>
//wicket app bean     
<bean id="wicketApplication" class="com.team.wicket.ShopApp" scope="singleton"/>

//DAO beans
<bean id="userDAO" class="com.team.wicket.daoImpl.UserDAOImpl" scope="singleton"/>
<bean id="contactDAO" class="com.team.wicket.daoImpl.ContactDAOImpl" scope="singleton"/>

这是web.xml:

    <display-name>wicket-spring-hibernate</display-name>
    <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>opensessioninview</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>

    <filter>
        <filter-name>wicket-spring-hibernate</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>applicationFactoryClassName</param-name>
            <param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
        </init-param>
        <init-param>
            <param-name>applicationClassName</param-name>
            <param-value>com.mycompany.WicketApplication</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>opensessioninview</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>wicket-spring-hibernate</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

实际上,我只需要将 @Autowired 与 userDAO 和 contactDAO bean 一起使用。UserDAOImpl 和 ContactDAOImpl 的代码以@Service 注解开头:

@Service
public class ContactDAOImpl implements ContactDAO {
....
    public ContactDAOImpl() {
        System.out.println("contactDAO created");
    }

@Service
public class UserDAOImpl implements UserDAO {

 ...

    public UserDAOImpl() {
        System.out.println("UserDAO created");
    }

如您所见,我已将 System.out 添加到类的默认构造函数中,因为我想确保 spring 创建 bean。每次我启动应用程序时它都会创建 bean,所以问题一定出在自动装配上。最后是自动装配失败的类:

public class UserSession extends AuthenticatedWebSession {
    @Autowired
    @Qualifier("userDAO")
    private UserDAO userDAO;
    private User user;

    public UserSession(Request request) {
        super(request);
    }

    @Override
    public boolean authenticate(String username, String password) {
        User user = userDAO.getUser(username, password);
        if (user != null) {
            setUser(user);
            return true;
        }
        return false;
    }

    @Override
    public Roles getRoles() {
        Roles roles = new Roles();
        if (isSignedIn()) {
            roles.add(Roles.USER);
        }
        return roles;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

当 authenticate() 尝试调用 getUser(...) 时,它会给出 NullPointerException。通过调试,我知道 UserDAO 没有初始化(它只是空),我也知道 Spring 创建了 UserDAO 和 ContactDAO bean。那么我做错了什么?谢谢。

4

2 回答 2

1

尝试上下文:组件扫描 base-package="xyz"

确保添加 xmlns:context="http://www.springframework.org/schema/context"

于 2013-08-19T14:01:01.170 回答
0

Spring 只能将依赖项注入到它自己的 bean 中。UserSession 不在 context.xml 中,这意味着它不是 Spring bean,不能由 Spring 自动装配

于 2013-08-19T14:06:31.957 回答