搞了兩天的東西原來只有幾行代碼,就解決問題了,真是讓本身哭笑不得。折騰了1天之久,不想後面的朋友繼續走上個人錯誤之路,把過程給貼出來。java
一、添加struts2&&hibernate3.2&&spring2.5 所需jar包,注意還要添加struts2-spring-plugin-2.1.8.1.jar。web
二、刪除衝突asm2.23.jarspring
三、添加jdbc驅動.jar添加到 %tomcat%/lib 下sql
四、修改%tomcat%/cfg/context.xml文件在<Context>中添加數據庫
<Resource name="jdbc/sql_jndi"
auth="Container"
type="javax.sql.DataSource"
password=""
username="sa"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=taiji_xlg"
maxActive="100" maxIdle="30" maxWait="5000"/> 具體數據庫自行修改。tomcat
五、在WEB-INF/web.xml中添加session
<resource-ref>
<description>demo</description>
<res-ref-name>jdbc/sql_jndi</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>app
六、最後就是配置spring數據源與sessionFacotry函數
<!-- 數據源 -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/sql_jndi"></property>
</bean>sqlserver
<!-- 鏈接工廠 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
</props>
</property>
</bean>
至此,配置完成!
本人犯的錯誤,在main函數中調用ClassPathXMLApplicationContext得到bean,結果老是獲取不到,而且報錯,具體Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
容易犯的錯誤2: jdbc之類的錯誤,注意配置jndiName先後一致,而且java:comp/env/%jndiName%