Spring 3将@Required字段注入为NULL

时间:2011-10-06 06:53:36

标签: java spring

我在正确设置Spring环境时遇到了一些麻烦。在我的applicationContext.xml我有:

...
<context:annotation-config />
<context:component-scan base-package="com.company.server" />

<import resource="databaseConfig.xml" />

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:config.properties</value>
        </list>
    </property>
</bean>

和我的databaseConfig

<tx:annotation-driven />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass">
        <value>${jdbc.driver.className}</value>
    </property>
    <property name="jdbcUrl">
        <value>${jdbc.url}</value>
    </property>
    <property name="user">
        <value>${jdbc.username}</value>
    </property>
    <property name="password">
        <value>${jdbc.password}</value>
    </property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="packagesToScan" value="org.adit.spring.hibernate.entity" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

现在我的问题开始了。如果你看下面的课程:

@Service
public class ViewContactsServiceImpl extends RemoteServiceServlet implements ViewContactsService {

    private ContactDao contactDao;

    @Autowired
    public void setContactDao(ContactDao contactDao) {
        this.contactDao = contactDao;
    }

    @Override
    public ArrayList<Contact> getAllContacts() {
        return contactDao.getAllContacts();
    }
}

在应用程序启动期间一切正常。 Spring并没有抱怨它无法创建bean或者它无法注入属性。但是,每当我尝试访问contactDao字段时,都是null

谢谢!


更新

我还应该提到我的ContactDaoImpl.java被定义为:

@Repository("contactDao")
@Transactional
public class ContactDaoImpl implements ContactDao { ... }

更新2

NB。这是一个GWT应用程序。

ViewContactServiceImpl.java

package com.company.server.service.viewcontacts;

ViewContactsService.java

package com.company.client.viewcontacts;

ContactDaoImpl.java

package com.company.server.contact;

ContactDao.java

package com.company.server.contact;

1 个答案:

答案 0 :(得分:3)

我认为ViewContactsServiceImpl正在由GWT实例化(基于RemoteServiceServlet进行猜测) - 因此它不是Spring托管bean。

您需要通过覆盖和实现init方法手动调用自动连线。类似于下面显示的代码(来自此article)。正如该文章中所解释的那样,创建一个所有GWTService都可以扩展的AbstractRemoteServlet。

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    WebApplicationContext ctx = WebApplicationContextUtils
            .getRequiredWebApplicationContext(config.getServletContext());
    AutowireCapableBeanFactory beanFactory = ctx
            .getAutowireCapableBeanFactory();
    beanFactory.autowireBean(this);
}

查看GWT-SL库,了解将spring托管bean作为GWT远程服务公开的另一种方法。