搞了两天的东西原来只有几行代码,就解决问题了,真是让本身哭笑不得。折腾了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%