2013-01-10 46 views
0

我有一個GenericServlet類,用@Configurable註解,它有一個叫做dao的字段,它在春天被自動裝入,但它在使用它時不會自動裝入,而不會拋出nullpointerexception。我試圖使用@Qualifier強制執行Spring DI,但它仍然返回null;可配置的自動佈線空指針異常

@Configurable 
public class GenericServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    @Autowired 
    @Qualifier("genericDaoImpl") 
    private GenericDAO dao; 
} 

@Repository 
@Qualifier("genericDaoImpl") 
@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT, rollbackFor={Exception.class, SQLException.class, DataAccessException.class}, timeout=9999) 
public class GenericDAOImpl implements GenericDAO { 

    @Autowired 
    @Qualifier("jdbcTemplate") 
    private JdbcTemplate jdbcDao; 
} 

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 

    <!-- Enable Spring Application Context --> 
    <context:spring-configured /> 

    <!-- Enable @Required @Autowired @PreDestroy @PostConstruct @Resource --> 
    <context:annotation-config /> 

    <!-- Scan class file in class path for annotated component -> @Component, @Repository, @Service, and @Controller --> 
    <context:component-scan base-package="com.breeze.bis.core.service.jdbcTemplate" /> 

    <!-- Enable load time weaving for @Configurable --> 
    <context:load-time-weaver aspectj-weaving="autodetect" /> 

    <!-- Alternate method to enable @Autowired --> 
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
     <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/vbossdb" /> 
     <property name="driverClass" value="com.mysql.jdbc.Driver" /> 
     <property name="user" value="root" /> 
     <property name="password" value="root" /> 
    </bean> 

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
     <constructor-arg type="javax.sql.DataSource" ref="dataSource"></constructor-arg> 
    </bean> 

    <bean id="genericDaoImpl" class="com.breeze.bis.core.service.jdbcTemplate.GenericDAOImpl"> 
     <property name="jdbcDao" ref="jdbcTemplate"></property> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource"></property> 
    </bean> 

    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
     <property name="configLocation" value="WEB-INF/ehcache.xml"></property> 
     <property name="shared" value="true"></property> 
    </bean> 

</beans> 

請讓我知道這個配置有什麼問題。

謝謝。

+0

任何人都請幫忙。謝謝。 – peterwkc

+0

任何人都請幫忙。謝謝。 – peterwkc

回答

1

如果我正確記住我的Spring註釋,則@Qualifier不會在您要標識的bean上繼續。相反,你應該註釋它,如@Repository("genericDaoImpl")

+0

我試過了,但它仍然有相同的錯誤。任何診斷方法,如日誌記錄或手動實例化?沒有堆棧跟蹤或日誌 – peterwkc

+0

我使用GenericServlet中的setter更改了Autowired,並且setter甚至沒有被調用。 – peterwkc

+0

任何人都請幫忙。謝謝。 – peterwkc