属性'dataSource'是必需的java(Spring)错误 [英] Property 'dataSource' is required Error in java (Spring)

查看:381
本文介绍了属性'dataSource'是必需的java(Spring)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Java(Spring)中开发一个Web应用程序

I am developing an web application in Java (Spring)

我的java文件是,

try
    {
        JdbcTemplate jt = new JdbcTemplate(dataSource);

        System.out.println("Connection ....."+jt.toString());

        Connection conn;
        Statement st;
        conn =DriverManager.getConnection(jt.toString());
        conn = (Connection) jt.getDataSource();
        st=conn.createStatement();
        System.out.println("Connection created....."+st);
    }
    catch (Exception e) {
         System.out.println("Error Found...."+ e.getMessage());
         System.out.println("Strack Trace....."+e.getStackTrace());
    }

我的 spring-servlet.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:3306/cjbranchdb" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
    <property name="dataSource"><ref bean="dataSource"/></property> 
</bean> 

但它收到错误,

Error Found: Property 'dataSource' is required.
Strack Trace: [Ljava.lang.StackTraceElement;@7948dd

在这里,我想要在Java文件中建立连接并将其作为Jasper Report传递给另一个变量。

Here, I want to make a connection in Java file and pass it to the another variable as Jasper Report.

请帮助,如何解决此问题?

Please help, How to fix this issue?

推荐答案

我猜你是Java,JEE,Spring和JDBC的新手。正如我在评论中所述,很难回答你的问题,如果你在那里所做的事情在其基础上是不正确的。我将尝试通过一些主题,并希望确定您当前问题的位置。

I am guessing you are completely new to Java, JEE, Spring and JDBC. As I have stated in my comment, it is hard to answer your question, if what you are doing in there is incorrect in its base. I will try to go through few topics and hopefully also pin point where your current issue is.

您需要确保正确构建项目:

You need to be sure to correctly structure your project:


  • src

    • main

      • java - Java源代码

        • in / mmali / springtest / controller / IndexController.java - 你的控制器类

        • src
          • main
            • java - directory for Java sources
              • in/mmali/springtest/controller/IndexController.java - Your controller class

              • WEB-INF / web.xml - JEE Web应用程序配置

              • WEB-INF / spring-servlet.xml - 调度员servlet的应用程序上下文配置

              • WEB-INF/web.xml - JEE web application configuration
              • WEB-INF/spring-servlet.xml - application context configuration for dispatcher servlet

              我会打电话这是Java项目的一个常见结构,主要由Maven标准化。

              I would call this a common structure for Java project, mostly "standardized" by Maven.

              你需要拥有正确的 web.xml 配置:

              You need to have correct web.xml configuration:

              <?xml version="1.0" encoding="UTF-8"?>
              <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 
              
                  <servlet>
                      <servlet-name>spring</servlet-name>
                      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                      <load-on-startup>1</load-on-startup>
                  </servlet>
              
                  <servlet-mapping>
                      <servlet-name>spring</servlet-name>
                      <url-pattern>/</url-pattern>
                  </servlet-mapping>
              
              </web-app>
              

              这是一个基本配置(没有root上下文),它将使用你的 spring-servlet.xml 作为Spring上下文配置。

              This is a basic configuration (without root context), which will use your spring-servlet.xml as Spring context configuration.

              您需要具有正确的Spring上下文配置:

              You need to have correct Spring context configuration:

              <?xml version="1.0" encoding="UTF-8"?>
              <beans xmlns="http://www.springframework.org/schema/beans"
                  xmlns:context="http://www.springframework.org/schema/context"
                  xmlns:mvc="http://www.springframework.org/schema/mvc"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                                      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                                      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
                  <mvc:annotation-driven />
                  <mvc:resources location="/resources/" mapping="/resources/**" />
              
                  <!-- With ROOT context we would restrict component scan on controllers here -->
                  <context:component-scan base-package="in.mmali.springtest" />
              
                  <!-- Data source configuration would normally go inside ROOT context. -->
                  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                      <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                      <property name="url" value="jdbc:mysql://localhost:3306/cjbranchdb" />
                      <property name="username" value="root" />
                      <property name="password" value="root" />
                  </bean>
                  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
                      <property name="dataSource" ref="dataSource" />
                  </bean> 
              </beans>
              

              这将加载所有使用 @Component (和它的同伴 @Controller @Service @Repository )作为你的豆子。 Spring应用程序上下文中的 Bean 是由Spring管理的对象 - 即由Spring本身实例化的对象。当你想使用Spring bean时,你需要注入它(例如使用 @Autowired 注释)或者你需要从 ApplicationContext#getBean 手动。

              This will load all classes annotated with @Component (and its companions @Controller, @Service, @Repository) as your beans. Bean in a context of Spring application is a object managed by Spring -> i.e. object which is being instantiated by Spring itself. When you want to work with a Spring bean, you need to have it injected (e.g. by using @Autowired annotation) or you need to pull it out from ApplicationContext#getBean manually.

              使用JDBC是所有可关闭的资源和已检查的异常都很痛苦。这就是为什么Spring-JDBC项目包装JDBC API所以你不必使用它。

              Working with JDBC is painful with all the closeable resources and checked exceptions. That is why Spring-JDBC project wraps JDBC API so you don't have to use it.

              展示如何使用JDBC以及如何让Spring注入依赖项,这里是一个简单的控制器:

              To showcase how you should work with JDBC and also how to let Spring inject dependencies, here is a simple controller:

              @Controller // Will be detected by <context:component-scan>
              @RequestMapping // Will be detected by <mvc:annotation-driven> (more specifically by one of its component - RequestMappingHandlerMapping)
              public class IndexController {
              
                  @Autowired // Spring will inject JdbcTemplate here
                  private JdbcOperations jdbcOperations; 
              
                  @RequestMapping // This method should be called for requests to "/" 
                  @ResponseBody // Returned string will be returned to client... normally you would register view resolver and just return name of a JSP to render
                  public String renderIndex() {
                      // You don't need to worry about JDBC's DataSource, Connection, ResultSet, ... just use JdbcTemplate
                      long rowCount = jdbcOperations.queryForLong("SELECT COUNT(*) FROM my_test_table;");
                      return "Number of rows in database is: " + String.valueOf(rowCount);
                  } 
              
              }
              

              注意,在实际应用中您不允许控制器直接使用您的数据源,而是通过服务和数据层。

              Note, that in a real application you would not allow controller to work with your data source directly, but rather through service and data layer.


              • 开始使用日志记录系统,再也不要在Web应用程序中使用 System.out.println ;)。我建议 slf4j 使用简单的绑定启动(稍后您可以将其配置为使用 logback log4j )。

              • 配置您的应用程序以使用交易。使用Spring的事务处理(< tx:annotation-driven /> @Transactional )。它起初可能看起来很神奇,但是当你发现有关AOP和代理类的东西时,你会开始真正地了解Spring的工作原理。

              • 将应用程序逻辑拆分为服务层和数据(DAO)层。

              • 检查Spring的示例应用程序 http://docs.spring.io/docs/petclinic.html )和参考申请( https ://github.com/spring-projects/greenhouse )。

              • Start using logging system and never use System.out.println in a web application again ;). I suggest slf4j with its simple binding for start (later you can configure it to use logback or log4j).
              • Configure your application to use transactions. Use Spring's transaction handling (<tx:annotation-driven/> with @Transactional). It might look as magic at first, but when you discover something about AOP and proxy classes, you will then start really appretiating the principles of how Spring works.
              • Split your application logic to service layer and data (DAO) layer.
              • Check Spring's sample application (http://docs.spring.io/docs/petclinic.html) and reference application (https://github.com/spring-projects/greenhouse).

              这篇关于属性'dataSource'是必需的java(Spring)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆