可配置的自动装配NullPointerException

时间:2013-01-10 02:22:13

标签: java

我有一个使用@Configurable注释的GenericServlet类,它有一个名为dao的字段,它由spring自动装配,但它没有正确自动装配,而是在使用它时抛出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>

请告诉我这个配置有什么问题。

感谢。

1 个答案:

答案 0 :(得分:1)

如果我正确地记住了我的Spring注释,@Qualifier不会继续你要识别的bean。相反,您应该像@Repository("genericDaoImpl")一样注释它。