6.自动代理aop
自动代理能够让切面的切点定义来决定哪个Bean需要代理,不需要为特定的bean创建代理了。共2种自动代理,spring提供的和aspectJ提供的。
为了
使用spring提供的自动代理,要在xml中声明
<!--spring声明的自动代理,它会被自动识别-->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
这样声明,配置文件就不用声明 audienceProxyBase了,也不需要把目标bean改名字叫做xxxxTarget了。方便了许多。Xml配置文件也少了许多。
然而
aspectJ提供可一种基于jdk1.5注解技术的方式,使得配置文件更少,更方便
首先,直接把Audience类改造成切面。首先要有aspectjweaver 这个jar包,具体代码如下,全部用了注解来声明切面中各个元素,通俗易懂。
@Aspect
public class Audience {
public Audience() {
}
@Pointcut("execution(* *.perform(..))")
public void performance(){}
@Before("performance()")
public void takeSeat(){
System.out.println("观众们找到自己的座位,都坐下来了");
}
@Before("performance()")
public void turnOffMobilePhone(){
System.out.println("请所有观众确定手机已经关闭");
}
@AfterReturning("performance()")
public void appluad(){
System.out.println("观众们大声鼓掌,啪啦啪啦啪啦");
}
@AfterThrowing
public void demandRefund(){
System.out.println("演的太差了,我们要退钱!");
}
}
其中
@Pointcut("execution(* *.perform(..))")
public void performance(){}
performance方法没有实际意义,它只是为了让切点注解
@Pointcut("execution(* *.perform(..))")
附在它身上而已,注解必须要附在字段或者方法上,才有意义。
现在只要在xml中声明
<bean id="audience" class="com.spring.springcase.Audience"/>
这个声明, audience已经就是切面了,再也不用在xml里定义什么切点和通知了,再次大大简化。
现在要做的是,声明一个aspectJ提供的自动代理bean,让他能自动把切面用到方法上。
我们用spring的aop空间的提供的一个自定义配置元素。
现在xml头部声明就变成了
<?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-2.0.xsd">
现在所有的xml加起来,是这样的了:
<bean id="showBoy" class="com.spring.springcase.ShowBoy"/>
<bean id="showGirl" class="com.spring.springcase.ShowGirl"/>
<bean id="audience" class="com.spring.springcase.Audience"/>
<aop:aspectj-autoproxy />
这样确实是简化了许多。
要说明一点:这里只是用@AspectJ的注解作为指导来创建切面,其实质仍人是Spring风格的切面,也就是说,虽然我们用@AspectJ很拉风的写出了切面,但是仍然只能用代理方法调用
spring自己也意识到,用代理工厂bean,那样代码不好看,所以它提供了另外一种方式,
定义纯粹的pojo切面
它可以把任何类都转换成为切面,这个类没有接口,没有注解,都可以。而且也不用改变原类,全部在配置文件中定义。
现在的 Audience又变回原来那个普通的bean了。我们要把这个普通的bean,配置成切面
public class Audience {
public Audience() {
}
public void takeSeat(){
System.out.println("观众们找到自己的座位,都坐下来了");
}
public void turnOffMobilePhone(){
System.out.println("请所有观众确定手机已经关闭");
}
public void appluad(){
System.out.println("观众们大声鼓掌,啪啦啪啦啪啦");
}
public void demandRefund(){
System.out.println("演的太差了,我们要退钱!");
}
}
<bean id="audience" class="com.spring.springcase.Audience"/>
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(* *.perform(..))"/>
<aop:before method="takeSeat" pointcut-ref="performance"/>
<aop:before method="turnOffMobilePhone" pointcut-ref="performance"/>
<aop:after-returning method="appluad" pointcut-ref="performance"/>
<aop:after-throwing method="takeSeat" pointcut-ref="performance"/>
</aop:aspect>
</aop:config>
这样就可以了
运行一下测试
public class performTest extends TestCase{
ApplicationContext ctx;
@Override
protected void setUp() throws Exception {
ctx = new ClassPathXmlApplicationContext("spring-springcase.xml");
}
public void testShowBoy(){
Perform perform = (Perform)ctx.getBean("showBoy");
perform.perform();
}
public void testShowGirl(){
Perform perform = (Perform)ctx.getBean("showGirl");
perform.perform();
}
}
打印出
观众们找到自己的座位,都坐下来了
请所有观众确定手机已经关闭
表演街舞
观众们大声鼓掌,啪啦啪啦啪啦
分享到:
相关推荐
Spring_AOP笔记Spring_AOP笔记Spring_AOP笔记Spring_AOP笔记Spring_AOP笔记Spring_AOP笔记Spring_AOP笔记
spring之AOP(动态代理),包括jdk动态代理和CGLib动态代理
NULL 博文链接:https://microjava.iteye.com/blog/525796
主要对Spring AOP的相关概念和简单的静态代理、动态代理以及常见的几种AOP配置方式做总结学习。主要包括:1. AOP的常见概念 2. 静态代理 3. jdk动态代理 4. Aspectj and Aspectjweaver 5. **aop-config** 6. CGLIB ...
死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理
Spring Aop 学习笔记
When to use Spring AOP and AspectJ AOP? Expert author Ramnivas Laddad shows how to combine technologies such as Spring, Hibernate, Swing, and JDBC with AspectJ. The book fully covers the latest ...
自己学习spring课程的笔记。笔记都是根据尚硅谷的课程(spring ioc,spring aop,spring mvc,spring boot等)写的。 主要内容:spring ioc,spring aop,spring mvc,spring boot
JavaEE spring半自动实现AOP代理
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
NULL 博文链接:https://linres.iteye.com/blog/281221
SpringAOP学习笔记以及四个可运行的Demo,涵盖经典代理模式、基于注解、基于xml配置这3方面的Demo
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
《Spring技术内幕》学习笔记3——IoC容器载入Bean定义资源文件 《Spring技术内幕》学习笔记4——IoC容器解析Bean定义资源并注册解析后的Bean 《Spring技术内幕》学习笔记5——IoC容器的依赖注入 《Spring技术内幕...
spring-springMVC开发文档和AOP详解,便于学习运用框架知识
介绍了springAop默认代理方式
JavaEE spring自动实现AOP代理
SpringAOP动态代理 Spring AOP 使用的动态代理主要有两种方式:JDK 动态代理和 CGLIB 代理。 JDK 动态代理:用于代理实现了接口的类。Spring 会使用 java.lang.reflect.Proxy 类来创建代理对象。 CGLIB 代理:用于...