标签:http round beans code getbean oct image advice int
AroundAdvice
1、在方法之前和之后来执行相应的操作
2、实现MethodInterceptor接口
接口文件:
public interface IHello { public void sayHello(String str); } public class Hello implements IHello { @Override public void sayHello(String str) { System.out.println("你好"+str); } }
SayAroundAdvice文件:
public class SayAroundAdvice implements MethodInterceptor { @Override public Object invoke(MethodInvocation arg0) throws Throwable { // TODO Auto-generated method stub Object result=null; System.out.println("Around在方法执行前做事情!"); result=arg0.proceed(); System.out.println("Around在方法执行后做事情!"); return result; } }
Main文件:
public class MainTest { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); IHello hello=(IHello)context.getBean("helloProxy"); hello.sayHello("访客"); } }
applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <!-- 建立目标对象实例 --> <bean id="bean_hello" class="com.pb.Hello" /> <!-- 创建Around advice实例 --> <bean id="ssd" class="com.pb.SayAroundAdvice" /> <!-- 建立代理对象 --> <bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 设置代理的接口 --> <property name="proxyInterfaces"> <value>com.pb.IHello</value> </property> <!-- 设置目标对象实例 --> <property name="target"> <ref bean="bean_hello"/> </property> <!-- 设置Advice实例 --> <property name="interceptorNames"> <list> <value>ssd</value> </list> </property> </bean> </beans>
执行效果:
标签:http round beans code getbean oct image advice int
原文地址:https://www.cnblogs.com/schangxiang/p/11145462.html