Spring 3 JavaConfig - 注入DataSource的问题

时间:2013-12-06 01:58:39

标签: java spring datasource code-injection

我是Spring的新手,我正在尝试将DataSource注入工作。我得到一个“属性'dataSource'是必需的”错误。被遗忘了一段时间。在此先感谢您的帮助!

WebConfig.java

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.test")
public class WebConfig extends WebMvcConfigurerAdapter {

@Autowired
DataSource Source;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    System.out.println("addResourceHandlers :: init");
    registry.addResourceHandler("/resources/**").addResourceLocations(
            "/resources/");
}

@Bean
public InternalResourceViewResolver viewResolver() {
    System.out.println("viewResolver :: init");
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

}

AppConfig.java

import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
@ComponentScan(basePackages = { "com.test" })
public class AppConfig {

@Bean
public DataSource dataSource() {
    System.out.println("userDBDatasource :: init");
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/test");
    dataSource.setUsername("foo");
    dataSource.setPassword("foo");
    return dataSource;
}

}

JDBCUserDaoImpl.java

package com.test.foo;

import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class JDBCUserDAOImpl implements JDBCUserDAO {

private Logger logger = Logger.getRootLogger();
private JdbcTemplate jdbcTemplate;
private DataSource dataSource;

@Autowired
public void setDataSource(DataSource dataSource) {
    logger.debug("setDataSource :: called");
    this.dataSource = dataSource;
}

public List<UserBean> getUsers() {
    logger.info("Getting List of Users!");
    List<UserBean> userBeans = new ArrayList<UserBean>();
    String sql = "SELECT * FROM users";
    jdbcTemplate = new JdbcTemplate(dataSource);
    userBeans = jdbcTemplate.query(sql, new UserBeanRowMapper());
    return userBeans;
}
}

错误

Dec 07, 2013 10:46:53 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [main] in context with path [/springmvc] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required] with root cause
java.lang.IllegalArgumentException: Property 'dataSource' is required
    at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:134)
    at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:165)
    at com.tajima.models.JDBCUserDAOImpl.getUsers(JDBCUserDAOImpl.java:42)
    at com.tajima.controllers.HomeController.loadUserList(HomeController.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

2 个答案:

答案 0 :(得分:1)

@Import注释提供了这种支持,它直接等同于Spring bean XML文件中的元素。

您错过了将AppConfig类导入WebMvcConfig类文件。因此,Spring无法使用名称&#39; dataSource&#39;。

创建bean

在WebMvcConfig类中,您需要使用@Import注释导入DataSource配置类。这将有助于通过spring Container创建dataSource bean。

 ex:@Configuration
    @EnableWebMvc
    @Import({AppConfig.class})
    @ComponentScan(basePackages = "com.test")
    public class WebConfig extends WebMvcConfigurerAdapter {

我希望已将webConfig类正确指定为Dispatcher servlet的contextConfigLocation参数。如果没有指定如下指定

<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <param-name>contextClass</param-name>
  <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>yourpackage.config.WebMvcConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
 <servlet-name>mvc</servlet-name>
 <url-pattern>/</url-pattern>
</servlet-mapping>

或者您可以将此上下文类和contextConfiglocation指定为ContextLoaderLister作为上下文参数,如下所示

   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 <context-param>
<param-name>contextClass</param-name>
<param- value>org.springframework.web.context.support.AnnotationConfigWebApplicationC     ontext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>yourpackage.WebMvcConfig</param-value>
</context-param>

答案 1 :(得分:0)

从WebConfig.java中删除DataSource注入