自动装配在Spring中不起作用

时间:2013-08-19 13:53:22

标签: java spring dependency-injection javabeans autowired

我正在尝试使用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 annotation:

开头
@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添加到类的默认construstors中,因为我想确保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。从debuging我知道UserDAO没有初始化(它只是null)我也知道Spring创建了UserDAO和ContactDAO bean。那么我做错了什么?感谢。

2 个答案:

答案 0 :(得分:1)

尝试 context:component-scan base-package =“x.y.z”

确保添加 的xmlns:上下文= “http://www.springframework.org/schema/context”

答案 1 :(得分:0)

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