AOP:Aspect-Oriented programming(面向切面编程)
Spring除了使用本身的AOP实现外,还封装了业界优秀的解决方案AspectJ,因此Spring AOP编程时还需引入AspectJ、以及spring兼容AspectJ的jar包
advice(通知):定义在连接点做什么,为切面增强提供织入接口。主要描述Spring Aop围绕方法调用而诸如的切面行为。
主要有:beforeAdvice、afterAdvice、afterReturn、throwsAdvice
pointCut(切点):决定Advice通知应该作用于哪个连接点,也就是说通过pointCut切点来定义需要增强的方法结合。
jiontPoint(连接点):执行过程中的某一行为。
目标对象:被一个切面或躲过去切面所通知的对象。
Spring除了使用本身的AOP实现外,还封装了业界优秀的解决方案AspectJ,因此Spring AOP编程时还需引入AspectJ、以及spring兼容AspectJ的jar包
advice(通知):定义在连接点做什么,为切面增强提供织入接口。主要描述Spring Aop围绕方法调用而诸如的切面行为。
主要有:beforeAdvice、afterAdvice、afterReturn、throwsAdvice
pointCut(切点):决定Advice通知应该作用于哪个连接点,也就是说通过pointCut切点来定义需要增强的方法结合。
jiontPoint(连接点):执行过程中的某一行为。
目标对象:被一个切面或躲过去切面所通知的对象。
第一种方式(注解):
IPersonDao:
package com.aop1;
public interface IPersonDao {
void work();
}
Person:
package com.aop1;
public class Person implements IPersonDao{
@Override
public void work() {
System.out.println("go to work......");
}
}
Things(切面):
package com.aop1;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Things {
@Pointcut("execution(* com.aop1.IPersonDao.*(..))")
public void method(){
System.out.println("aspect");
}
@Before("method()")
public void wear(){
System.out.println("wear clothes......");
}
@After("method()")
public void working(){
System.out.println("working......");
}
@Around("method()")
public void around(){
System.out.println("around");
}
@AfterReturning("method()")
public void afterReturning(){
System.out.println("afterReturning");
}
@AfterThrowing(pointcut="method()", throwing="ex")
public void afterThrowing(JoinPoint joinPoint,Exception ex){
System.out.println("afterThrowing"+joinPoint+ex.getMessage());
}
}
aopBean(配置文件):
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="person" class="com.aop1.Person"/>
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.aop1"/>
<!-- 激活自动代理 -->
<aop:aspectj-autoproxy/>
</beans>
Test(测试类):
package com.aop1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("aopBean.xml");
IPersonDao person = (IPersonDao)context.getBean("person");
person.work();
}
}
第二种方式:
IpersonDao.java、Person.java与方法一相同不发生任何改变,切面Thing.java、配置文件有变化。
Thing.java:
package com.aop;
public class Things {
public void wear(){
System.out.println("wear clothes......");
}
public void working(){
System.out.println("working......");
}
}
aopBean.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.1.xsd">
<!--
1.文档解析格式:xmlns
2.aop语义规范
xmlns:aop="http://www.springframework.org/schema/aop"
3.事务相关:xmlns:tx
4.添加:
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
否则错误信息提示: 通配符的匹配很全面, 但无法找到元素 'aop:config' 的声明
-->
<bean id="person" class="com.aop.Person"/>
<bean id="things" class="com.aop.Things"/>
<aop:config>
<aop:aspect id="thing" ref="things">
<!-- execution(返回值 接口路径.方法名(参数)) -->
<aop:pointcut id="addMethod" expression="execution(* com.aop.IPersonDao.*(..))"/>
<aop:before method="wear" pointcut-ref="addMethod"/>
<aop:after method="working" pointcut-ref="addMethod"/>
</aop:aspect>
</aop:config>
</beans>