@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)
どんな助けでも大歓迎です、乾杯。