定义将会进行切面增强的类
/**
* @author 周宁
* @Date 2019-07-20 13:48
*/
public class AspectBean {
public void aspectTest(){
System.out.println("我会被当成切面处理了");
}
}
1.使用@Aspect的方式
定义切面类,定义切入点,增强逻辑
/**
* @author 周宁
* @Date 2019-07-20 13:50
*/
@Aspect
public class AspectJTest {
@Pointcut("execution(* *.aspectTest(..))")
public void p1(){
}
@Before("p1()")
public void before(){
System.out.println("before");
}
@After("p1()")
public void after(){
System.out.println("after");
}
@Around("p1()")
public Object arroundTest(ProceedingJoinPoint p) throws Throwable {
System.out.println("arround-before");
Object o = null;
o = p.proceed();
System.out.println("arround-after");
return o;
}
}
添加aspectJTest.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"
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:aspectj-autoproxy/>
<bean id="aspectBean" class="org.springframework.study.day11.AspectBean"/>
<bean id="aspectJTest" class="org.springframework.study.day11.AspectJTest"/>
</beans>
编写测试方法
@Test
public void testAspectJ() {
ApplicationContext ac = new ClassPathXmlApplicationContext("aspectJTest.xml");
AspectBean aspectBean = ac.getBean(AspectBean.class);
aspectBean.aspectTest();
}
2.使用aop:aspect实现AOP
编写增强Advice
/**
* @author 周宁
* @Date 2019-07-22 20:54
*/
public class ExplictAspect {
public void beforeAdvice(){
System.out.println("before advice");
}
public void afterAdvice(){
System.out.println("after advice");
}
public Object aroundAdvice(ProceedingJoinPoint p) throws Throwable {
System.out.println("arround-before");
Object o = null;
o = p.proceed();
System.out.println("arround-after");
return o;
}
}
在aopXmlConfTest.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"
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">
<bean id="aspectBean" class="org.springframework.study.day11.AspectBean"/>
<bean id="explictAspect" class="org.springframework.study.day11.ExplictAspect"/>
<aop:config>
<!--切入点-->
<aop:pointcut expression="execution(* *.aspectTest(..))" id="p1"/>
<!-- 配置aop操作中的切面 -->
<aop:aspect ref="explictAspect" order="0">
<aop:around method="aroundAdvice" pointcut-ref="p1"/>
<aop:before method="beforeAdvice" pointcut-ref="p1"/>
<aop:after method="afterAdvice" pointcut-ref="p1"/>
</aop:aspect>
</aop:config>
</beans>
编写测试类
@Test
public void testXmlAop() {
ApplicationContext ac = new ClassPathXmlApplicationContext("aopXmlConfTest.xml");
AspectBean aspectBean = ac.getBean(AspectBean.class);
aspectBean.aspectTest();
}
3.使用aop:advisor实现AOP
编写Advisor实现实现MethodBeforeAdvice,AfterReturnningAdvice,MethodInterceptor接口
/**
* @author 周宁
* @Date 2019-07-23 14:11
*/
public class ExplictAdvisor implements MethodBeforeAdvice, AfterReturningAdvice, MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("arround-before");
Object o = null;
o = invocation.proceed();
System.out.println("arround-after");
return o;
}
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("before advice");
}
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("after advice");
}
}
在aopXmlConfTest2.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"
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">
<bean id="aspectBean" class="org.springframework.study.day11.AspectBean"/>
<bean id="explictAdvisor" class="org.springframework.study.day11.ExplictAdvisor"/>
<aop:config>
<!--切入点-->
<aop:pointcut expression="execution(* *.aspectTest(..))" id="p1"/>
<!-- 配置aop操作中的切面 -->
<aop:advisor advice-ref="explictAdvisor" pointcut-ref="p1"/>
</aop:config>
</beans>
编写测试类
@Test
public void testXmlAop2() {
ApplicationContext ac = new ClassPathXmlApplicationContext("aopXmlConfTest2.xml");
AspectBean aspectBean = ac.getBean(AspectBean.class);
aspectBean.aspectTest();
}