spring-AOP
2019-12-09 本文已影响0人
李霖神谷
普通的oop工程,方法都是纵向的依次执行的,而aop是当方法执行的时候,执行前后添加额外的一些功能,这种纵向的执行流程称之为面向切面编程(aop)。
这里介绍几个概念:
切点:pointcut,指的是原有的执行方法
前置通知:在切点之前进行的操作befor advice
后置通知:在切点之后进行的操作after advice
切点执行过程中出现异常会触发异常通知throws advice
所有的这些功能叫做切面
织入:把切面织入到原有的功能上叫做织入
代码实现,当一个方法被调用的时候,执行前后分别由林外的操作
前置通知,后置通知类:
package com.shuai.advice;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterAdvice implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("我先得洗澡,擦润肤乳");
}
}
package com.shuai.advice;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("我要先玩一局王者");
}
}
原有的功能类:
package com.shuai.domain;
public class people {
public void study(){
System.out.println("我要学习了");
}
public void sleep(){
System.out.println("我要睡觉了");
}
}
xml配置:execution(* com.shuai.domain.people.study())星后要有空格
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置切点-->
<aop:config>
<aop:pointcut id="peopleCut" expression="execution(* com.shuai.domain.people.study())" ></aop:pointcut>
<aop:advisor advice-ref="beforAdvice" pointcut-ref="peopleCut"></aop:advisor>
<aop:advisor advice-ref="afterAdvice" pointcut-ref="peopleCut"></aop:advisor>
</aop:config>
<!--指定前置通知-->
<bean id="beforAdvice" class="com.shuai.advice.BeforAdvice"></bean>
<!--指定后置通知-->
<bean id="afterAdvice" class="com.shuai.advice.AfterAdvice"></bean>
<bean id="people" class="com.shuai.domain.people"></bean>
</beans>
测试:
import com.shuai.domain.people;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
people p= applicationContext.getBean("people", people.class);
p.study();
p.sleep();
}
}
异常通知:
<aop:config>
<aop:aspect ref="advice">
<aop:pointcut id="peopleCut" expression="execution(* com.shuai.domain.people.study())"></aop:pointcut>
<aop:after-throwing method="mytestException" pointcut-ref="peopleCut"></aop:after-throwing>
</aop:aspect>
</aop:config>
<bean id="people" class="com.shuai.domain.people"></bean>
<bean id="advice" class="com.shuai.advice.ExceptionAdvice"></bean>
注解配置aop
package com.shuai.domain;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
public class people {
@Pointcut("execution(* com.shuai.domain.people.study())")
public void study(){
System.out.println("我要学习了");
}
public void sleep(){
System.out.println("我要睡觉了");
}
}
package com.shuai.advice;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class BeforAdvice {
@Before("com.shuai.domain.people.study()")
public void before() {
System.out.println("我要先玩一局王者");
}
}
xml配置:
<context:component-scan base-package="com.shuai.domain,com.shuai.advice"></context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
people p= applicationContext.getBean("people", people.class);
p.study();