开发者

Spring 3 injecting @Required field as NULL

开发者 https://www.devze.com 2023-04-10 20:47 出处:网络
I am having some trouble with setting up my Spring environment properly. In my applicationContext.xml I have:

I am having some trouble with setting up my Spring environment properly. In my applicationContext.xml I have:

...
<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>

and in my databaseConfig:

<tx:annotation-driven />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass">
        <value>${jdbc.driver.className}</value>
    &开发者_如何转开发lt;/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>

Now my problem starts. If you look at the following class:

@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();
    }
}

During application startup everything is fine. Spring does not complain that it cannot create the bean or that it cannot inject the property. However, whenever I try to access the contactDao field, it is null.

Thanks!


UPDATE

I should also mention my ContactDaoImpl.java is defined as:

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

UPDATE 2

NB. This is a GWT application.

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;


I thins ViewContactsServiceImpl is being instantiated by GWT (guessing based on RemoteServiceServlet) - so it is not spring managed bean.

You need to invoked auto-wire manually by overriding and implementing the init method. Similar to the code shown below (from this article). As explained in that article create an AbstractRemoteServlet that all your GWTService can extend.

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

Look at GWT-SL library for another approach to exposing spring managed beans as GWT remote services.

0

精彩评论

暂无评论...
验证码 换一张
取 消