面向切面编程(Aspect Oriented Programming ,AOP)是软件编程思想发展到一定阶段的产物,是对面向对象编程的有益补充,AOP一般适用于具有横切逻辑的场合,如访问控制、事务管理、性能监测等。
面向切面编程,简单的说就是在不改变源程序的基础上为代码段增加新的功能,对代码段进行增强处理。
基本概念:
# 增强处理(Advice)类型:在源对象的fun()方法之前插入的增强处理为前置增强,该方法正常执行完以后插入的增强处理为后置增强,此外还有环绕增强、异常抛出增
强最终增强等类型。
#切入点:可以插入增强处理的方法,方法fun()就是一个切入点。
在这里应用:Advice增强处理类型如下
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<bean id="printer" class="cn.jbit.print.Printer">
<property name="inkbox" ref="gray"></property>
<property name="paper" ref="a4"></property>
</bean>
<bean id="a4" class="cn.jbit.dao.impl.A4Paper"></bean>
<bean id="gray" class="cn.jbit.dao.impl.GrayInkbox"></bean>
<!-- 注入前置增强类 -->
<bean id="before" class="cn.jbit.util.LoggerBefore"></bean>
<!-- 注入后置增强类 -->
<bean id="afterLogger" class="cn.jbit.util.AfterLogger"></bean>
<aop:config>
<aop:pointcut expression="execution(public void print())" id="pt"/>
<aop:advisor advice-ref="afterLogger" pointcut-ref="pt"/><!-- 引入后置增强 -->
<aop:advisor advice-ref="before" pointcut-ref="pt"/><!-- 引入前置增强 -->
</aop:config>
</beans>
两个dao接口:
public interface Inkbox {
public String getColor();
}
public interface Paper {
public String getSize();
}
两个dao实现类:
public class A4Paper implements Paper {
public String getSize() {
return "A4";
}
}
public class GrayInkbox implements Inkbox {
public String getColor() {
return "灰色";
}
}
打印类:
public class Printer {
private Inkbox inkbox;
private Paper paper;
public void print(){
System.out.println("正在使用"+inkbox.getColor()+"的颜色的墨盒在"+paper.getSize()+"的纸上打印。。。");
}
public Inkbox getInkbox() {
return inkbox;
}
public void setInkbox(Inkbox inkbox) {
this.inkbox = inkbox;
}
public Paper getPaper() {
return paper;
}
public void setPaper(Paper paper) {
this.paper = paper;
}
}
前置增强类:
public class LoggerBefore implements MethodBeforeAdvice {
public static final Logger log=Logger.getLogger(LoggerBefore.class);
/**
* 执行了前置增强before()方法
*/
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
log.debug("执行了前置增强before()方法");
}
}
后置增强类:
public class AfterLogger implements AfterReturningAdvice {
/**
* 后置增强
*/
public static Logger logger=Logger.getLogger(AfterLogger.class);
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
//后置增强 执行了afterReturning()方法
logger.debug("执行了afterReturning()方法");
}
}
测试类:
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Printer printer = (Printer) ac.getBean("printer");
printer.print();
}
}
运行结果:
2015-05-30 22:00:37 Finished creating instance of bean 'printer'
2015-05-30 22:00:37 Returning cached instance of singleton bean 'a4'
2015-05-30 22:00:37 Returning cached instance of singleton bean 'gray'
2015-05-30 22:00:37 Returning cached instance of singleton bean 'before'
2015-05-30 22:00:37 Returning cached instance of singleton bean 'afterLogger'
2015-05-30 22:00:37 Returning cached instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator'
2015-05-30 22:00:37 Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
2015-05-30 22:00:37 Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#1'
2015-05-30 22:00:37 Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@35ecfe07]
2015-05-30 22:00:37 Returning cached instance of singleton bean 'lifecycleProcessor'
2015-05-30 22:00:37 Returning cached instance of singleton bean 'printer'
2015-05-30 22:00:37 执行了前置增强before()方法
正在使用灰色的颜色的墨盒在A4的纸上打印。。。
2015-05-30 22:00:37 执行了afterReturning()方法