
我们现在建立一个A类,里面有个a方法<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>
在建立一个B类,b方法,用于在a方法前调用public class A {
void a(){
System.out.println("这是要执行的A方法");
}
}
然后是C类,c方法,用于a方法后调用public class B {
void b(){
System.out.println("这是B方法");
}
}
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面向切面编程,就介绍完了~