gpt4 book ai didi

java - 可配置的 Autowiring NullPointerException

转载 作者:行者123 更新时间:2023-12-01 14:56:50 29 4
gpt4 key购买 nike

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com