Spring中的事務html
java <bean id="user1" class="pojo.User"></bean>
xml <bean id="user2" class="pojo.User"> <property name="id" value="2"></property> <property name="username" value="李四"></property></bean>
xml <bean id="user3" class="pojo.User"> <constructor-arg value="1"> </constructor-arg> <constructor-arg value="張三"> </constructor-arg></bean>
xml <bean id="user5" class="factory.Dfactory" factory-method="b"></bean>
普通工廠方法
xml <bean id="c" class="factory.Dfactory"></bean><bean id="user4" factory-bean="c" factory-method="a"></bean>
java
普通方法spring
<!--外部注入--> <bean id="userDao" class="com.Spring.dao.impl.UserDaoImpl"> </bean> <bean id="userService" class="com.Spring.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property></bean> <bean id="userAction" class="com.Spring.controller.UserAction"> <property name="userService" ref="userService"></property></bean>
xml <!--內部注入--> <bean id="userAction" class="com.Spring.controller.UserAction"> <property name="userService"> <bean class="com.Spring.service.impl.UserServiceImpl"> <property name="userDao"> <bean class="com.Spring.dao.impl.UserDaoImpl"> </bean> </property> </bean> </property> </bean>
xmlns:p="http://www.springframework.org/schema/p"
<!--p命名空間注入--> <bean id="userDao" class="com.Spring.dao.impl.UserDaoImpl"> </bean> <bean id="userService" class="com.Spring.service.impl.UserServiceImpl" p:userDao-ref="userDao"></bean> <bean id="userAction" class="com.Spring.controller.UserAction" p:userService-ref="userService"></bean>
xml <!--自動裝配 byName --> <bean id="userDao" class="com.Spring.dao.impl.UserDaoImpl"></bean> <bean id="userService" class="com.Spring.service.impl.UserServiceImpl" autowire="byName"></bean> <bean id="userAction" class="com.Spring.controller.UserAction" autowire="byName"></bean>
xml <!--自動裝配 byType --> <bean id="userDao" class="com.Spring.dao.impl.UserDaoImpl"> </bean> <bean id="userService" class="com.Spring.service.impl.UserServiceImpl" autowire="byType"> </bean> <bean id="userAction" class="com.Spring.controller.UserAction" autowire="byType"></bean>
須要環境: 幫助文檔搜索xmlns:context
數據庫
xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
java package com.Spring.test; /** * @Author Jamin <br> * @Date 2019/3/26 15:43 <br> * @Description 接口 */public interface SALE { String sale(int money);}
實現類
java package com.Spring.test; /** * @Author Jamin <br> * @Date 2019/3/26 15:44 <br> * @Description * 目標對象 * */ public class Apple{ public String sale(int money) { return "賣蘋果手機"; }}
express
package com.Spring.test; /** * @Author Jamin <br> * @Date 2019/3/26 15:47 <br> * @Description * * * 靜態代理 */ public class AppleProxy implements SALE { private Apple apple = new Apple(); public AppleProxy(Apple apple) { this.apple = apple; } @Override public String sale(int money) { String s=apple.sale(money); String a="買手機送手機殼"; return s+a; } }
package com.Spring.test; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * @Author Jamin <br> * @Date 2019/3/26 16:19 <br> * @Description */ public class SaleFactory { private Object o; public SaleFactory(Object o) { this.o = o; } public Object factory() { return Proxy.newProxyInstance( o.getClass().getClassLoader(), o.getClass().getInterfaces(), new InvocationHandler() { /** * * @param proxy 被代理的對象 * @param method 要調用的方法 * @param args 方法調用時所須要的參數 */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("aaaaaaaaaaaaaaa"); Object invoke = method.invoke(o, args); System.out.println("bbbbbbbbbbbbbbb"); return invoke; } }); } }
Cglib代理 子類代理
```java
package com.Spring.test;編程
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;緩存
import java.lang.reflect.Method;併發
/**public Cglib(Object o) {
this.o = o;
}mvc
public Object getProxy() {
Enhancer en = new Enhancer();
en.setSuperclass(o.getClass());
en.setCallback(this);
return en.create();
}app
@Override
public Object intercept(Object obj, Method method, Object[] objects, MethodProxy methodProxy)
throws Throwable {
System.out.println("aaaaaaaaaaaaa");
Object invoke = method.invoke(o, objects);
System.out.println("bbbbbbbbbbbb");
return invoke;
}
}
xml <!--建立目標對象--> <bean id="empServiceImpl" class="com.spring.EmpService.EmpServiceImpl"></bean> <!--建立代理對象--> <bean id="empAop" class="com.spring.aop.EmpAop"></bean> <!--將對象織入目標對象中--> <aop:config> <!--配置切入點--> <aop:pointcut id="pt" expression="execution(* com.spring.EmpService.EmpServiceImpl.*())"></aop:pointcut> <!--指定通知--> <aop:aspect ref="empAop"> <aop:before method="getBefore" pointcut-ref="pt"></aop:before> <aop:after method="getAfter" pointcut-ref="pt"></aop:after> <aop:around method="getRound" pointcut-ref="pt"></aop:around> <aop:after-returning method="getReturn" pointcut-ref="pt"></aop:after-returning> <aop:after-throwing method="getThrowable" pointcut-ref="pt"></aop:after-throwing> </aop:aspect> </aop:config>
方法
```java
package com.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint; /** * @Author Jamin <br> * @Date 2019/3/27 10:22 <br> * @Description */ public class EmpAop { /** 前置通知 */ public void getBefore() { System.out.println("前置通知"); } /*後置通知*/ public void getAfter() { System.out.println("後置通知"); } /*環繞通知*/ public Object getRound(ProceedingJoinPoint pj) throws Throwable { System.out.println("環繞通知前"); Object proceed = pj.proceed(); System.out.println("環繞通知後"); return proceed; } /*返回通知*/ public void getReturn() { System.out.println("返回通知"); } /*異常通知*/ public void getThrowable() { System.out.println("異常通知"); } }```
配置文件
<!--建立目標對象--> <bean id="empServiceImpl" class="com.spring.EmpService.EmpServiceImpl"></bean> <!--建立代理對象--> <bean id="empAop2" class="com.spring.aop.EmpAop2"></bean> <!--開啓自動代理--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
方法
package com.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.core.annotation.Order; /** * @Author Jamin <br> * @Date 2019/3/27 14:02 <br> * @Description */ @Aspect public class EmpAop2 { @Pointcut("execution(* com.spring.EmpService.EmpServiceImpl.*())") public void pointCut() {} /** 前置通知 */ @Before("pointCut()") public void getBefore() { System.out.println("前置通知"); } /*返回通知*/ @AfterReturning("pointCut()") public void getReturn() { System.out.println("返回通知"); } /*後置通知*/ @After("pointCut()") public void getAfter() { System.out.println("後置通知"); } /*環繞通知*/ @Order(2) @Around("pointCut()") public Object getRound(ProceedingJoinPoint pj) throws Throwable { System.out.println("環繞通知前"); Object proceed = pj.proceed(); System.out.println("環繞通知後"); return proceed; } /*異常通知*/ @AfterThrowing("pointCut()") public void getThrowable() { System.out.println("異常通知"); } }
xml <!-- 定義事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSources"/> </bean> <!--配置事務通知--> <tx:advice id="txadvice" transaction-manager="transactionManager"> <!--事務操做--> <tx:attributes> <tx:method name="account*"/> </tx:attributes> </tx:advice> <!--切面配置--> <aop:config> <!--切入點service層的實現方法--> <aop:pointcut id="pt" expression="execution(* com.springmvc.*())"/> <!--切面--> <aop:advisor advice-ref="txadvice" pointcut-ref="pt"></aop:advisor> </aop:config>
註解
<!-- 定義事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSources"/> </bean> <!--開啓註解事務管理--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> <!--在須要事務管理的方法上添加註解 @Transactional-->