2014-02-10 27 views
1

我正在開發中的Java(春季)屬性 '數據源',需要在Java(春季)錯誤

我的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()); 
    } 

春天的Web應用程序-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傳遞給另一個變量。

請幫助,如何解決這個問題?

+1

我不得不說,有在從你的問題的代碼太多的問題。你可能對Java世界是全新的。我建議你閱讀最新的Spring文檔,教程和參考應用程序。 –

+1

@PavelHoral可以請您糾正我的代碼問題或有關問題的存在......請...... –

回答

7

我猜你對Java,JEE,Spring和JDBC是全新的。正如我在評論中所說的那樣,如果你在那裏做的是不正確的,它很難回答你的問題。我會嘗試通過幾個主題,並希望能夠指出你目前的問題在哪裏。

春天應用結構

您需要確保正確地構建您的項目:

  • src
    • main
      • java - 爲Java源代碼目錄
        • in/mmali/springtest/controller/IndexController.java - 控制器類
      • resources - 目錄非Java(RE)來源
      • webapp - 根的Web應用程序資源
        • WEB-INF/web.xml - JEE的Web應用程序配置
        • WEB-INF/spring-servlet.xml - 應用程序上下文調度程序servlet的配置
  • pom.xml - Maven的配置(如果你正在使用Maven)

我會叫這個Maven的共同結構的Java項目,大多是 「標準化」。

正確的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> 

這是一個基本配置(無根上下文),它會使用你的spring-servlet.xml爲Spring上下文配置。

正確的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> 

這將加載與@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); 
    } 

} 

注意,在實際的應用中,你不會允許控制器與你的工作數據源,而是通過服務和數據層。

下一個步驟,使用

  • 開始記錄系統,從來沒有在Web應用程序再次使用System.out.println)。我建議slf4j以其簡單的啓動綁定(稍後您可以配置它使用logbacklog4j)。
  • 配置應用程序使用交易。使用Spring的事務處理(<tx:annotation-driven/>@Transactional)。它可能一開始看起來很神奇,但是當你發現AOP和代理類的一些東西時,你會開始真正欣賞Spring的工作原理。
  • 分割應用程序邏輯到服務層和數據(DAO)層。
  • 查看Spring的示例應用程序http://docs.spring.io/docs/petclinic.html)和參考應用程序(https://github.com/spring-projects/greenhouse)。
+0

我可以在'spring-dispatcher-servlet.xml'文件中添加數據庫連接屬性嗎? – viper

+0

是的,你可以,但它沒有那麼多的意義 - 數據庫連接通常是你需要有根上下文的東西。 –

0

JdbcTemplate jt = new JdbcTemplate(dataSource);你不能只使用new關鍵字來構造一個bean對象。很明顯,該對象中的引用將爲空。

相反,你應該問春天給你那個bean對象。有些事情是這樣的。

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-servlet.xml"); 
JdbcTemplate jt = (JdbcTemplate)context.getBean("JdbcTemplate"); 

在這種情況下,spring會注入依賴項,如配置中給出的datasource

+0

沒有做過先生至少說明.... –

+0

它得到下列錯誤,發現錯誤.... IOException異常從類解析XML文檔路徑資源[jsp/spring-servlet.xml];嵌套異常是java.io.FileNotFoundException:類路徑資源[JSP /彈簧servlet.xml中]不能被打開,因爲它不存在 Strack的痕量..... [Ljava.lang.StackTraceElement; @ 1a295d6 –

+0

彈簧的servlet .xml應該出現在類路徑 – Subin

0

根據文件名,我假設你正在構建一個基於Web的應用程序。

爲了捕獲錯誤,請使用正確的日誌框架而不是System.out.println。

根據錯誤,dataSource似乎爲空。敬請檢查您的web.xml定義servlet的XML文件,如

<servlet> 
    <servlet-name>spring_app</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

此外,DriverManagerDataSource這個僅用於測試不夠好。對於生產用途,請考慮Apache的Jakarta Commons DBCP或C3P0。詳見官方webpage

由於您的XML文件中提到

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

,XML文件中應包含一個servlet部分的來電ID = JdbcTemplate的對象。例如,

<bean id="MyServlet" class="com.my.MyServlet"> 
    <property name="jdbcTemplate"><ref bean="JdbcTemplate"/></property> 
</bean>