Spring AOP
xml方式
切面类
public class MyAop {
public void Before(){
System.out.println("前置通知");
}
public void After(){
System.out.println("后置通知");
}
}
目标类
@Component
public class Service {
public void say(){
System.out.println("Service 执行");
}
}
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">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.zzm"></context:component-scan>
<bean class="com.zzm.aop.MyAop" id="myAop"></bean>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<aop:config>
<aop:aspect ref="myAop">
<aop:pointcut id="point" expression="execution(* com.zzm.aop.Service.say())"/>
<aop:before method="Before" pointcut-ref="point"></aop:before>
<aop:after method="After" pointcut-ref="point"></aop:after>
</aop:aspect>
</aop:config>
</beans>
测试
public class AopTest {
@Test
public void go(){
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Service service = (Service) classPathXmlApplicationContext.getBean("service");
service.say();
}
}
使用注解方式
切面类
@Aspect
@Component
public class AOP {
@Before(value = "execution(* com.etc.annotation.Service.go())")
public void before(){
System.out.println("前置通知....");
}
@After(value = "execution(* com.etc.annotation.Service.go())")
public void after(){
System.out.println("后置通知....");
}
@Around(value = "execution(* com.etc.annotation.Service.go())")
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕前置通知....");
Object proceed = point.proceed();
System.out.println("环绕后置通知....");
return proceed;
}
}
目标接口和目标类
public interface Service {
public void go();
}
@Component
public class ServiceImpl implements Service{
@Override
public void go() {
System.out.println("服务运行中.....");
}
}
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 ">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.etc"></context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
测试
public class AOPTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
ServiceImpl serviceImpl = (ServiceImpl) applicationContext.getBean("serviceImpl");
serviceImpl.go();
}
}
切面类切入点冗余解决
@Aspect()
@Component
public class AOP {
@Pointcut(value = "execution(* com.etc.annotation.Service.go())")
public void method(){}
@Before(value = "method()")
public void before(){
System.out.println("前置通知....");
}
@After(value = "method()")
public void after(){
System.out.println("后置通知....");
}
@Around(value = "method()")
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕前置通知....");
Object proceed = point.proceed();
System.out.println("环绕后置通知....");
return proceed;
}
}