1
2 1------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
2
说说那四种增强:前置增强,后置增强,环绕增强,异常增强
那什么是代理工厂bean呢?
**
org.springframework.aop.framework.ProxyFactoryBean**
就是这个东西,他可以实现对方法的增强
@No.1:前置增强:
需要前置增强的类SomeServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12 1package cn.dawn.day11aop01;
2
3
4/**
5 * Created by Dawn on 2018/3/8.
6 */
7public class SomeServiceImpl {
8 public void doSome() {
9 System.out.println("do something");
10 }
11}
12
前置增强的内容的类,可以说实现前置增强接口的类LoggerBefore
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 1package cn.dawn.day11aop01;
2
3
4import org.springframework.aop.MethodBeforeAdvice;
5
6import java.lang.reflect.Method;
7
8/**
9 * Created by Dawn on 2018/3/5.
10 */
11/*前置增强*/
12public class LoggerBefore implements MethodBeforeAdvice {
13 public void before(Method method, Object[] objects, Object o) throws Throwable {
14 System.out.println("日志记录");
15 }
16}
17
配置文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 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:aop="http://www.springframework.org/schema/aop"
5 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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/aop
9 http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
10 <!--要增强的对象-->
11 <bean id="service" class="cn.dawn.day11aop01.SomeServiceImpl"></bean>
12 <!--增强的内容-->
13 <bean id="before" class="cn.dawn.day11aop01.LoggerBefore"></bean>
14 <!--代理工厂bean-->
15 <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
16 <!--要增强的对象-->
17 <property name="target" ref="service"></property>
18 <!--增强的内容-->
19 <property name="interceptorNames" value="before"></property>
20 </bean>
21
22</beans>
23
单测方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 1package cn.dawn.day11aop01;
2
3import org.junit.Test;
4import org.springframework.context.ApplicationContext;
5import org.springframework.context.support.ClassPathXmlApplicationContext;
6
7/**
8 * Created by Dawn on 2018/3/3.
9 */
10public class test20180305 {
11 @Test
12 /*aop代理工厂bean前置增强*/
13 public void t01(){
14 ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day11aop01.xml");
15 SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
16 service.doSome();
17 }
18}
19
@No.2:后置增强:
需要后置增强的类:SomeServiceImpl(此类和前置那个写在不同包下,一会的配置文件也不同,往后都是如此)
1
2
3
4
5
6
7
8
9
10
11
12 1package cn.dawn.day12aop02;
2
3
4/**
5 * Created by Dawn on 2018/3/8.
6 */
7public class SomeServiceImpl {
8 public void doSome() {
9 System.out.println("do something");
10 }
11}
12
后置增强的内容的的类,他与前置增强的那个实现的接口不同:LoggerAfter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 1package cn.dawn.day12aop02;
2
3import org.springframework.aop.AfterReturningAdvice;
4
5import java.lang.reflect.Method;
6
7/**
8 * Created by Dawn on 2018/3/5.
9 */
10/*后置增强*/
11public class LoggerAfter implements AfterReturningAdvice {
12 public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
13 System.out.println("===============after==================");
14 }
15}
16
配置文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 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:aop="http://www.springframework.org/schema/aop"
5 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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/aop
9 http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
10 <!--要增强的对象-->
11 <bean id="service" class="cn.dawn.day12aop02.SomeServiceImpl"></bean>
12 <!--增强的内容-->
13 <bean id="afteradvice" class="cn.dawn.day12aop02.LoggerAfter"></bean>
14 <!--代理工厂bean-->
15 <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
16 <!--要增强的对象-->
17 <property name="target" ref="service"></property>
18 <!--增强的内容-->
19 <property name="interceptorNames" value="afteradvice"></property>
20 </bean>
21
22</beans>
23
单测方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 1package cn.dawn.day12aop02;
2
3 import cn.dawn.day12aop02.SomeServiceImpl;
4 import org.junit.Test;
5 import org.springframework.context.ApplicationContext;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8/**
9 * Created by Dawn on 2018/3/3.
10 */
11public class test20180305 {
12 @Test
13 /*aop代理工厂bean后置增强*/
14 public void t01(){
15 ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day12aop02.xml");
16 SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
17 service.doSome();
18 }
19}
20
@No.3:环绕增强:
需要环绕增强的类:SomeServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12 1package cn.dawn.day13aop03;
2
3
4/**
5 * Created by Dawn on 2018/3/8.
6 */
7public class SomeServiceImpl {
8 public void doSome() {
9 System.out.println("do something");
10 }
11}
12
环绕增强内容的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 1package cn.dawn.day13aop03;
2
3import org.aopalliance.intercept.MethodInterceptor;
4import org.aopalliance.intercept.MethodInvocation;
5
6/**
7 * Created by Dawn on 2018/3/8.
8 */
9/*环绕增强需要实现MethodInterceptor这个接口*/
10public class MethodAdvice implements MethodInterceptor {
11 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
12 System.out.println("前置增强");
13 /*它可以使前置增强和后置增强分开,同时实现前置和后置*/
14 methodInvocation.proceed();
15 System.out.println("后置增强");
16 return null;
17 }
18}
19
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 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:aop="http://www.springframework.org/schema/aop"
5 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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/aop
9 http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
10 <!--要增强的对象-->
11 <bean id="service" class="cn.dawn.day13aop03.SomeServiceImpl"></bean>
12 <!--增强的内容-->
13 <bean id="methodAdvice" class="cn.dawn.day13aop03.MethodAdvice"></bean>
14 <!--代理工厂bean-->
15 <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
16 <!--要增强的对象-->
17 <property name="target" ref="service"></property>
18 <!--增强的内容-->
19 <property name="interceptorNames" value="methodAdvice"></property>
20 </bean>
21
22</beans>
23
单测方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 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:aop="http://www.springframework.org/schema/aop"
5 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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/aop
9 http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
10 <!--要增强的对象-->
11 <bean id="service" class="cn.dawn.day13aop03.SomeServiceImpl"></bean>
12 <!--增强的内容-->
13 <bean id="methodAdvice" class="cn.dawn.day13aop03.MethodAdvice"></bean>
14 <!--代理工厂bean-->
15 <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
16 <!--要增强的对象-->
17 <property name="target" ref="service"></property>
18 <!--增强的内容-->
19 <property name="interceptorNames" value="methodAdvice"></property>
20 </bean>
21
22</beans>
23
@No.4:异常增强:
异常增强是主要用在他出错的时候,进行记录用的,此处模拟起来简单的就是除0异常和空指针异常
写一个类,它里面的方法存在异常,运行时异常
SomeServiceImpl类
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1package cn.dawn.day14aop04;
2
3
4/**
5 * Created by Dawn on 2018/3/8.
6 */
7public class SomeServiceImpl {
8 public void doSome() {
9 System.out.println("do something");
10 String abc=null;
11 System.out.println(abc.toString());;
12 }
13}
14
此处模拟的是空指针异常,当然异常种类很多,不一一模拟了
实现ThrowsAdvice接口的类
1
2
3
4
5
6
7
8
9
10
11
12
13 1package cn.dawn.day14aop04;
2
3import org.springframework.aop.ThrowsAdvice;
4
5/**
6 * Created by Dawn on 2018/3/8.
7 */
8public class MyThrowsAdvice implements ThrowsAdvice {
9 public void afterThrowing(Exception ex){
10 System.out.println("网络中断XXXXX-错误号05289");
11 }
12}
13
他这个接口里面没有方法要重写,很是奇怪,那是随便写什么defgabc的都可以吗?不是的,翻他的源码,他在上面的注释里提供了模板,只有按照他模板写的才能读取到,此处我用了他的其中一个模板方法
配置文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 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:aop="http://www.springframework.org/schema/aop"
5 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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/aop
9 http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
10 <!--要增强的对象-->
11 <bean id="service" class="cn.dawn.day14aop04.SomeServiceImpl"></bean>
12 <!--增强的内容-->
13 <bean id="myThrowsAdvice" class="cn.dawn.day14aop04.MyThrowsAdvice"></bean>
14 <!--代理工厂bean-->
15 <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
16 <!--要增强的对象-->
17 <property name="target" ref="service"></property>
18 <!--增强的内容-->
19 <property name="interceptorNames" value="myThrowsAdvice"></property>
20 </bean>
21
22</beans>
23
单测方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 1package cn.dawn.day14aop04;
2
3import cn.dawn.day14aop04.SomeServiceImpl;
4import org.junit.Test;
5import org.springframework.context.ApplicationContext;
6import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8/**
9 * Created by Dawn on 2018/3/3.
10 */
11public class test20180305 {
12 @Test
13 /*aop代理工厂bean异常增强*/
14 public void t01(){
15 ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day14aop04.xml");
16 SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
17 try{
18 service.doSome();
19 }catch (Exception ex){
20 ex.printStackTrace();
21 }
22
23 }
24}
25
**看到这儿的try-catch了吧,他的作用是让单测能通过单元测试,能变成对勾,能执行到结束,不会因为模拟的异常而中断 **
**————————–End————————-
**
基于代理工厂Bean实现aop的四种增强总结完毕