多种方式实现AOP
一、使用代理工厂完成声明式增强
1.创建业务接口
public interface IdoSomeService {
public void doSomething();
}
2.创建接口实现类
public class IdoSomeServiceImpl implements IdoSomeService{
@Override
public void doSomething() {
System.out.println("真实业务");
}
}
3.创建切面类
/**
* 切面
*/
public class MyBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("前置增强");
}
}
4.编写applicationContext.xml配置文件
<!--注入业务Bean-->
<bean id="idoSomeService" class="cn.spring.proxyfactory.IdoSomeServiceImpl"></bean>
<!--增强:切面-->
<bean id="myBeforeAdvice" class="cn.spring.proxyfactory.MyBeforeAdvice"></bean>
<!--使用代理工厂实现增强-->
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--将增强和业务织入 到一起 -->
<property name="target" ref="idoSomeService"></property>
<!--拦截增强类-->
<property name="interceptorNames" value="myBeforeAdvice"></property>
</bean>
5.创建测试类
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取代理工厂
IdoSomeService idoSomeService=(IdoSomeService)context.getBean("proxyFactory");
idoSomeService.doSomething();
}
}
二、使用代理工厂完成环绕增强
1.创建业务接口
public interface IdoSomeService {
public void doSomething();
}
2.创建业务接口实现类
public class IdoSomeServiceImpl implements IdoSomeService{
@Override
public void doSomething() {
System.out.println("真实业务");
}
}
3.创建切面类
public class MyAroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("环绕前");
//调用核心业务方法也可以获取方法内的参数 也可以获取目标对象
Object proceed = methodInvocation.proceed();
Object aThis = methodInvocation.getThis();
System.out.println(aThis);
System.out.println("环绕后");
return proceed;
}
}
4.编写applicationContext.xml配置文件
环绕增强
<bean id="idoSomeService" class="cn.spring.around.IdoSomeServiceImpl"></bean>
<bean id="myAroundAdvice" class="cn.spring.around.MyAroundAdvice"></bean>
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--将增强和业务织入到一起-->
<property name="target" ref="idoSomeService"></property>
<property name="interceptorNames" value="myAroundAdvice"></property>
<!--更换代理方式 proxyTargetClass默认值为false 默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
<property name="proxyTargetClass" value="true"></property>
</bean>
4.创建测试类
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取代理工厂
IdoSomeService idoSomeService=(IdoSomeService)context.getBean("proxyFactory");
idoSomeService.doSomething();
}
三、使用工厂代理工厂完成异常增强
1.创建业务接口
public interface IdoSomeService {
public void doSomething() throws Exception;
}
2.创建业务接口实现类
public class IdoSomeServiceImpl implements IdoSomeService {
@Override
public void doSomething() throws Exception{
int result=5/0;
System.out.println("真实业务");
}
}
3.创建切面类
public class MyThrowAdvice{
public void afterThrowing(Exception ex){
System.out.println("=====发生了异常,执行增强操作===============");
}
}
4.编写applicationContext.xml配置文件
<!--环绕增强-->
<bean id="idoSomeService" class="cn.spring.around.IdoSomeServiceImpl"></bean>
<bean id="myAroundAdvice" class="cn.spring.around.MyAroundAdvice"></bean>
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--将增强和业务织入到一起-->
<property name="target" ref="idoSomeService"></property>
<property name="interceptorNames" value="myAroundAdvice"></property>
<!--更换代理方式 proxyTargetClass默认值为false 默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
<property name="proxyTargetClass" value="true"></property>
</bean>
5.创建测试类
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取代理工厂
IdoSomeService idoSomeService=(IdoSomeService)context.getBean("idoSomeService");
try{
idoSomeService.doSomething();
}catch (Exception e){
e.printStackTrace();
}
System.out.println("2222222222222");
}
四、使用代理工厂实现最终增强
1.创建业务接口
public interface IdoSomeService {
public void doSomething() throws Exception;
}
2.创建业务接口实现类
public class IdoSomeServiceImpl implements IdoSomeService {
@Override
public void doSomething() throws Exception{
int result=5/0;
System.out.println("真实业务");
}
}
3.创建切面类
public class MyThrowAdvice{
public void afterAdvice(){
System.out.println("======执行最终异常===============");
}
}
4.编写applicationContext.xml配置文件
<bean id="idoSomeService" class="cn.spring.throwadvice.IdoSomeServiceImpl"></bean>
<bean id="myAdvice" class="cn.spring.throwadvice.MyThrowAdvice"></bean>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/>
<aop:aspect ref="myAdvice">
<aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
<aop:after method="afterAdvice" pointcut-ref="pointcut"></aop:after>
</aop:aspect>
</aop:config>
5.创建测试类
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取代理工厂
IdoSomeService idoSomeService=(IdoSomeService)context.getBean("idoSomeService");
try{
idoSomeService.doSomething();
}catch (Exception e){
e.printStackTrace();
}
System.out.println("2222222222222");
}
多种方式实现AOP的更多相关文章
- 特性attribute,声明和使用attribute,应用attribute,AOP面向切面,多种方式实现AOP
1 特性attribute,和注释有什么区别2 声明和使用attribute3 应用attribute4 AOP面向切面5 多种方式实现AOP ---------------------------- ...
- IOC和AOP使用扩展 多种方式实现依赖注入
多种方式实现依赖注入 1.Spring 使用setter访问器实现对属性的赋值, 2.Spring 构造constructor方法赋值, 3.接口注入 4.Spring P命名空间注入直接量 sett ...
- Spring学习总结(一)——Spring实现IoC的多种方式
控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法,也有人认为DI只是IoC的另一种说法.没有IoC的程序中我们使用面向对象编程对象的创 ...
- idea打包jar的多种方式
这里总结出用IDEA打包jar包的多种方式,以后的项目打包Jar包可以参考如下形式: 用IDEA自带的打包形式 用Maven插件maven-shade-plugin打包 用Maven插件maven-a ...
- Java中测试异常的多种方式
使用JUnit来测试Java代码中的异常有很多种方式,你知道几种? 给定这样一个class. Person.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- PDO多种方式取得查询结果
PDO多种方式取得查询结果 01 December 2009 1:26 Tuesday by Sjolzy PDO最大的特点之一是它的灵活性,本节将介绍如何取得查询结果,包括: 数组(数值或关联数组) ...
- java 获取classpath下文件多种方式
java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...
- sql语句分页多种方式ROW_NUMBER()OVER
sql语句分页多种方式ROW_NUMBER()OVER 摘自: http://www.cnblogs.com/CodingArt/articles/1692468.html 方式一 select to ...
- JavaScript中判断为整数的多种方式
之前记录过JavaScript中判断为数字类型的多种方式,这篇看看如何判断为整数类型(Integer). JavaScript中不区分整数和浮点数,所有数字内部都采用64位浮点格式表示,和Java的d ...
随机推荐
- android studio连接雷电模拟器 【AS 模拟器】
⭐ 我的网站: www.mengyingjie.com ⭐ Android studio 怎么连接安卓模拟器呢,跟着下面的步骤即可 (以雷电模拟器为例,其他模拟器类似) 一.下载模拟器 下载安装完雷电 ...
- SpringBoot区块链之以太坊区块高度扫描(简洁版)
继续昨天的demo往下写写:[SpringBoot区块链之以太坊开发(整合Web3j)](https://juejin.im/post/5d88e6c1518825094f69e887),将复杂的逻辑 ...
- 配置文件my.cnf---配置信息注释大全
在进行MySQL与CM+CHD之间的应用配置时,发现此前对于MySQL的配置含义过于模糊,所以将CM+CHD集群所涉及MySQL方面的配置含义进行抽取并加以注释,方便此后的配置和使用. 一.客户端设置 ...
- SpringCloud实现服务间调用(RestTemplate方式)
上一篇文章<SpringCloud搭建注册中心与服务注册>介绍了注册中心的搭建和服务的注册,本文将介绍下服务消费者调用服务提供者的过程. 本文目录 一.服务调用流程二.服务提供者三.服务消 ...
- Mycat 配置文件server.xml
server.xml 几乎保存了所有 mycat 需要的系统配置信息. 1.system 标签: 该标签内嵌套的所有 property 标签都与系统配置有关. charset 属性: 该属性用于字符集 ...
- Flask基础(06)-->视图常用逻辑
Flask基础(06)-->视图常用逻辑 返回json 重定向:url_for 自定义状态码 返回json:在使用 Flask 写一个接口时候需要给客户端返回 JSON 数据,在 Flask 中 ...
- malformed header from script. Bad header的解决方法
今天配了CGI服务器,打开CGI报错: [Wed Jun 02 13:57:21 2010] [error] [client 192.168.0.1] malformed header from sc ...
- Spring IOC(3)----bean实例化
前面一节说到invokeBeanFactoryPostProcessors方法的调用来注册bean定义,这次来看看finishBeanFactoryInitialization这个方法实例化非懒加载的 ...
- 【Java】登录验证码
Html: <input id="verifyCode" name="verifyCode" type="text" value=&q ...
- Nginx常用命令,解决你日常运维的烦恼
前面,跟大家简单地介绍了负载均衡和Nginx的一些基础配置(Nginx负载均衡配置实例),接下来,跟大家介绍一下Nginx的常用命令,便于日常的运维. 查看原文 停止Nginx的方法 通过之前的学习, ...