我是Spring的新手。我试图使用@Autowired注释将数据源实例注入我的DAOImpl类。但是数据源始终为空。
我的主要课程:
package com.cts.main;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import com.cts.status.bizImpl.AddTableDetailsBizImpl;
import com.cts.status.dao.JDBCEmployeeDetailsDAO;
public class Main {
public static void main(String[] args) {
XmlBeanFactory factory = AddTableDetailsBizImpl.getApplicationContext();
JDBCEmployeeDetailsDAO dao = (JDBCEmployeeDetailsDAO) factory.getBean("JDBCEmployeeDetailsDAO");
dao.insertEmployeeDetails(null);
}
}
我实现接口的DAOImpl类如下:
package com.cts.status.dao;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import com.cts.status.bean.EmployeeDetails;
public class JDBCEmployeeDetailsDAOImpl implements JDBCEmployeeDetailsDAO{
private DataSource dataSource;
private JdbcTemplate jdbctemplate;
public DataSource getDataSource() {
return dataSource;
}
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbctemplate = new JdbcTemplate(dataSource);
}
@Override
public void insertEmployeeDetails(EmployeeDetails employeeDetails) {
System.out.println("HIIIIII");
}
@Override
public EmployeeDetails retrieveEmployeeDetails(int employeeId) {
System.out.println("HIIIIII");
return null;
}
My Spring Config 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cts.status.dao"/>
<bean id="JDBCEmployeeDetailsDAO" class="com.cts.status.dao.JDBCEmployeeDetailsDAOImpl">
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url" value="jdbc:derby://localhost:1527/StatusGen" />
<property name="username" value="app" />
<property name="password" value="app" />
</bean>
</beans>
当我运行Main类时,我能够创建JDBCEmployeeDetailsDAO类型的实例,但数据源值为null。
我不确定我错过了什么。请帮助。
由于 维杰