Spring面向切面编程AOP讲解
2017-04-25 10:26
561 查看
提及Spring,就是IoC和 AOP及一些事物。我之前使用spring都是拿来做控制对象的,并没有使用到aop,所以写这个博文,也是为了记录自己把对于aop的理解和学习记录一下,和大家分享。***
这个是我自己话的图,说明下切面编程。内容很简单,就是要执行一个方法A时,AOP进行控制,如果要执行的这个方法有前置方法,那么先执行,也就是B。如果有后置方法,那么执行完A后,还会执行C。这些都是自动执行的,只需要在xml文件中进行配置。那么接下来我用源码实例讲解下。
首先引入所需的jar包 <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.1</version>
</dependency>
我们现在建立一个A类,里面有个a方法public class A {
void a(){
System.out.println("这是要执行的A方法");
}
}
在建立一个B类,b方法,用于在a方法前调用public class B {
void b(){
System.out.println("这是B方法");
}
}
然后是C类,c方法,用于a方法后调用 public class C {
void c(){
System.out.println("这是c方法");
}
}
这时候该去写application.xml这个文件了,首先建立一个.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>
<bean id="aaa" class="aop.A"/>
<bean id="bbb" class="aop.B" />
<bean id="ccc" class="aop.C"/>
<aop:config>
<aop:aspect ref="bbb">
<aop:pointcut id="b" expression="execution(* aop.A.a(..))" />
<aop:before pointcut-ref="b" method="b" />
</aop:aspect>
<aop:aspect ref="ccc">
<aop:pointcut id="c" expression="execution(* aop.A.a(..))" />
<aop:after pointcut-ref="c" method="c" />
</aop:aspect>
</aop:config>
</beans>
这个xml里的内容非常简单,定义三个类,然后用写明在 A.a方法运行的时候,去执行之前的befor bbb类的b方法。execution(* aop.A.a(..))是一个AspectJ切点表达式,execution表示在执行时触发,后面的*表示任意类型返回值, a(..)括号里面的 ..表示任意参数。
那么现在再写一个Run类,来跑下我们写的这个切面demo。代码如下public class Run {
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("aop/application.xml");
A a =(A) context.getBean("aaa");
a.a();
}
}
我这是执行成功了,你们可以看到,使用了IoC直接从spring中取得A类对象,然后执行a方法,再根据切面,去寻找之前执行和之后执行的B,C两个类。
通知环绕
那么通知环绕其实和上面所说的差不多,就是将前后两个方法,集成到一起,在xml中展现,我们可以不修改类,直接修改xml文件,并把B,C两个类整合成一个,中间使用 ProceedingJoinPoint类的proceed()方法,这个就是去调用A类的。便可以实现,代码如下: public void test(ProceedingJoinPoint pj) throws Throwable{
System.out.println("这是b方法");
pj.proceed();//调用a方法
System.out.println("这是c方法");
}
application.xml文件修改为: <aop:aspect ref="bbb">
<aop:pointcut id="b" expression="execution(* aop.A.a(..))" />
<!-- <aop:before pointcut-ref="b" method="b" />-->
<aop:around pointcut-ref="b" method="test"/>
</aop:aspect>
显示的结果,和上面的结果是相同的,我就不粘贴出来了。
那么Spring的AOP面向切面编程,就介绍完了~
这个是我自己话的图,说明下切面编程。内容很简单,就是要执行一个方法A时,AOP进行控制,如果要执行的这个方法有前置方法,那么先执行,也就是B。如果有后置方法,那么执行完A后,还会执行C。这些都是自动执行的,只需要在xml文件中进行配置。那么接下来我用源码实例讲解下。
首先引入所需的jar包 <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.1</version>
</dependency>
我们现在建立一个A类,里面有个a方法public class A {
void a(){
System.out.println("这是要执行的A方法");
}
}
在建立一个B类,b方法,用于在a方法前调用public class B {
void b(){
System.out.println("这是B方法");
}
}
然后是C类,c方法,用于a方法后调用 public class C {
void c(){
System.out.println("这是c方法");
}
}
这时候该去写application.xml这个文件了,首先建立一个.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>
<bean id="aaa" class="aop.A"/>
<bean id="bbb" class="aop.B" />
<bean id="ccc" class="aop.C"/>
<aop:config>
<aop:aspect ref="bbb">
<aop:pointcut id="b" expression="execution(* aop.A.a(..))" />
<aop:before pointcut-ref="b" method="b" />
</aop:aspect>
<aop:aspect ref="ccc">
<aop:pointcut id="c" expression="execution(* aop.A.a(..))" />
<aop:after pointcut-ref="c" method="c" />
</aop:aspect>
</aop:config>
</beans>
这个xml里的内容非常简单,定义三个类,然后用写明在 A.a方法运行的时候,去执行之前的befor bbb类的b方法。execution(* aop.A.a(..))是一个AspectJ切点表达式,execution表示在执行时触发,后面的*表示任意类型返回值, a(..)括号里面的 ..表示任意参数。
那么现在再写一个Run类,来跑下我们写的这个切面demo。代码如下public class Run {
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("aop/application.xml");
A a =(A) context.getBean("aaa");
a.a();
}
}
我这是执行成功了,你们可以看到,使用了IoC直接从spring中取得A类对象,然后执行a方法,再根据切面,去寻找之前执行和之后执行的B,C两个类。
通知环绕
那么通知环绕其实和上面所说的差不多,就是将前后两个方法,集成到一起,在xml中展现,我们可以不修改类,直接修改xml文件,并把B,C两个类整合成一个,中间使用 ProceedingJoinPoint类的proceed()方法,这个就是去调用A类的。便可以实现,代码如下: public void test(ProceedingJoinPoint pj) throws Throwable{
System.out.println("这是b方法");
pj.proceed();//调用a方法
System.out.println("这是c方法");
}
application.xml文件修改为: <aop:aspect ref="bbb">
<aop:pointcut id="b" expression="execution(* aop.A.a(..))" />
<!-- <aop:before pointcut-ref="b" method="b" />-->
<aop:around pointcut-ref="b" method="test"/>
</aop:aspect>
显示的结果,和上面的结果是相同的,我就不粘贴出来了。
那么Spring的AOP面向切面编程,就介绍完了~
相关文章推荐
- idea 实现Spring讲解(Ioc-控制反转)/Aop(面向切面的编程)
- 解释通知Spring AOP 面向切面编程
- Spring 面向切面编程AOP
- 黑马程序员--Spring Aop 面向切面编程,实现前置通知
- Spring之面向切面编程AOP(二)
- 使用Spring进行面向切面(AOP)编程
- 使用Spring进行面向切面(AOP)编程
- spring aop 面向切面编程 如何来做一个强大的日志记录功能
- Spring笔记——使用Spring进行面向切面(AOP)编程
- 从头认识Spring-1.2 什么是AOP?为什么需要面向切面编程?
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- 【spring-boot】spring aop 面向切面编程初接触
- Spring AOP 面向切面编程相关注解
- spring AOP面向切面编程的四种实现方式
- Spring in Action 入门之面向切面编程AOP
- spring心得7--spring第二大特点AOP(面向切面)讲解
- Spring in Action 入门之面向切面编程AOP
- spring心得7--spring第二大特点AOP(面向切面)讲解
- Spring面向切面编程AOP的个人理解
- Spring学习笔记(三)--面向切面编程AoP