2010-02-25 55 views
1

我想部署一個簡單的彈簧應用程序。它需要一個用戶名(來自jsp),將該值插入數據庫,並在新的jsp頁面上顯示問候語hello!, [username]IllegalArgumentException在Spring應用程序中的dataSource

我的環境是:

  • 操作系統:Windows XP專業
  • Db的:MS訪問(這僅僅是在第一次嘗試的東西出來,打算轉移到MySQL)
  • IDE:月食
  • 服務器:Tomcat的6

我得到一個錯誤如下:

Feb 25, 2010 11:21:04 AM org.springframework.web.servlet.FrameworkServlet processRequest SEVERE: Could not complete request java.lang.IllegalArgumentException: dataSource is required 
     at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSe(JdbcAccessor.java:130) 
     at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:122) 
     at SpringClasses.Profile.setUsername(Profile.java:32) ……… 

ApplicationContext的文件是如下:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
    <property name="dataSource"><ref bean="dataSource"/></property> 
</bean> 

<bean id="Profile" class="SpringClasses.Profile"> 
    <property name="dataSource"><ref bean="dataSource"/></property> 
</bean> 

<bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource" singleton="true" destroy-method="close"> 
    <property name="driverClassName" value="sun.jdbc.odbc.JdbcOdbcDriver"/> 
    <property name="url" value="jdbc:odbc:usernamedb"/> 
    <property name="username" value="admin"/> 
    <property name="password" value=""/> 
</bean> 
</beans> 
</code> 

我已受到控制面板中設置的DSN(usernamedb) - >管理員工具 - > ODBC來源 - >用戶DSN.The問題似乎是與dataSource的URL。

從控制器類和豆類相關的Java代碼爲如下

從ProfileFormController.java類:

protected ModelAndView onSubmit(Object command) 
{ 
    Profile profile = (Profile) command; 
    String greeting = "Hello," + profile.getUsername() + "!"; 
    profile.setUsername(profile.getUsername()); 
    return new ModelAndView("greetingDisplay", "greeting", greeting); 
} 

從Profile.java類:

private String username; 
private JdbcTemplate jt; 
private DataSource dataSource; 

public Profile() { 
} 

public Profile(String username) { 
this.username = username; 
} 

public String getUsername() { 
return username; 
} 
public String setUsername(String username) { 
int rowsInserted; 
setDataSource(dataSource); 
jt = new JdbcTemplate(dataSource); 
rowsInserted = jt.update("insert into username_db (username) values(?)",new Object[]  {username}); 

//System.out.println("In Profile.getUsername, num. of rows inserted:"+rowsInserted); 
return username; 
} 
public void setDataSource(DataSource dataSource) { 
this.jt = new JdbcTemplate(dataSource); 
    } 

在web.xml中配置Profile bean,如下所示:

在web.xml

public ProfileFormController() { 
    setCommandClass(Profile.class); 
    setCommandName("profile"); } 

增加時,ContextLoaderServlet太:

在ProfileFormController,我設置的構造如下。

我錯過了什麼?非常感謝幫助。

謝謝
Neetu。

+0

顯示如何配置控制器,以及從何處獲取配置文件命令 – 2010-02-25 10:51:12

+0

編輯以提供來自web.xml的配置詳細信息 – Pratyusha 2010-02-25 11:07:48

+0

儘管在問題中提及Access非常有用,但我不認爲該標記有用,因爲你的問題與數據存儲有關。 – 2010-02-25 19:30:32

回答

1

如果您在方法formBackingObject中創建Profile命令,那麼它將不會被Spring處理,也不會被填充爲dataSource。你可以用這個方法來設置它,或者從spring beans holder中獲取bean。

另外你需要知道的是,當你創建spring bean時,默認情況下它是單例。這意味着您的所有控制器都將使用(並修改)其中的一個實例。 Imho它不是你想要的。你可以使用scope="request"作爲你的模型bean。

或者最好保持模型清潔,就像簡單的POJO一樣,並將所有以數據庫爲中心的代碼移動到控制器/數據庫層。

0

您試圖在spring中應用DDD(域驅動設計),這並不容易。「落差」,是因爲:

  • 域對象必須由你
  • 域對象實例化應該是春季管理,以適用依賴注入。

Read this關於使用spring實現DDD的一些提示。

或者如果您不確定是否要使用DDD,請遵循splix的建議並將業務操作中的數據分開。 (DDD意味着你的類定義了數據和數據上的操作,在非DDD中它們是分開的,你得到了異常,因爲(可能)Profile是由你實例化的,而不是由Spring框架)

+0

按照splix的建議,我創建了一個包含數據庫相關操作的ProfileDao類。我仍然不明白爲什麼我會得到「dataSource required exception」。另外,我是一名春季新手,整個DDD部分都超過了我的頭。我需要添加/刪除一些配置嗎?更改網址可能? – Pratyusha 2010-02-26 05:24:09

+0

沒有。 DDD意味着您的對象包含數據和對該數據的操作。在非DDD中,它們是分開的。你正在得到這個異常,因爲(或許)Profile是由你實例化的,而不是Spring框架。 – Bozho 2010-02-26 06:20:37

相關問題