我在使用 @Autowired 注释将 xml 文件中定义的数据源 bean 放入 Spring 中的类时遇到了一些麻烦。
有人知道这里发生了什么吗?似乎 *Impl 类中的 dataSource 没有被连接。
dao-context.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:8889/spring" />
<property name="username" value="user" />
<property name="password" value="1234" />
</bean>
PersonDaoImpl.java
public class PersonDaoImpl implements PersonDao {
@Autowired
private DataSource dataSource;
private JdbcTemplate jdbcTemplate = new JdbcTemplate();
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
System.out.println("SETTING DATA SOURCE");
this.dataSource = dataSource;
}
public List<Person> getPersonList() {
// TODO Auto-generated method stub
return null;
}
public void savePerson(Person person) {
// TODO Auto-generated method stub
}
public Person getPersonById(int id) {
System.out.println("Getting Person: " + id);
return null;
}
@Override
public int getPersonCount(){
String sql = "SELCECT COUNT(*) FROM PERSON";
jdbcTemplate.setDataSource(getDataSource());
int count = jdbcTemplate.queryForInt(sql);
System.out.println("Count of Person: " + count);
return count;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
...最后,例外。
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No DataSource specified
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
java.lang.IllegalArgumentException: No DataSource specified
org.springframework.util.Assert.notNull(Assert.java:112)
org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:97)
org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:382)
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:464)
org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:472)
org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:477)
org.springframework.jdbc.core.JdbcTemplate.queryForInt(JdbcTemplate.java:486)
com.myPackage.rest.dao.impl.PersonDaoImpl.getPersonCount(PersonDaoImpl.java:50)
com.myPackage.rest.HomeController.home(HomeController.java:36)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
任何帮助将不胜感激,干杯。