0

我有一个用 @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>

请让我知道这个配置有什么问题。

谢谢。

4

1 回答 1

1

如果我正确地记住了我的 Spring 注释,@Qualifier则不会继续使用您要识别的 bean。相反,您应该将其注释为@Repository("genericDaoImpl").

于 2013-01-10T02:35:02.027 回答