java.lang.IllegalArgumentException:属性'dataSource'是必需的

时间:2016-12-21 02:53:52

标签: spring spring-mvc datasource spring-jdbc jdbctemplate

我正在研究Spring MVC应用程序。当我尝试运行应用程序时,我收到以下异常:

java.lang.IllegalArgumentException: Property 'dataSource' is required

我试图关注stackverflow中的其他帖子并基于commnets实现,但没有解决问题。请找到以下文件:

Servlet的context.xml中

<beans:bean id="SearchResultsServiceDAOImpl"  class="com.easyFinder.app.ServiceDAOImpl.SearchResultsServiceDAOImpl">
          <beans:property name="dataSource"  ref="dataSource" />    
</beans:bean>

<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
    <beans:property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
    <beans:property name="username" value="sysman"/>
    <beans:property name="password" value="orcl"/>
</beans:bean>  

SearchresultsServiceDAOImpl.java

private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}

public String enterIntoNewEntrytable(Date date, String message) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    List args = new ArrayList();
    String query = "INSERT INTO NEW_ENTRY VALUES ('1',?,?)";
    jdbcTemplate = new JdbcTemplate(dataSource);
    args.add(date);
    args.add(message);
    jdbcTemplate.update(query,args);
    return null;
}

堆栈跟踪:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/app] 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:135)
    at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:168)
    at com.easyFinder.app.ServiceDAOImpl.SearchResultsServiceDAOImpl.enterIntoNewEntrytable(SearchResultsServiceDAOImpl.java:28)
    at com.easyFinder.app.SearchActionController.home(SearchActionController.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    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.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    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:100)
    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:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

请帮我找一个解决方案。

3 个答案:

答案 0 :(得分:0)

你需要

<beans:bean id="searchResultsServiceDAO"  class="com.easyFinder.app.ServiceDAOImpl.SearchResultsServiceDAOImpl">
    <beans:property name="dataSource"  ref="dataSource" />    
</beans:bean>

接口

public interface SearchresultsServiceDAO {
   public String enterIntoNewEntrytable(Date date, String message);
}

然后

public class SearchresultsServiceDAOImpl 
    implements SearchresultsServiceDAO {

    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public String enterIntoNewEntrytable(Date date, String message) {
        //Then you can use
        Connection conn = dataSource.getConnection();
        // ...
        return null;
    }

}

如果要将数据源注入JdbcTemplate使用

<beans:bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
    <beans:property name="dataSource" ref="dataSource" />
</beans:bean> 

并以这种方式使用

@Autowired 
JdbcTemplate jdbcTemplate;

您可能还需要<context:annotation-config/>,具体取决于您的配置

答案 1 :(得分:0)

您需要在配置中使用interface个定义,而不是implementation

您目前已定义哪个引用实现,

<beans:bean id="SearchResultsServiceDAOImpl"  class="com.easyFinder.app.ServiceDAOImpl.SearchResultsServiceDAOImpl">
          <beans:property name="dataSource"  ref="dataSource" />    
</beans:bean>

而是更新它(请验证路径),

<beans:bean id="searchResultsServiceDAO"  class="com.easyFinder.app.ServiceDAO.SearchResultsServiceDAO">
    <beans:property name="dataSource" ref="dataSource" />    
</beans:bean>

我看到你使用了constructor注射。但是您可以对inject bean使用基于注释的实现方法,因为这将删除在每个DAO构造函数中传递dataSource的要求。

检查@Autowired文档。

@Autowired
DataSource dataSource;

答案 2 :(得分:0)

确保初始化DataSource和JdbcTemplate,并在数据流中定义getJdbcTemplate(任何地方,但应该完成)。以下是一个例子:

@Autowired
protected DataSource dataSource;

/** Spring JDBC helper. */
public JdbcTemplate jdbcTemplate;

/**
 * Gets the jdbc template. Instantiates on first call.
 * 
 * @return the jdbc template
 */
public JdbcTemplate getJdbcTemplate() {
    if (null == jdbcTemplate) {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }
    return jdbcTemplate;
}

当我直接调用impl类时,我得到了相同的异常,但这些异常是在基类中启动的。