AOP?
基本概念
切面(aspect):横切关注点被模块化的特殊对象。
通知(advice):切面必须要完成的工作。切面中的每个方向称之为通知。通知是在切面对象中的。
目标(target):被通知的对象。
代理(proxy):向目标对象应用通知后创建的对象。
连接点(joinpoint):目标对象的程序执行的某个特定位置。如某个方法调用前,调用后的位置。包括两个信息:1.目标程序的哪个方法?2.方法执行
前还是执行后?
切点(pointcut):每个类会有多个连接点,AOP通过切点定位到特定的边接点。
类比,连接点相当于数所成方圆 中的记录,而切点相当于查询条件。一个切点匹配多个连接点。
使用XML配置的方法:
1.导入 spring包和aspectj的两个jar包
2.目标对象(要切入的对象)
首先为了不违反开闭原则和更好的可扩展性,目标对象实际上是实现了已定义好的某个接口
接口:
package com.itnba.test;
public interface IHuman {
public void chifan();
public void shuijiao();
}
实现接口的两个类:
package com.itnba.test;
import org.springframework.stereotype.Component;
public class Chinese implements IHuman {
@Override
public void chifan() {
// TODO 自动生成的方法存根
System.out.println("中国人吃饭");
}
@Override
public void shuijiao() {
// TODO 自动生成的方法存根
System.out.println("中国人睡觉");
}
}
package com.itnba.test;
import org.springframework.stereotype.Component;
public class American implements IHuman {
@Override
public void chifan() {
// TODO 自动生成的方法存根
System.out.println("美国人吃饭");
}
@Override
public void shuijiao() {
// TODO 自动生成的方法存根
System.out.println("美国人睡觉");
}
}
3.定义一个切面类
package com.itnba.test;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
public class Qiemian {
public void chifanqian(){
System.out.println("洗手");
}
public void chifanhou(){
System.out.println("漱口");
}
public void shuijiaoqian(){
System.out.println("洗澡");
}
}
4.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"
default-autowire="byName"
>
<!-- 首相要实例化目标对象类和切面类 -->
<bean id="chinese" class="com.itnba.test.Chinese"></bean>
<bean id="american" class="com.itnba.test.American"></bean>
<bean id="qiemian" class="com.itnba.test.Qiemian"></bean>
<aop:config>
<!-- 要切入的对象 -->
<aop:pointcut expression="execution(* com.itnba.test.*.chifan(..))" id="chifan"/>
<aop:pointcut expression="execution(* com.itnba.test.*.shijiao(..))" id="shijiao"/>
<!-- 切入点 -->
<aop:aspect id="ha" ref="qiemian"><!-- 切面类 -->
<!-- <aop:之前before、之后after... method="切面类中的方法" pointcut-ref="上面的切入的对象"/> -->
<aop:before method="chifanqian()" pointcut-ref="chifan"/><!-- 之前通知 -->
<aop:after method="chifanhou()()" pointcut-ref="chifan"/><!-- 之后通知 -->
<aop:before method="shuijiaoqian()" pointcut-ref="shijiao"/>
</aop:aspect>
</aop:config>
</beans>
5.运行
package com.itnba.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//要用接口接收
IHuman c= (IHuman)context.getBean("chinese");
c.chifan();
c.shuijiao();
System.out.println("*********************************");
//要用接口接收
IHuman a= (IHuman) context.getBean("american");
a.chifan();
a.shuijiao();
}
}