Spring框架用XML实现AOP
1.首先还是要搭建环境,如果是maven项目直接导入一个spring-mvcweb的包
2.创建通知类(代理)和实体类
3.创建xml文件,导入context和aop的命名空间
4.准备测试类
AOP其实就是一个动态代理的过程,有了spring框架的帮助下,我们可以在不修改源代码的情况下也能够添加一些新的功能。
用xml来使用aop增强方法的原理也并不难,就是把我们通知类里面的增强部分(通知)通过切面放到切入点(实体类要增强的方法)的过程
下面给出具体代码
代码(studentBean.xml)
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="JDKProxy.pojo"/>
<bean id="student" class="JDKProxy.pojo.Student"/>
<bean id="studentproxy" class="JDKProxy.pojo.StudentProxy"/>
<aop:config>
<!-- 设置切入点-->
<aop:pointcut id="p" expression="execution(* JDKProxy.pojo.Student.show(..))"/>
<aop:aspect ref="studentproxy">
<aop:before method="before" pointcut-ref="p"></aop:before>
</aop:aspect>
</aop:config>
<!-- 给学习方法设置好通知-->
<aop:config>
<!-- 设置切入点-->
<aop:pointcut id="study" expression="execution(* JDKProxy.pojo.Student.study(..))"/>
<!-- 切面,把通知设置到切入点去-->
<aop:aspect ref="studentproxy">
<aop:before method="before" pointcut-ref="study"/>
<aop:after method="after" pointcut-ref="study"/>
<aop:after-returning method="afterReturning" pointcut-ref="study"/>
<aop:around method="around" pointcut-ref="study"/>
</aop:aspect>
</aop:config>
<!-- 把通知设置到play-->
<aop:config>
<aop:pointcut id="play" expression="execution(* JDKProxy.pojo.Student.play())"/>
<!-- 方面,而且还要设置好使用哪里的通知-->
<aop:aspect ref="studentproxy">
<aop:before method="before" pointcut-ref="play"/>
<aop:around method="around" pointcut-ref="play"/>
<aop:after method="after" pointcut-ref="play"/>
<aop:after-returning method="afterReturning" pointcut-ref="play"/>
</aop:aspect>
</aop:config>
</beans>
代码(通知类)
package JDKProxy.pojo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class StudentProxy {
public void before(){
System.out.println("befores.....");
}
public void after(){
System.out.println("after");
}
public void afterReturning(){
System.out.println("afterReturning");
}
public void aferThrowing(){
System.out.println("afterThrowing");
}
public void around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕前");
point.proceed();
System.out.println("环绕后");
}
}
代码(实体类)
package JDKProxy.pojo;
import org.springframework.stereotype.Component;
@Component
public class Student {
public void show(){
System.out.println("上课了");
}
//为下面两个方法加上所有的aop强化
public void play(){
System.out.println("我想要玩耍");
}
public void study(){
System.out.println("我想要学习");
}
}
代码(测试类)
@Test
public void test3(){
ApplicationContext context=new ClassPathXmlApplicationContext("studentBean.xml");
Student student = context.getBean("student", Student.class);
student.show();
System.out.println();
student.study();
System.out.println();
student.play();
}