属性'dataSource'是必需的java(Spring)错误

Mah*_*ali 2 java spring datasource

我正在用Java开发一个Web应用程序(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());
    }
Run Code Online (Sandbox Code Playgroud)

我的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> 
Run Code Online (Sandbox Code Playgroud)

但它得到一个错误,因为,

Error Found: Property 'dataSource' is required.
Strack Trace: [Ljava.lang.StackTraceElement;@7948dd
Run Code Online (Sandbox Code Playgroud)

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

请帮忙,如何解决这个问题?

Pav*_*ral 7

我猜你是Java,JEE,Spring和JDBC的新手.正如我在评论中所述,很难回答你的问题,如果你在那里所做的事情在其基础上是不正确的.我将尝试通过几个主题,并希望也指出你当前的问题.

Spring应用程序结构

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

  • src
    • main
      • java - Java源代码目录
        • in/mmali/springtest/controller/IndexController.java - 你的控制器类
      • resources - 非Java(重新)源的目录
      • webapp - 用于Web应用程序资源的root
        • WEB-INF/web.xml - JEE Web应用程序配置
        • WEB-INF/spring-servlet.xml - 调度程序servlet的应用程序上下文配置
  • pom.xml - Maven配置(如果你使用Maven)

我称之为Java项目的一个常见结构,主要是由Maven"标准化".

纠正JEE配置

您需要具有正确的web.xml配置:

<?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>
Run Code Online (Sandbox Code Playgroud)

这是一个基本配置(没有根上下文),它将使用您的spring-servlet.xmlSpring上下文配置.

正确的Spring配置

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

<?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>
Run Code Online (Sandbox Code Playgroud)

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

使用JDBC

使用JDBC对于所有可关闭的资源和已检查的异常都很痛苦.这就是Spring-JDBC项目包装JDBC API的原因,因此您不必使用它.

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

@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);
    } 

}
Run Code Online (Sandbox Code Playgroud)

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

下一步

  • 开始使用日志记录系统,永远不再System.out.println在Web应用程序中使用;).我建议slf4j使用简单的绑定来启动(稍后你可以将它配置为使用logbacklog4j).
  • 配置应用程序以使用事务.使用Spring的事务处理(<tx:annotation-driven/>with @Transactional).起初它可能看起来很神奇,但是当你发现有关AOP和代理类的东西时,你会开始真正地了解Spring的工作原理.
  • 将应用程序逻辑拆分为服务层和数据(DAO)层.
  • 检查Spring的示例应用程序(http://docs.spring.io/docs/petclinic.html)和参考应用程序(https://github.com/spring-projects/greenhouse).