1.前置增强
接口 类
public interface ISomeService { public void doSome(); }
public class SomeService implements ISomeService { //核心业务 public void doSome(){ System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K"); } }
import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; //前置通知 public class MyBeforeAdvise implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("=============log=================="); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--01.目标对象--> <bean id="someService" class="cn.bdqn.spring09aop01.SomeService"></bean> <!--02.增强 通知--> <bean id="beforeAdvice" class="cn.bdqn.spring09aop01.MyBeforeAdvise"></bean> <!--03.aop --> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <!--做怎么样的增强--> <property name="interceptorNames" value="beforeAdvice"></property> <property name="proxyTargetClass" value="true"></property> </bean> </beans>
单侧
@Test // 前置增强 public void test04(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring07aop01.xml"); SomeService service = (SomeService) ctx.getBean("proxyService"); service.doSome(); }
2.后置增强
public class MyAfterReturningAdvice implements AfterReturningAdvice { public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("===========after================"); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--01.目标对象--> <bean id="someService" class="cn.bdqn.spring10afterreturingadvice.SomeService"></bean> <!--02.增强 通知--> <bean id="afterAdvice" class="cn.bdqn.spring10afterreturingadvice.MyAfterReturningAdvice"></bean> <!--03.aop --> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <!--做怎么样的增强--> <property name="interceptorNames" value="afterAdvice"></property> </bean> </beans>
单侧
@Test // 后置增强 public void test05(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring08aop02.xml"); ISomeService service = (ISomeService) ctx.getBean("proxyService"); service.doSome(); }
3.环绕增强
public class MyMethodInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("before"); methodInvocation.proceed(); System.out.println("after"); return null; } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--01.目标对象--> <bean id="someService" class="cn.bdqn.spring11methodinterceptor.SomeService"></bean> <!--02.增强 通知--> <bean id="methodAdvice" class="cn.bdqn.spring11methodinterceptor.MyMethodInterceptor"></bean> <!--03.aop --> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <!--做怎么样的增强--> <property name="interceptorNames" value="methodAdvice"></property> </bean> </beans>
单侧
@Test // 环绕增强 public void test06(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring09aop03.xml"); ISomeService service = (ISomeService) ctx.getBean("proxyService"); service.doSome(); }
4.异常增强
public class MyThrowsAdvice implements ThrowsAdvice { public void afterThrowing(Exception ex){ System.out.println("错误"); } }
public interface IHHJJ { public void run(); public void run(String style); }
public class HHJJImpl implements IHHJJ { public void run() { } public void run(String style) { } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--01.目标对象--> <bean id="someService" class="cn.bdqn.spring12throwadvice.SomeService"></bean> <!--02.增强 通知--> <bean id="throwsAdvice" class="cn.bdqn.spring12throwadvice.MyThrowsAdvice"></bean> <!--03.aop --> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置需要增强的目标对象--> <property name="target" ref="someService"></property> <!--做怎么样的增强--> <property name="interceptorNames" value="throwsAdvice"></property> </bean> </beans>
单侧
@Test // 异常增强 public void test07(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring10aop04.xml"); ISomeService service = (ISomeService) ctx.getBean("proxyService"); service.doSome(); }
时间: 2024-10-26 20:20:09