AOP(Aspect Oriented Programming)就是面向切面编程,通过预编译方式和运行期动态代理实现程序功能的横向多模块统一控制的一种技术。AOP是OOP的补充,是Spring框架中的一个重要内容。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。AOP可以分为静态织入与动态织入,静态织入即在编译前将需织入内容写入目标模块中,这样成本非常高。动态织入则不需要改变目标模块。Spring框架实现了AOP,使用注解配置完成AOP比使用XML配置要更加方便与直观。
1.基于xml配置的spring aop
第一次做 简单一点
首先创建一个包 aop
创建接口
package aop;
public interface count{
public int jiafa(int a,int b);
public int jianfa(int a,int b);
}
接口实现类
package aop;
public class c1 implements count {
private void sysoo() {
System.out.println("构造器");
}
public int jiafa(int a, int b) {
return a+b;
}
public int jianfa(int a, int b) {
return a-b;
}
}
切面类
package aop;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
public class c3 {
//切面类
public void qierudian(){}
public void before(JoinPoint joinpoint){
//得到方法的名字
String name=joinpoint.getSignature().getName();
//得到方法的参数
List<Object> list=Arrays.asList(joinpoint.getArgs());
System.out.println("before"+name+list);
}
//后置通知
public void after(JoinPoint joinpoint){
//得到方法的名字
String name=joinpoint.getSignature().getName();
//得到方法的参数
List<Object> list=Arrays.asList(joinpoint.getArgs());
System.out.println("after"+name+list);
}
//返回通知
public void fanhui(JoinPoint joinpoint){
String name=joinpoint.getSignature().getName();
System.out.println("得到方法的返回值"+name);
}
//异常通知
public void yicchang(JoinPoint joinPoint,Exception ex){
String name = joinPoint.getSignature().getName();
System.out.println("The yichang "+name+" ends with "+ ex.toString());
}
}
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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="pojo"></context:component-scan>
<context:component-scan base-package="test"></context:component-scan>
<context:component-scan base-package="aop"></context:component-scan>
<bean id="c1" class="aop.c1"></bean>
<!--配置切面类的bean-->
<bean id="c3" class="aop.c3"></bean>
<!--配置aop-->
<aop:config>
<!--配置切点表达式-->
<aop:pointcut id="pointcut" expression="execution(* aop.count.*(..))" />
<aop:aspect ref="c3" order="1">
<!--配置具体通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
测试类
package aop;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class tess{
@Test
public void testa()throws Exception{
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
count c1=(count) context.getBean("c1");
int result=c1.jiafa(10, 3);
System.out.println(result);
}}
结束啦
结果:
beforejiafa[10, 3]
afterjiafa[10, 3]
13