- 浏览: 884734 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (354)
- Java学习 (122)
- jstl el表达式 (2)
- struts2 标签 (6)
- Ibatis (14)
- S2SH (3)
- struts2 json (2)
- JDK1.6-JDK1.5 (1)
- spring mvc (1)
- 工作项目开发 (8)
- jxl (3)
- jqgrid (9)
- POI (10)
- jQuery (9)
- itext (11)
- oracle (56)
- js (20)
- treegrid (1)
- myeclipse (4)
- Jforum (1)
- iphone (2)
- 角色权限 (1)
- Log4J (5)
- WebService (1)
- spring定时 (1)
- swfupload (12)
- Servlet (1)
- KindEditor (1)
- Java学习,httpclient (2)
- http (4)
- datagrid (1)
- KinSlideshow (1)
- android (7)
- html (1)
- CSS (1)
- 正则表达式 (3)
- easyui (6)
- JSONP (1)
- SQLServer (1)
- tomcat (2)
- Spring (4)
- Jquery plugins (2)
- windows7 (3)
- bootstrap (1)
- FreeMarker (1)
- flex (1)
- ExtJS (6)
- tTP-Link (1)
- MySQL (4)
- JavaScript (3)
- Notepad++ (1)
- ora (1)
- C语言 (1)
- 计算机 (1)
- DWR (2)
- 吉他 (1)
- hibernate (1)
- eclipse (1)
最新评论
-
天使建站:
这里的这篇文章写得很好:http://www.aijquery ...
jS 如何删除二维数组的重复项 -
羽翼的心动:
POI处理的格式单一,无法导出格式比较复杂的表格。POI不能使 ...
poi合并单元格同时导出excel -
zhixinhuacom:
jqgrid 按回车键默认提交数据,怎么做才能时回车不提交数据 ...
JQgrid要实现在jqGrid表格上动态的加行、删行,最后点击“保存”按钮,与后台交互,保存数据 -
w_mojian180:
nice 很多情况都是因为引入文件导入
Uncaught SyntaxError: Unexpected token < -
springdata_springmvc:
bootstrap demo实例教程源代码下载:http:// ...
bootstrap-switch开关按钮表单插件
1、在建立单纯的spring项目时,web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application.xml</param-value>
</init-param>
上面加框的一段是用来指定spring配置文件的(可以为任意名,如上),也可以不写,不写时spring的配置文件默认名为: “<servlet-name>”名-servlet.xml。(如此处的<servlet-name>的值为“spring”,则配置文件名为:springt-servlet.xml)
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
spring配置文件(这里假定为spring-servlet.xml)如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- - Application context definition for "springapp" DispatcherServlet. -->
<beans>
<bean id="LoginAction" class="com.ln.spring.LoginAction">
在bean中配置有属性(包括bean格式的属性)时,bean对应的java文件内必须要定义一个与property的name名字一样的属性值,并且需要有这个属性值的getter和setter方法。
<property name="msg">
<value>Hello World</value>
</property>
<property name="data">
<ref bean="data1" />
当一个bean中有格式为bean的属性时,用“<ref bean="bean-name" />”来指定,在指定”bean-name”后还要在下面用”<bean></bean>”来指定这个bean格式的属性的值,此时”<bean></bean>”中的”id”必须与前面指定的”bean-name”一致。
</property>
</bean>
<bean id="data1" class="com.ln.spring.MyAction">
<property name="message">
<value>liang</value>
</property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/login.do">LoginAction</prop>
</props>
</property>
</bean>
</beans>
而在建立spring+struts项目时,web.xml文件按照struts项目的规格建立,在此不在多说,而在此时struts-config.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.ln.struts.LoginForm" />
</form-beans>
<action-mappings>
<action path="/login"
type="org.springframework.web.struts.DelegatingActionProxy"
name="loginForm">
<forward name="success" path="/main.jsp" />
<forward name="failure" path="/loginfail.jsp" />
</action>
</action-mappings>
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/spring-servlet.xml" />
</plug-in>
</struts-config>
在这个配置文件中,由于使用spring来管理struts的action所以有两个地方要注意:
1. Action的”type”指向spring里定义的用来处理action的类,而不是自己写的action.
2.增加了一个”<plug-in></plug-in>“来指定spring配置文件。
此时spring-servlet.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="/login" class="com.ln.struts.LoginAction"
singleton="false">
此处的bean与struts-config.xml中的action相对应,name与struts-config.xml中action的path一致
<property name="msg">
<value>Hello World</value>
</property>
</bean>
</beans>
2、Spring配置文件总结
首先来看一个标准的Spring配置文件 applicationContext.xml
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName"
default-lazy-init="true">
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>
jdbc:mysql://localhost/ssh?characterEncoding=utf-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123</value>
</property>
</bean>
<!--配置SessionFactory
-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/ssh/pojo/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop
key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- hibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置数据持久层 -->
<bean id="userDao"
class="com.ssh.dao.impl.UserDaoImpl">
<property name="hibernateTemplate"
ref="hibernateTemplate"></property>
</bean>
<!-- 配置业务逻辑层 -->
<bean id="userService"
class="com.ssh.service.impl.UserServiceImpl">
<property name="userDao"
ref="userDao"></property>
</bean>
<!-- 配置控制层 -->
<bean id="UserAction"
class="com.ssh.action.UserAction"
scope="prototype">
<property name="userService"
ref="userService"></property>
</bean>
<!-- 配置pojo -->
<bean id="User" class="com.ssh.pojo.User"
scope="prototype"/>
</beans>
//////////////////////////////////////////////// 下面是详解: ////////////////////////////////////////////////////////////////////////
1.基本配置:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<context:component-scan base-package="com.persia">
<!-- 开启组件扫描 -->
</context:component-scan>
<context:annotation-config>
<!--开启注解处理器-->
</context:annotation-config>
<!-- 使用注解,省去了propertity的xml配置,减少xml文件大小
-->
<bean id="personServiceAnno"
class="com.persia.PersonServiceAnnotation"></bean>
<bean id="personDaoBeanAnno"
class="com.persia.PersonDaoBean"></bean>
<bean id="personDaoBeanAnno2"
class="com.persia.PersonDaoBean"></bean>
<!-- 自动注解 -->
<bean id="personServiceAutoInject"
class="com.persia.PersonServiceAutoInject"
autowire="byName"></bean>
<bean id="personService"
class="com.persia.PersonServiceBean">
<!-- 由spring容器去创建和维护,我们只要获取就可以了 -->
</bean>
<bean id="personService2"
class="com.persia.PersonServiceBeanFactory"
factory-method="createInstance" lazy-init="true"
init-method="init" destroy-method="destory">
<!-- 静态工厂获取bean -->
</bean>
<bean id="fac"
class="com.persia.PersonServiceBeanInsFactory"></bean>
<bean id="personService3" factory-bean="fac"
factory-method="createInstance" scope="prototype">
<!-- 实例工厂获取bean,先实例化工厂再实例化bean-->
</bean>
<!-- ref方式注入属性 -->
<bean id="personDao"
class="com.persia.PersonDaoBean"></bean>
<bean id="personService4"
class="com.persia.PersonServiceBean">
<property name="personDao"
ref="personDao"></property>
</bean>
<!-- 内部bean方式注入
-->
<bean id="personService5" class="com.persia.PersonServiceBean">
<property name="personDao">
<bean
class="com.persia.PersonDaoBean"></bean>
</property>
<property name="name"
value="persia"></property>
<property name="age"
value="21"></property>
<property name="sets">
<!-- 集合的注入 -->
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
<property name="lists">
<!-- 集合的注入 -->
<list>
<value>第一个l</value>
<value>第二个l</value>
<value>第三个l</value>
</list>
</property>
<property name="properties">
<props>
<prop
key="key1">value1</prop>
<prop
key="key2">value2</prop>
<prop
key="key3">value3</prop>
</props>
</property>
<property name="map">
<map>
<entry key="key1"
value="value-1"></entry>
<entry key="key2"
value="value-2"></entry>
<entry key="key3"
value="value-3"></entry>
</map>
</property>
</bean>
<bean id="personService6"
class="com.persia.PersonServiceBean">
<constructor-arg index="0" value="构造注入的name" ></constructor-arg>
<!-- 基本类型可以不写type -->
<constructor-arg index="1"
type="com.persia.IDaoBean" ref="personDao">
</constructor-arg>
</bean>
</beans>2.开启AOP:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="myInterceptor"
class="com.persia.service.MyInterceptor"></bean>
<bean id="personServiceImpl"
class="com.persia.service.impl.PersonServiceImpl"></bean>
</beans>AOP的xml版本<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>
<bean id="aspectBean"
class="com.persia.service.MyInterceptor"></bean>
<aop:config>
<aop:aspect id="myaop" ref="aspectBean">
<aop:pointcut id="mycut" expression="execution(*
com.persia.service.impl.PersonServiceImpl.*(..))"/>
<aop:pointcut id="argcut" expression="execution(*
com.persia.service.impl.PersonServiceImpl.*(..)) and
args(name)"/>
<aop:before pointcut-ref="mycut"
method="doAccessCheck" />
<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
<aop:after-throwing pointcut-ref="mycut"
method="doThrowing"/>
<aop:after pointcut-ref="argcut"
method="doAfter" arg-names="name"/>
<aop:around pointcut-ref="mycut"
method="arround"/>
</aop:aspect>
</aop:config>
</beans>3.开启事务和注解:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username"
value="root"/>
<property name="password"
value=""/>
<!-- 连接池启动时的初始值
-->
<property name="initialSize"
value="1"/>
<!-- 连接池的最大值
-->
<property name="maxActive"
value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle"
value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle"
value="1"/>
</bean>
<!-- 配置事务管理器-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"
ref="dataSource"/>
</bean>
<!-- 配置业务bean -->
<bean id="personService"
class="com.persia.service.impl.PersonServiceImpl">
<property name="ds"
ref="dataSource"></property>
</bean>
<!-- 采用@Transactional注解方式来使用事务 -->
<tx:annotation-driven
transaction-manager="txManager"/>
</beans>XML版本:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username"
value="root"/>
<property name="password"
value=""/>
<!-- 连接池启动时的初始值
-->
<property name="initialSize"
value="1"/>
<!-- 连接池的最大值
-->
<property name="maxActive"
value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle"
value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle"
value="1"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"
ref="dataSource"/>
</bean>
<!-- 配置业务bean -->
<bean id="personService"
class="com.persia.service.impl.PersonServiceImpl">
<property name="ds"
ref="dataSource"></property>
</bean>
<!-- 使用XML来使用事务管理-->
<aop:config>
<!-- 配置一个切面,和需要拦截的类和方法
-->
<aop:pointcut id="transactionPointcut"
expression="execution(* com.persia.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut-ref="transactionPointcut"/>
</aop:config>
<!-- 配置一个事务通知 -->
<tx:advice id="txAdvice"
transaction-manager="txManager">
<tx:attributes>
<!-- 方法以get开头的,不使用事务 -->
<tx:method name="get*"
read-only="true" propagation="NOT_SUPPORTED"/>
<!-- 其他方法以默认事务进行
-->
<tx:method
name="*"/>
</tx:attributes>
</tx:advice>
</beans>4.SSH:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username"
value="root"/>
<property name="password"
value=""/>
<!-- 连接池启动时的初始值
-->
<property name="initialSize"
value="1"/>
<!-- 连接池的最大值
-->
<property name="maxActive"
value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle"
value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle"
value="1"/>
</bean>
<!-- 配置hibernate的sessionFactory
-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref
bean="dataSource" /></property>
<property name="mappingResources">
<list>
<value>com/persia/model/Person.hbm.xml</value>
</list>
</property>
<!-- 1.首先在sessionFactory里面配置以上3条设置 -->
<!-- 2.然后得在类路径下面添加一个ehcache.xml的缓存配置文件 -->
<!-- 3.最后在要使用缓存的实体bean的映射文件里面配置缓存设置 -->
<!--使用二级缓存-->
<!-- 不使用查询缓存,因为命中率不是很高 -->
<!-- 使用Ehcache缓存产品
-->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>
<!-- 配置Spring针对hibernate的事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"
ref="sessionFactory"/>
</bean>
<!-- 配置使用注解的方式来使用事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 使用手工配置的注解方式来注入bean -->
<context:annotation-config></context:annotation-config>
<!--定义要注入的业务bean -->
<bean id="personService"
class="com.persia.service.impl.PersonServiceImpl"></bean>
<!--将Struts的action交给Spring容器来管理 -->
<bean name="/person/list"
class="com.persia.struts.PersonListAction">
<!--1.这里要求name和struts-config里面的action的path名称一致,因为id不允许有特殊字符-->
<!--2.还得在Struts-config文件里面添加Spring的请求处理器,该处理器会根据action的path属性到Spring容器里面寻找这个bean,若找到了则用这个bean来处理用户的请求-->
<!--3.然后去掉action的type标签和值(可选),当Spring处理器找不到该bean时,才会使用Struts的action-->
<!--4.最后在action里面使用Spring的注入方式来注入业务bean-->
</bean>
<bean name="/person/manage"
class="com.persia.struts.PersonManageAction"></bean>
</beans>5.SSH2:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username"
value="root"/>
<property name="password"
value=""/>
<!-- 连接池启动时的初始值
-->
<property name="initialSize"
value="1"/>
<!-- 连接池的最大值
-->
<property name="maxActive"
value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle"
value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle"
value="1"/>
</bean>
<!-- 配置hibernate的sessionFactory
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref
bean="dataSource" /></property>
<property name="mappingResources">
<list>
<value>com/persia/model/Person.hbm.xml</value>
</list>
</property>
<!-- 1.首先在sessionFactory里面配置以上3条设置 -->
<!-- 2.然后得在类路径下面添加一个ehcache.xml的缓存配置文件 -->
<!-- 3.最后在要使用缓存的实体bean的映射文件里面配置缓存设置 -->
<!--使用二级缓存-->
<!-- 不使用查询缓存,因为命中率不是很高 -->
<!-- 使用Ehcache缓存产品
-->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>
<!-- 配置Spring针对hibernate的事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"
ref="sessionFactory"/>
</bean>
<!-- 配置使用注解的方式来使用事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 使用手工配置的注解方式来注入bean -->
<context:annotation-config></context:annotation-config>
<!--定义要注入的业务bean -->
<bean id="personService"
class="com.persia.service.impl.PersonServiceImpl"></bean>
<!--注入Struts 2的action
-->
<bean id="personList"
class="com.persia.struts2.action.PersonListAction"></bean>
</beans>6.SSJ:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<!-- 使用手工配置的注解方式来注入bean -->
<context:annotation-config></context:annotation-config>
<!-- 1.配置Spring集成JPA
-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property
name="persistenceUnitName" value="SpringJPAPU"/>
</bean>
<!--2.配置Spring针对JPA的事务 -->
<bean id="txManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory"/>
</bean>
<!--3.开启事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!--以上3个Spring集成JPA的配置,在web项目先添加Spring支持,后添加JPA支持时会自动生成 -->
<!-- 配置业务bean -->
<bean id="personService"
class="com.persia.service.impl.PersonServiceImpl"></bean>
<!-- 配置Struts的action
-->
<bean name="/person/list"
class="com.persia.struts.PersonListAction"/>
<bean name="/person/manage"
class="com.persia.struts.PersonManageAction"/>
</beans>
3、Spring事务配置的五种方式
前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。
总结如下:
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。
DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问 时,DataSource实际为SessionFactory,TransactionManager的实现为 HibernateTransactionManager。
具体如下图:
根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:
第一种方式:每个Bean都有一个代理
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property
name="configLocation" value="classpath:hibernate.cfg.xml" />
<property
name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property
name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置DAO -->
<bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
<property
name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property
name="target" ref="userDaoTarget" />
<property
name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
<!-- 配置事务属性 -->
<property
name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
第二种方式:所有Bean共享一个代理基类
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property
name="configLocation" value="classpath:hibernate.cfg.xml" />
<property
name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property
name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionBase"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<!-- 配置事务管理器 -->
<property
name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property
name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
<property
name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao" parent="transactionBase" >
<property
name="target" ref="userDaoTarget" />
</bean>
</beans>
第三种方式:使用拦截器
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property
name="configLocation" value="classpath:hibernate.cfg.xml" />
<property
name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property
name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property
name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property
name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property
name="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
<property
name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
<property
name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
第四种方式:使用tx标签配置的拦截器
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bluesky" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property
name="configLocation" value="classpath:hibernate.cfg.xml" />
<property
name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property
name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut
id="interceptorPointCuts"
expression="execution(*
com.bluesky.spring.dao.*.*(..))" />
<aop:advisor
advice-ref="txAdvice"
pointcut-ref="interceptorPointCuts" />
</aop:config>
</beans>
第五种方式:全注解
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bluesky" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property
name="configLocation" value="classpath:hibernate.cfg.xml" />
<property
name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property
name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
此时在DAO上需加上@Transactional注解,如下:
package com.bluesky.spring.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import com.bluesky.spring.domain.User;
@Transactional
@Component("userDao")
public class UserDaoImpl extends
HibernateDaoSupport implements UserDao {
public List<User> listUsers() {
return this.getSession().createQuery("from User").list();
}
<!--[if gte vml 1]><v:shape
id="图片_x0020_2" o:spid="_x0000_i1025" type="#_x0000_t75" alt="http://www.blogjava.net/Images/dot.gif"
style='width:11.25pt;height:15pt;visibility:visible;mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image002.gif"
o:title="dot" />
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
}
发表评论
-
Java中List Set Map 是否有序等总结
2015-05-05 11:09 662转http://blog.csdn.net/zhengqiq ... -
ava操作word宏
2015-04-29 10:44 471转:http://aa8945163.iteye.com/b ... -
System.load 和 System.loadLibrary详解
2015-04-09 10:50 569转:http://www.cnblogs.com/h ... -
(转)谈谈对Java中Unicode、编码的理解
2015-03-03 11:07 380转:http://www.cnblogs.com/newst ... -
HTTP status code 说明
2015-01-15 16:15 446200 – 服务器成功返回 ... -
JAVA邮件发送
2015-01-13 16:56 586转:http://www.cnblogs.com/codep ... -
Java排序算法 记录
2015-01-13 10:24 382转:http://www.cnblogs.com/dians ... -
java中volatile关键字
2015-01-05 14:30 452转:http://www.cnblogs.com/aigon ... -
4.1 Servlet简介
2014-12-22 16:34 388转:http://www.blogjava.net/ ... -
在 Windows 中实现 Java 本地方法
2014-12-21 15:18 420转:http://www.ibm.com/devel ... -
严重: Error listenerStart
2014-11-18 17:14 3822007-5-31 14:27:13 org.apache. ... -
java.security.AccessControlException: access denied 的解决方法
2014-11-07 22:07 3286转:http://blog.csdn.net/maomao ... -
Linux下反斜杠号"\"
2014-10-27 13:36 2433转:http://www.cnblogs.com/Muyou ... -
@override在JDK1.5和JDK1.6中用法区别
2014-10-14 22:54 357转:http://gaowenming.iteye.com/ ... -
ClientAbortException:java.io.IOException解决方案
2014-09-09 10:13 12454转:http://blog.sina.com.cn/s/bl ... -
SchedulerFactoryBean 注入
2014-08-26 10:19 774转:http://blog.csdn.net/neutro ... -
Spring 3整合Quartz 2实现定时任务二:动态添加任务
2014-08-26 09:47 786转:http://www.meiriyouke.net/?p ... -
Spring 3整合Quartz 2实现定时任务一:常规整合
2014-08-26 09:45 615转:http://www.meiriyouke.net/?p ... -
Spring 3整合Quartz 2实现定时任务三:动态暂停 恢复 修改和删除任务
2014-08-26 09:21 2192转http://my.oschina.net/u/1177 ... -
java.lang.UnsupportedClassVersionError: Bad version number in .class file
2014-08-18 16:45 341今天新建了一个基于jdk6的工程,在tomcat中部署之后, ...
相关推荐
MVC实例!初学者可以看看!MVC实例!MVC案例!
python基础教程:推荐几本Python入门书籍,初学者必看!.docxpython基础教程:推荐几本Python入门书籍,初学者必看!.docxpython基础教程:推荐几本Python入门书籍,初学者必看!.docxpython基础教程:推荐几本Python入门...
webwork+spring+ibatis很适合初学者的实例
初学asp.net开发实例!(仅限初学者)
这是springcloud的实例demo,非常适合初学者学习!!!
初学SpringCloud简单配置Oracle数据库,从Oracle数据库中获取数据
华为交换机配置实例,适合初学者,高手,全部是实例,企业级别的首选!
org.springframework.remoting.httpinvoker最基本的实现例子,这是3个eclipse的JavaEE工程,全部导入即可,能运行。 初学可以轻松了解Spring HttpInvoker 的结构和使用。
Servlet JSP和Spring MVC初学指南
pb6.5初级教程适合初学者使用!!!pb6.5初级教程适合初学者使用!!!
初学spring者的福音。 spring简介 spring应用 spring整合hibernate spring整合struts
Spring框架,非常简单Demo,只适用于初学者。 1 首先我们需要一个bean,用某种方法(设置注入需要有setter函数,构造注入需要有构造函数,相应的bean.xml配置文件也会不同)设定注入方式。 2 bean.xml,设置bean...
spring mvc 实例有数据库,jar包,代码之间还有详细的注解,特别适合初学者学习,之间导入myeclipse工程中就可以用.希望对初学者学习spring mvc框架有帮助.
初学Spring的小伙伴,还在为找不到主配置文件而着急吗,该配置文件是基于Spring4.10版本的,不用再自己到官网页面上到处找了,分享给大家,也希望大家别再像我一样因为这个坑耽误了大半天的时间,如果要自己配置也...
跟老师做的一些spring的小实例,对于初学者是很有用的,希望对大家有点用
721配置实例,解释的精简,适合初学者。
Spring 参考手册 Spring速查手册 spring初学者使用
简单实用struts+spring+hibernate配置实例--.rar 配置简单清晰 最适合初学着学习
spring jdbctemplate组件的简单实例。可以直接运行该实例来学习spring的jdbctemplate。处于初学者,或开发互联网性能要求较高的比较有料。