spring增强
1.前置增强
接口:ISomeService
public interface ISomeService { public void doSome(); }
类
public class MyBeforeAdvise implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("==========log=========="); } }
public class SomeService implements ISomeService{ //核心业务 public void doSome() { System.out.println("拜托别让他一番努力换来是奢求!"); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象--> <bean id="someService" class="cn.happy.spring04aop01.SomeService"></bean> <!--02.前置增强(通知)--> <bean id="beforeAdvice" class="cn.happy.spring04aop01.MyBeforeAdvise"></bean> <!--03.aop--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvice"></property> <property name="proxyTargetClass" value="true"></property> </bean> </beans>
单测
//前置增强 @Test public void test05(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext01_aop01.xml"); SomeService service = (SomeService) ctx.getBean("proxyService"); service.doSome(); }
2.后置增强
接口:ISomeService
public interface ISomeService { public void doSome(); }
类
public class MyAfterReturningAdvice implements AfterReturningAdvice { public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("==========after=========="); } }
public class SomeService implements ISomeService { //核心业务 public void doSome() { System.out.println("拜托别让他一番努力换来是奢求!"); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象--> <bean id="someService" class="cn.happy.spring05aop_postposition.SomeService"></bean> <!--02.增强(通知)--> <bean id="afterReturning" class="cn.happy.spring05aop_postposition.MyAfterReturningAdvice"></bean> <!--03.aop--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <property name="interceptorNames" value="afterReturning"></property> </bean> </beans>
单测
//后置增强 @Test public void test06(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext02_aop02.xml"); ISomeService service = (ISomeService) ctx.getBean("proxyService"); service.doSome(); }
3.环绕增强
接口:ISomeService
public interface ISomeService { public void doSome(); }
类
public class MyMethodInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("before"); methodInvocation.proceed(); System.out.println("after"); return null; } }
public class SomeService implements ISomeService { //核心业务 public void doSome() { System.out.println("拜托别让他一番努力换来是奢求!"); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象--> <bean id="someService" class="cn.happy.spring06aop_convolution.SomeService"></bean> <!--02.增强(通知)--> <bean id="methodAdvice" class="cn.happy.spring06aop_convolution.MyMethodInterceptor"></bean> <!--03.aop--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <property name="interceptorNames" value="methodAdvice"></property> </bean> </beans>
单测
//环绕增强 @Test public void test07(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext03_aop03.xml"); ISomeService service = (ISomeService) ctx.getBean("proxyService"); service.doSome(); }
4.异常增强
接口:ISomeService Mystoo
public interface ISomeService { public void doSome(); }
public interface Mystoo { public void run(); public void run(String style); }
类
public class MystooImpl implements Mystoo { public void run() { } public void run(String style) { } }
public class MyThrowsAdvice implements ThrowsAdvice { public void afterThrowing(Exception ex){ int age=6/0; System.out.println("错误"); } }
public class SomeService implements ISomeService { //核心业务 public void doSome() { System.out.println("拜托别让他一番努力换来是奢求!"); } public void doSecont() { } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象--> <bean id="someService" class="cn.happy.spring07aop_abnormal.SomeService"></bean> <!--02.增强 通知--> <bean id="throwsAdvice" class="cn.happy.spring07aop_abnormal.MyThrowsAdvice"></bean> <!--03.aop --> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <!--做怎么样的增强--> <property name="interceptorNames" value="throwsAdvice"></property> </bean> </beans>
单测
//异常增强 @Test public void test08(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext04_aop04.xml"); ISomeService service = (ISomeService) ctx.getBean("proxyService"); service.doSome(); }
Spring增强
1.前置增强
接口:ISomeService
public interface ISomeService { public void doSome(); }
类
public class MyBeforeAdvise implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("==========log=========="); } }
public class SomeService implements ISomeService{ //核心业务 public void doSome() { System.out.println("拜托别让他一番努力换来是奢求!"); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象--> <bean id="someService" class="cn.happy.spring04aop01.SomeService"></bean> <!--02.前置增强(通知)--> <bean id="beforeAdvice" class="cn.happy.spring04aop01.MyBeforeAdvise"></bean> <!--03.aop--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvice"></property> <property name="proxyTargetClass" value="true"></property> </bean> </beans>
单测
//前置增强 @Test public void test05(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext01_aop01.xml"); SomeService service = (SomeService) ctx.getBean("proxyService"); service.doSome(); }
2.后置增强
接口:ISomeService
public interface ISomeService { public void doSome(); }
类
public class MyAfterReturningAdvice implements AfterReturningAdvice { public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("==========after=========="); } }
public class SomeService implements ISomeService { //核心业务 public void doSome() { System.out.println("拜托别让他一番努力换来是奢求!"); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象--> <bean id="someService" class="cn.happy.spring05aop_postposition.SomeService"></bean> <!--02.增强(通知)--> <bean id="afterReturning" class="cn.happy.spring05aop_postposition.MyAfterReturningAdvice"></bean> <!--03.aop--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <property name="interceptorNames" value="afterReturning"></property> </bean> </beans>
单测
//后置增强 @Test public void test06(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext02_aop02.xml"); ISomeService service = (ISomeService) ctx.getBean("proxyService"); service.doSome(); }
3.环绕增强
接口:ISomeService
public interface ISomeService { public void doSome(); }
类
public class MyMethodInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("before"); methodInvocation.proceed(); System.out.println("after"); return null; } }
public class SomeService implements ISomeService { //核心业务 public void doSome() { System.out.println("拜托别让他一番努力换来是奢求!"); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象--> <bean id="someService" class="cn.happy.spring06aop_convolution.SomeService"></bean> <!--02.增强(通知)--> <bean id="methodAdvice" class="cn.happy.spring06aop_convolution.MyMethodInterceptor"></bean> <!--03.aop--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <property name="interceptorNames" value="methodAdvice"></property> </bean> </beans>
单测
//环绕增强 @Test public void test07(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext03_aop03.xml"); ISomeService service = (ISomeService) ctx.getBean("proxyService"); service.doSome(); }
4.异常增强
接口:ISomeService Mystoo
public interface ISomeService { public void doSome(); }
public interface Mystoo { public void run(); public void run(String style); }
类
public class MystooImpl implements Mystoo { public void run() { } public void run(String style) { } }
public class MyThrowsAdvice implements ThrowsAdvice { public void afterThrowing(Exception ex){ int age=6/0; System.out.println("错误"); } }
public class SomeService implements ISomeService { //核心业务 public void doSome() { System.out.println("拜托别让他一番努力换来是奢求!"); } public void doSecont() { } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象--> <bean id="someService" class="cn.happy.spring07aop_abnormal.SomeService"></bean> <!--02.增强 通知--> <bean id="throwsAdvice" class="cn.happy.spring07aop_abnormal.MyThrowsAdvice"></bean> <!--03.aop --> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <!--做怎么样的增强--> <property name="interceptorNames" value="throwsAdvice"></property> </bean> </beans>
单测
//异常增强 @Test public void test08(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext04_aop04.xml"); ISomeService service = (ISomeService) ctx.getBean("proxyService"); service.doSome(); }
昵称:
退出 订阅评论
[Ctrl+Enter快捷键提交]
【推荐】极光开发者服务平台,五大功能一站集齐
【推荐】阿里云“全民云计算”优惠升级
【推荐】一小时搭建人工智能应用,让技术更容易入门
· 鸡走路时为什么脑袋一抖一抖的?总算明白了
· 马云说新制造要来了,结果中国连造伞都还没自动化
· TensorFlow 1.3.0正式发布,包含诸多更新
· 开放Java EE?甲骨文考虑将Java EE移至开源社区
· GitHub首席执行官正在寻找接替者 将辞去CEO职务
» 更多新闻...
· 写给立志做码农的大学生
· 架构腐化之谜
· 学会思考,而不只是编程
· 编写Shell脚本的最佳实践