(14)Spring AOP Advisor
2017-12-09 20:00
399 查看
只能有一个通知,但是感觉很强大
被拦截类的接口
package shuai.spring.study.service;
public interface IHelloService {
public void sayHello();
public String returnHello(String param0, String param1);
}
被拦截类
package shuai.spring.study.service.impl;
import shuai.spring.study.Info;
import shuai.spring.study.service.IHelloService;
public class HelloServiceImpl implements IHelloService {
@Override
public void sayHello() {
System.out.println("============Hello World!");
}
@Override
public String returnHello(String param0, String param1) {
System.out.println("返回前输出,两个参数:" + param0 + "和" + param1);
return Info.info(param0, param1);
}
}
Info类
package shuai.spring.study;
public class Info {
public static String info() {
System.out.println("返回时输出:hello world 1");
return "返回值:hello world 1";
}
public static String info(String param0, String param1) {
System.out.println("返回时输出:" + param0 + "," + param1);
return "返回值为:" + param0 + "," + param1;
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="iHelloService" class="shuai.spring.study.service.impl.HelloServiceImpl"/>
<bean id="helloAdvisorBefore" class="shuai.spring.study.aop.HelloAdvisorBefore"/>
<!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
<aop:config>
<aop:pointcut expression="execution(* shuai.spring.study.service..*.*(..))" id="pointcut"/>
<aop:advisor
advice-ref="helloAdvisorBefore"
pointcut-ref="pointcut"/>
</aop:config>
</beans>
注意:advice-ref引用的bean必须要实现某个通知的接口,比如这里的HelloAdvisorBefore要实现前置通知的接口MethodBeforeAdvice,就是一个通知类,目标方法被拦截了会到这里执行。
定义HelloAdvisorBefore类
package shuai.spring.study.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class HelloAdvisorBefore implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("所在方法的名字为:" + arg0.getName());
for (int i = 0; i < arg1.length; i++) {
System.out.println("第" + (i + 1) + "个参数的值为:" + arg1[i]);
}
System.out.println("所在类的toString方法为:" + arg2);
}
}
测试类
package shuai.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import shuai.spring.study.service.IHelloService;
public class HelloTest {
@Test
public void testHelloWorld() {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorld.xml");
IHelloService iHelloService = context.getBean("iHelloService", IHelloService.class);
iHelloService.returnHello("hello", "world");
}
}
测试结果:
所在方法的名字为:returnHello
第1个参数的值为:hello
第2个参数的值为:world
所在类的toString方法为:shuai.spring.study.service.impl.HelloServiceImpl@7ff95560
返回前输出,两个参数:hello和world
返回时输出:hello,world
被拦截类的接口
package shuai.spring.study.service;
public interface IHelloService {
public void sayHello();
public String returnHello(String param0, String param1);
}
被拦截类
package shuai.spring.study.service.impl;
import shuai.spring.study.Info;
import shuai.spring.study.service.IHelloService;
public class HelloServiceImpl implements IHelloService {
@Override
public void sayHello() {
System.out.println("============Hello World!");
}
@Override
public String returnHello(String param0, String param1) {
System.out.println("返回前输出,两个参数:" + param0 + "和" + param1);
return Info.info(param0, param1);
}
}
Info类
package shuai.spring.study;
public class Info {
public static String info() {
System.out.println("返回时输出:hello world 1");
return "返回值:hello world 1";
}
public static String info(String param0, String param1) {
System.out.println("返回时输出:" + param0 + "," + param1);
return "返回值为:" + param0 + "," + param1;
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="iHelloService" class="shuai.spring.study.service.impl.HelloServiceImpl"/>
<bean id="helloAdvisorBefore" class="shuai.spring.study.aop.HelloAdvisorBefore"/>
<!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
<aop:config>
<aop:pointcut expression="execution(* shuai.spring.study.service..*.*(..))" id="pointcut"/>
<aop:advisor
advice-ref="helloAdvisorBefore"
pointcut-ref="pointcut"/>
</aop:config>
</beans>
注意:advice-ref引用的bean必须要实现某个通知的接口,比如这里的HelloAdvisorBefore要实现前置通知的接口MethodBeforeAdvice,就是一个通知类,目标方法被拦截了会到这里执行。
定义HelloAdvisorBefore类
package shuai.spring.study.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class HelloAdvisorBefore implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("所在方法的名字为:" + arg0.getName());
for (int i = 0; i < arg1.length; i++) {
System.out.println("第" + (i + 1) + "个参数的值为:" + arg1[i]);
}
System.out.println("所在类的toString方法为:" + arg2);
}
}
测试类
package shuai.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import shuai.spring.study.service.IHelloService;
public class HelloTest {
@Test
public void testHelloWorld() {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorld.xml");
IHelloService iHelloService = context.getBean("iHelloService", IHelloService.class);
iHelloService.returnHello("hello", "world");
}
}
测试结果:
所在方法的名字为:returnHello
第1个参数的值为:hello
第2个参数的值为:world
所在类的toString方法为:shuai.spring.study.service.impl.HelloServiceImpl@7ff95560
返回前输出,两个参数:hello和world
返回时输出:hello,world
相关文章推荐
- Spring Aop之Advisor解析
- Spring AOP实例(Pointcut,Advisor)
- Spring3系列10-Spring AOP——Pointcut,Advisor拦截指定方法
- spring aop方式配置事务中的三个概念 pointcut advice advisor
- Spring AOP Example – Pointcut , Advisor
- Spring学习(十六)----- Spring AOP实例(Pointcut(切点),Advisor)
- Spring AOP AspectJExpressionPointcutAdvisor
- 学习AOP之透过Spring的Ioc理解Advisor
- 17、spring AOP通知——Pointcut、Advisor
- Spring3系列: Spring AOP——Pointcut,Advisor
- Spring 使用 DefaultAdvisorAutoProxyCreator创建AOP 小结
- Spring源码-AOP(六)-自动代理与DefaultAdvisorAutoProxyCreator
- Spring AOP(三)-AOP的Advisor
- 死磕Spring AOP系列3:剖析Bean处理器之DefaultAdvisorAutoProxyCreator
- JavaWeb学习笔记-spring-14-AOP-创建切面(静态正则表达式方法匹配切面)
- Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法
- 编程式实现Spring AOP 引入(Introduction)和切面(Advisor)
- Spring3- Spring AOP——Pointcut,Advisor
- 学习AOP之透过Spring的Ioc理解Advisor
- Spring源码分析----AOP概念(Advice,Pointcut,Advisor)和AOP的设计与实现