10 SPring编写代理半自动和全自动
AOP 联盟通知类型
aop联盟为通知Advice定义了org.aopalliance.aop.advice
spring按照通知Advice在目标类方法的连接点位置,可以分为5类
- 前置通知:org.springframework.aop.MethodBeforeAdvice
在目标方法执行前实施增强
- 后置通知:org.springframework.aop.AfterReturningAdvice
在目标方法执行后实施增强
- 环绕通知:org.aopalliance.intercept.MethodInterceptor
在目标方法执行前后实施增强
- 异常抛出通知:org.springframework.aop.ThrowsAdvice
在方法抛出异常后实施增强
- 引介通知:org.springframework.aop.introductionInterceptor
在目标类中添加一些新的方法和属性
spring编写代理半自动
目标:掌握让spring创建代理对象,从spring容器中手动的获取代理对象。
第一步:导入jar包
【核心4+1,aop联盟(规范),spring-aop(实现)】

接口类:IStudentService.java
1 package com.iwakan.service; 2 3 public interface IStudentService { 4 5 //切面编程 6 public void addStudent(); 7 8 public void updateStudent(); 9 10 public int deleteStudent(int id); 11 12 }
目标类:StudentServiceImpl.java
1 package com.iwakan.service; 2 3 public class StudentServiceImpl implements IStudentService{ 4 5 //切面编程 6 public void addStudent(){ 7 System.out.println("添加学生信息。。。"); 8 } 9 10 public void updateStudent(){ 11 System.out.println("更新学生信息。。。"); 12 } 13 14 public int deleteStudent(int id){ 15 System.out.println("根据id删除学生信息。。。"); 16 return 1; 17 } 18 19 }
切面类:MyAspect.java
1 package com.iwakan.service; 2 3 import org.aopalliance.intercept.MethodInterceptor; 4 import org.aopalliance.intercept.MethodInvocation; 5 6 /** 7 * 切面类:增强代码与切入点 结合 8 */ 9 public class MyAspect implements MethodInterceptor { 10 11 public void before(){ 12 System.out.println("开启事务。。。"); 13 } 14 public void after(){ 15 System.out.println("提交事务。。。"); 16 } 17 18 @Override 19 public Object invoke(MethodInvocation methodInvocation) throws Throwable { 20 //拦截方法 21 22 //开启事务 23 before(); 24 //放行 25 Object retObj = methodInvocation.proceed(); 26 //提交事务 27 after(); 28 return retObj; 29 } 30 }
配置文件:beans.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 <!--配置UserService--> 10 <bean id="studentService" class="com.iwakan.service.StudentServiceImpl"></bean> 11 <!--配置切面类--> 12 <bean id="myAspect" class="com.iwakan.service.MyAspect"></bean> 13 <!--配置代理对象--> 14 <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> 15 <!--接口:如果只是一个接口,就写value,如果是多个接口,用list标签--> 16 <property name="interfaces" value="com.iwakan.service.IStudentService"></property> 17 <!--目标对象--> 18 <property name="target" ref="studentService"></property> 19 <!--切面类--> 20 <property name="interceptorNames" value="myAspect"></property> 21 <!--配置使用cglib生成--> 22 <property name="optimize" value="true"></property> 23 </bean> 24 </beans>
主方法:main.java
1 package com.iwakan.test; 2 3 import com.iwakan.service.IStudentService; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class Run { 8 public static void main(String[] args) { 9 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 10 IStudentService serviceProxy = (IStudentService)context.getBean("serviceProxy"); 11 serviceProxy.addStudent(); 12 System.out.println("============================="); 13 serviceProxy.updateStudent(); 14 System.out.println("============================="); 15 serviceProxy.deleteStudent(10); 16 } 17 18 }
结果:
开启事务。。。
添加学生信息。。。
提交事务。。。
=============================
开启事务。。。
更新学生信息。。。
提交事务。。。
=============================
开启事务。。。
根据id删除学生信息。。。
提交事务。。。
spring编写代理全自动
导入jar包:com.springsource.org.aspectj.weaver-1.7.2.RELEASE.jar

接口类:IStudentService.java
1 package com.iwakan.service;
2
3 public interface IStudentService {
4
5 //切面编程
6 public void addStudent();
7
8 public void updateStudent();
9
10 public int deleteStudent(int id);
11
12 }
目标类:StudentServiceImpl.java
1 package com.iwakan.service;
2
3 public class StudentServiceImpl implements IStudentService{
4
5 //切面编程
6 public void addStudent(){
7 System.out.println("添加学生信息。。。");
8 }
9
10 public void updateStudent(){
11 System.out.println("更新学生信息。。。");
12 }
13
14 public int deleteStudent(int id){
15 System.out.println("根据id删除学生信息。。。");
16 return 1;
17 }
18
19 }
切面类:MyAspect.java
1 package com.iwakan.service;
2
3 import org.aopalliance.intercept.MethodInterceptor;
4 import org.aopalliance.intercept.MethodInvocation;
5
6 /**
7 * 切面类:增强代码与切入点 结合
8 */
9 public class MyAspect implements MethodInterceptor {
10
11 public void before(){
12 System.out.println("开启事务。。。");
13 }
14 public void after(){
15 System.out.println("提交事务。。。");
16 }
17
18 @Override
19 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
20 //拦截方法
21
22 //开启事务
23 before();
24 //放行
25 Object retObj = methodInvocation.proceed();
26 //提交事务
27 after();
28 return retObj;
29 }
30 }
配置文件:beans1.xml
<aop:config proxy-target-class="true"> proxy-target-class 如果不写的话,默认为jdk动态代理,写的话代表使用cglib实现动态代理
expression表达式:*为任意
execution(* com.iwakan.service.*.*(..))
第一个*代表返回值
第二个*代表类名
第三个*代表方法名
(..)代表参数
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop.xsd"> 12 <!--配置UserService--> 13 <bean id="studentService" class="com.iwakan.service.StudentServiceImpl"></bean> 14 <!--配置切面类--> 15 <bean id="myAspect" class="com.iwakan.service.MyAspect"></bean> 16 <!--全自动aop配置 17 1.在bean中配置aop约束 18 2.配置aop:config内容,把切入点和通知结合 19 --> 20 <aop:config proxy-target-class="true"> 21 <!--切入点 22 expression:表达式 23 每个service的方法前面都开启事务和提交事务 24 --> 25 <aop:pointcut id="myPointcut" expression="execution(* com.iwakan.service.*.*(..))"></aop:pointcut> 26 <!--通知--> 27 <aop:advisor advice-ref="myAspect" pointcut-ref="myPointcut"></aop:advisor> 28 </aop:config> 29 </beans>
主方法:main.java
1 package com.iwakan.test; 2 3 import com.iwakan.service.IStudentService; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class Run1 { 8 public static void main(String[] args) { 9 ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml"); 10 IStudentService serviceProxy = (IStudentService)context.getBean("studentService"); 11 serviceProxy.addStudent(); 12 System.out.println("============================="); 13 serviceProxy.updateStudent(); 14 System.out.println("============================="); 15 serviceProxy.deleteStudent(10); 16 } 17 18 }
结果:
开启事务。。。
添加学生信息。。。
提交事务。。。
=============================
开启事务。。。
更新学生信息。。。
提交事务。。。
=============================
开启事务。。。
根据id删除学生信息。。。
提交事务。。。

浙公网安备 33010602011771号