我在正確設置Spring環境時遇到了一些麻煩。在我applicationContext.xml
我:Spring 3將@Required字段注入爲NULL
...
<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
。
謝謝!
UPDATE
我還要提到我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;
ViewContactsServiceImpl類及其接口的包是什麼? –
@ danny.lesnik - 查看我的更新2. –
對不起,我忘了提及ContactDaoImpl及其界面包呢? –