主要目的是回忆记录 Spring+proxool+hibernate+struts2+aop 的web项目实现过程
2015-12-21晚实现 ,jar包及完整源码下载见文章末尾
工具:MyEclipse10 ; mysql5 ; tomcat7 ; JDK1.7 pring3 ;hibernate3
下面是项目一步一步搭建的过程,不是一口吃成胖子的哦
前题:已经会单独使用hibernate、struts2、proxool
还不会使用的见这几篇文章
hibernate单独使用:http://blog.csdn.net/i_do_can/article/details/50380213
struts2单独使用:http://blog.csdn.net/i_do_can/article/details/50379809
proxool单独使用:http://blog.csdn.net/i_do_can/article/details/50380040
一、spring整合proxool
1.新建一个web项目
2.导入proxooljar包
3.修改web.xml文件添加如下内容
测试用访问地址 /admin <servlet> <servlet-name>Admin</servlet-name> <servlet-class> org.logicalcobwebs.proxool.admin.servlet.AdminServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>Admin</servlet-name> <url-pattern>/admin</url-pattern> </servlet-mapping>
5.添加spring框架的jar包6.修改web.xml添加如下内容
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/spring.xml</param-value> </context-param>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!—整合proxool连接池--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> </property> <property name="url"> <value>proxool.database</value> </property> </bean> </beans>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- 此处的ref="dataSource" 用的是上文中 <bean id="dataSource" --> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value></value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean>
3.需要新加一个jar包:struts2-spring-plugin-2.3.16.1.jar
4.运行没有异常发生说明整合正确
但是出错啦!
error:Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classes/spring.xml]: Invocation of init method failed;
百度到:http://spiritfrog.iteye.com/blog/709043
预先在"web.xml"先配置好Proxool连接池,这种方式(本例)下Spring的上下文加载如果想使用listener方式(Struts2要求),
则与连接池有关的Bean全得延迟初始化。因为listener比servlet优先初始化,
如果相关Bean不是lazy-init的话,则启动服务器时会出现Bean找不到连接定义的异常。
其实这上面的可能也对,但是没有去验证,同时也不是本项目的问题,问题在于我的spring.xml是这么写
<!-- 注解类 -->
<property name="annotatedClasses">
<list>
<value></value>
</list>
</property>
添上<value>com.cl.spring.bean.User</value> 值即可
1.jar包
2.新建struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <span style="white-space:pre"> </span><constant name="struts.objectFactory" value="spring" /> <span style="white-space:pre"> </span><package name="default" namespace="/" extends="struts-default" > <span style="white-space:pre"> </span><!-- class 为 spring 里 id --> <span style="white-space:pre"> </span><action name="test" class="testAction" > <span style="white-space:pre"> </span><result>/login.jsp</result> <span style="white-space:pre"> </span></action> <span style="white-space:pre"> </span><action name="addUser" class="testAction" method="addUser" > <span style="white-space:pre"> </span><result type="redirectAction">test</result> <span style="white-space:pre"> </span></action> <span style="white-space:pre"> </span></package> </struts>
3.看一下现在的目录结构:
4.spring.xml文件
<!-- spring proxool --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> </property> <property name="url"> <value>proxool.messageboard</value> </property> </bean> <!-- spring hibernate --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 注解类 --> <property name="annotatedClasses"> <list> <value>com.cl.spring.bean.User</value> <value>com.cl.spring.bean.Board</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- extends HibernateDaoSupport --> <bean id="userDao" class="com.cl.spring.dao.UserDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userService" class="com.cl.spring.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> <bean id="testAction" class="com.cl.spring.action.TestAction"> <property name="userService" ref="userService"></property> </bean>
5.proxool.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE proxool-config> <proxool-config> <proxool> <alias>messageboard</alias> <driver-url> <!-- 设置数据库编码,主要是中文会变成 "?????????????" 很讨厌--> <![CDATA[ jdbc:mysql://localhost/message_board?useUnicode=true&characterEncoding=utf8 ]]> </driver-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <driver-properties> <property name="user" value="root"/> <property name="password" value=""/> </driver-properties> <maximum-connection-count>30</maximum-connection-count> <minimum-connection-count>10</minimum-connection-count> <house-keeping-sleep-time>90000</house-keeping-sleep-time> <prototype-count>5</prototype-count> <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql> </proxool> </proxool-config>
看一下userservice类
userDao类:
运行 访问 地址 /test
运行结果:
Hibernate:
select
user0_.id as id0_,
user0_.email as email0_,
user0_.note as note0_,
user0_.password as password0_,
user0_.username as username0_
from
tbl_user user0_
5
三、继续整合AOP,进行事物控制:(增加删除数据时使用)
先看看不增加AOP怎么进行事物控制
那新增AOP呢 ,最后一句就可以去掉啦
1.需要jar包:
jar:aopalliance.jar aspectjweaver-1.5.0.jar cglib-nodep-2.1_3.jar
2.修改spring.xml文件
<!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 配置事务传播特性 --> <tx:advice id="TestAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="find*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" /> <tx:method name="apply*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 配置参与事务的类 --> <aop:config> <!-- 正则表达式 com.cl.spring.service 下所有的类的所有的方法 --> <aop:pointcut id="allTestServiceMethod" expression="execution(* com.cl.spring.service.*.*(..))" /> <aop:advisor pointcut-ref="allTestServiceMethod" advice-ref="TestAdvice" /> </aop:config>
只是一个简单的整合,但是把改用到的技术都用到啦
有需要的可以自行下载源码扩展
为什么需要 1 分,首先申明我不是缺这一分,是担心有人瞎下载源码,用作其他用途,所以加了限制,但是又不会需要你太多分
如果你需要,但是有没有分,可以私信我,或者:[email protected],不一定及时回复哦!
项目的完整jar:http://download.csdn.net/detail/i_do_can/9371702
项目的完整源码:http://download.csdn.net/detail/i_do_can/9371710