我想将 dao 注入 servlet,但它返回为 null。
应用程序上下文.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 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" />
<!-- -->
<tx:annotation-driven proxy-target-class="false" transaction-manager="transactionManager"/>
<!-- ==================================================================================================== -->
<!-- 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>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>BISDAO</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<display-name>contextLoaderListener</display-name>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</webapp>
DAO.java
@Component
@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT, rollbackFor={Exception.class, SQLException.class}, timeout=9999)
public class GenericDAOImpl implements GenericDAO {
private JdbcTemplate jdbcDao;
/**
*
*/
public GenericDAOImpl() {
}
/**
* @return the jdbcDao
*/
public JdbcTemplate getJdbcDao() {
return jdbcDao;
}
/**
* @param jdbcDao the jdbcDao to set
*/
public void setJdbcDao(JdbcTemplate jdbcDao) {
this.jdbcDao = jdbcDao;
}
/* (non-Javadoc)
* @see com.breeze.bis.core.service.jdbcTemplate.GenericDAO#createObject(java.lang.String, java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
public int createObject(String sql, Map<Integer, Object> paramMap) {
boolean hasResult = true, hasKey = true;
IStatementExecutor stmtExecutor = new UpdateStatementExecutor(hasKey);
IExtractor integerExtr = new IntegerExtractor(null, hasResult);
GenericDatabaseExecutor executor = new GenericDatabaseExecutor(stmtExecutor, integerExtr);
GenericPreparedStatementCreator stmtCreator = new GenericPreparedStatementCreator(hasKey, sql, paramMap);
GenericPreparedStatementCallback stmtCallback = new GenericPreparedStatementCallback(executor);
return (Integer) this.jdbcDao.execute(stmtCreator, stmtCallback);
}
}
Servlet.java
@Configurable
public class GenericServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Autowired
GenericDAO dao;
}
请帮忙。
谢谢。