AOP的hello world
来源:互联网 发布:类似易企秀的软件 编辑:程序博客网 时间:2024/06/01 22:05
AOP使用的三种方式: 1.ProxyFactoryBean代理工具类生成 2.自动代理生成器 3.AspectJ对AOP的实现
在spring中使用aop开发时,一般使用aspectj的实现方式
使用aop所需要的jar包 spring-aop 和 aopalliance
一、ProxyFactoryBean代理工具类生成
1.目标类
public class StudentServiceImpl implements IStudentService{ public StudentServiceImpl() { System.out.println("执行无参构造方法"); } public void doSome(){ System.out.println("执行doSome"); }}2.切面类
public class MyTransactionProcessor implements MethodBeforeAdvice { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("事务代码,正在执行的是:" + o.getClass().getName()); }}
3.xml配置
4.测试<!--静态工厂bean--><bean id="studentService" class="com.mh.spring.factory.StudentServiceFactory" factory-method="getInstance"/><!--配置切面:通知--><bean id="processor" class="com.mh.spring.executor.MyTransactionProcessor"/><!--配置顾问:名称匹配方式--><!--<bean id="pointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="processor"/> <property name="mappedNames" value="doSome,doOther"/></bean>--><!--配置顾问:正则匹配方式--><bean id="pointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="processor"/> <property name="pattern" value=".*doS.*"/></bean><!--配置代理--><bean id="studentServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="studentService"/> <property name="interfaces" value="com.mh.spring.service.IStudentService"/> <property name="interceptorNames" value="pointcutAdvisor"/></bean>
@Testpublic void testDoSome() throws Exception { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); IStudentService studentService = (IStudentService) ac.getBean("studentServiceProxy"); studentService.doSome();}
二、自动代理生成器(1. 默认advisor自动代理生成器 2. Bean名称自动代理生成器)
只是xml配置有变动和测试类有变动,其它同上
1. 默认advisor自动代理生成器
1.1 xml配置
<!--静态工厂bean--><bean id="studentService" class="com.mh.spring.factory.StudentServiceFactory" factory-method="getInstance"/><!--配置切面:通知--><bean id="processor" class="com.mh.spring.executor.MyTransactionProcessor"/><!--配置顾问:名称匹配方式--><!--<bean id="pointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="processor"/> <property name="mappedNames" value="doSome,doOther"/></bean>--><!--配置顾问:正则匹配方式--><bean id="pointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="processor"/> <property name="pattern" value=".*doS.*"/></bean><!--配置自动代理--><bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
1.2 测试
@Testpublic void testDoSome() throws Exception { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); IStudentService studentService = (IStudentService) ac.getBean("studentService"); studentService.doSome();}
2.Bean名称自动代理生成器
1).添加目标类
public class OtherServiceImpl implements IStudentService { public void doSome(){ System.out.println("正的执行 OtherService.doSome()"); }}2).xml配置
<!--目标类:普通方式创建bean--><bean id="otherService" class="com.mh.spring.service.impl.OtherServiceImpl" /><!--目标类:静态工厂bean--><bean id="studentService" class="com.mh.spring.factory.StudentServiceFactory" factory-method="getInstance"/><!--配置切面:通知--><bean id="processor" class="com.mh.spring.executor.MyTransactionProcessor"/><!--配置顾问:名称匹配方式--><!--<bean id="pointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="processor"/> <property name="mappedNames" value="doSome,doOther"/></bean>--><!--配置顾问:正则匹配方式--><bean id="pointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="processor"/> <property name="pattern" value=".*doS.*"/></bean><!--配置自动代理--><bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames" value="otherService,studentService"/> <property name="interceptorNames" value="pointcutAdvisor"/></bean>3)测试
@Testpublic void testDoSome() throws Exception { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); IStudentService studentService = (IStudentService) ac.getBean("studentService"); studentService.doSome(); IStudentService otherService = (IStudentService) ac.getBean("otherService"); otherService.doSome();}
3.AspectJ式的aop
要引入两个jar包spring-aspects和weaver,aop约束
1)定义业务接口及实现类
public class StudentServiceImpl implements IStudentService{ public void doSome(){ System.out.println("执行doSome"); } @Override public void doOther(int a, int b) { System.out.println("执行doOther方法"); } @Override public String doThird() { System.out.println("执行doThird方法"); return "aaaa"; }}2)定义切面
public class MyAspect { public void before(){ System.out.println("前置增强"); } public void afterReturning(Object result){ System.out.println("后置增强:目标方法执行结果为:" + result); }}3)xml配置
<!--目标对象--><bean id="studentService" class="com.mh.spring.service.impl.StudentServiceImpl"/><!--切面--><bean id="myAspect" class="com.mh.spring.executor.MyAspect"/><!--配置AOP--><aop:config> <!--切入点--> <aop:pointcut id="myDoSomePointcut" expression="execution(* *..StudentServiceImpl.doSome())"/> <aop:pointcut id="myDoOtherPointcut" expression="execution(* *..StudentServiceImpl.doOther())"/> <aop:pointcut id="myDoThirdPointcut" expression="execution(* *..StudentServiceImpl.doThird())"/> <!--切面--> <aop:aspect ref="myAspect"> <aop:before method="before" pointcut-ref="myDoSomePointcut"/> <aop:after-returning method="afterReturning" pointcut-ref="myDoThirdPointcut" returning="result"/> </aop:aspect></aop:config>4)测试
@Testpublic void testDoSome() throws Exception { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); IStudentService studentService = (IStudentService) ac.getBean("studentService"); studentService.doSome(); studentService.doThird();}
0 0
- AOP的hello world
- Spring AOP 的 Hello World
- Spring.AOP下的 hello world !
- Spring AOP之Hello World
- Spring AOP之Hello World
- Spring AOP之Hello World
- Spring AOP之Hello World
- spring & aop- hello world (一)
- aop aspectj [intellij idea配置] [hello world]
- JMX的Hello World
- Hello World 的故事
- 汇编的HELLO WORLD!
- JAVA 的HELLO WORLD
- 汇编语言的Hello world
- Python 的Hello World
- 千奇百怪的Hello World
- Itouch 的Hello world
- 汇编的hello world
- js获取select标签选中的值
- Codeforces Round #408 (Div. 2) D.Police Stations【Bfs+思维】
- okHttp网络请求框架 get 及post请求方式
- 【Spring学习29】cglib的Lookup方法
- bzoj4551: [Tjoi2016&Heoi2016]树
- AOP的hello world
- leetcode 100. Same Tree
- 1037. 在霍格沃茨找零钱(20)
- atcoder square869120Contest #4 D
- 【深度学习】caffe 中的一些参数介绍
- 林子雨—大数据技术原理与应用—上机实验五
- 序列化语义
- Java序列化与反序列化 & 深拷贝
- Mysql出现Can’t connect to MySQL server on ‘localhost’ (0)问题