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>
执行效果:
如对本文有疑问, 点击进行留言回复!!
Spring Security——认证授权的概念、授权的数据模型、RBAC实现授权
半夜整理50道微服务面试专题(含答案),你值得拥有,看完太香了~
荐 【Java】JavaMail编程实现邮件客户端-OutBox & InBox
基于JavaWeb的SSM框架的B/S微博系统(源码+数据库+论文)
Hadoop-HDFS-读写数据流图解+详细分析,源码解析-连载中
荐 Spring Security 实战干货:从零手写一个验证码登录
荐 SpringBoot+Vue实现SpringSecurity+验证码登录与点击刷新验证码
网友评论