java - 可配置的 Autowiring NullPointerException

标签 java

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

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

谢谢。

最佳答案

如果我没记错 Spring 注释,@Qualifier 不会出现在您想要识别的 bean 上。相反,您应该像 @Repository("genericDaoImpl") 一样对其进行注释。

关于java - 可配置的 Autowiring NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14249870/

相关文章:

java - 局部变量的定义会降低应用程序的性能吗?

协助创建 XSD 的 Java 库?

java - 是否有更干净的方法使用 RxJava 来适应标准观察者/监听器服务?

java - SWT:表大小调整问题

java - 如果 Java 没有预处理器 "import"是什么

java - hashmap 键的比较,为什么要同时比较键的哈希码和键的值

java - 重命名文件夹中的所有文件(交换由 '_' 分隔的单词/数字)

java - 一种方法的多个锁 - Java

java - Android 套接字连接中出现错误 'null'

java - Camel ProducerTemplate 发送到 JMS 消息属性