简体   繁体   English

可配置的自动连线NullPointerException

[英]Configurable Autowired NullPointerException

I have a GenericServlet class annotated with @Configurable which has field called dao which autowired by spring but it does not get autowired properly rather than throw nullpointerexception when used it. 我有一个用@Configurable注释的GenericServlet类,该类具有一个称为dao的字段,该字段由spring自动连接,但是它不能正确地自动连接,而不是在使用时抛出nullpointerexception。 I tried to enforce the Spring DI using @Qualifier but it still return null; 我试图使用@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>

Please let me know what wrong with this configuration. 请让我知道此配置有什么问题。

Thanks. 谢谢。

If I remember my Spring annotations correctly, @Qualifier doesn't go on the bean you want to identify. 如果我正确记得我的Spring注释,则@Qualifier不会出现在您要标识的bean上。 Rather, you should annotate it like @Repository("genericDaoImpl") . 相反,您应该像@Repository("genericDaoImpl")那样对其进行注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM