------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

说说那四种增强:前置增强,后置增强,环绕增强,异常增强

那什么是代理工厂bean呢?

  org.springframework.aop.framework.ProxyFactoryBean

  就是这个东西,他可以实现对方法的增强

@No.1:前置增强:

  需要前置增强的类SomeServiceImpl

package cn.dawn.day11aop01;

/**
* Created by Dawn on 2018/3/8.
*/
public class SomeServiceImpl {
public void doSome() {
System.out.println("do something");
}
}

  前置增强的内容的类,可以说实现前置增强接口的类LoggerBefore

package cn.dawn.day11aop01;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
* Created by Dawn on 2018/3/5.
*/
/*前置增强*/
public class LoggerBefore implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("日志记录");
}
}

  配置文件中:

<?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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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">
<!--要增强的对象-->
<bean id="service" class="cn.dawn.day11aop01.SomeServiceImpl"></bean>
<!--增强的内容-->
<bean id="before" class="cn.dawn.day11aop01.LoggerBefore"></bean>
<!--代理工厂bean-->
<bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--要增强的对象-->
<property name="target" ref="service"></property>
<!--增强的内容-->
<property name="interceptorNames" value="before"></property>
</bean> </beans>

  单测方法:

package cn.dawn.day11aop01;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180305 {
@Test
/*aop代理工厂bean前置增强*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day11aop01.xml");
SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
service.doSome();
}
}

@No.2:后置增强:

  需要后置增强的类:SomeServiceImpl(此类和前置那个写在不同包下,一会的配置文件也不同,往后都是如此)

package cn.dawn.day12aop02;

/**
* Created by Dawn on 2018/3/8.
*/
public class SomeServiceImpl {
public void doSome() {
System.out.println("do something");
}
}

  后置增强的内容的的类,他与前置增强的那个实现的接口不同:LoggerAfter

package cn.dawn.day12aop02;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

/**
* Created by Dawn on 2018/3/5.
*/
/*后置增强*/
public class LoggerAfter 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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">
<!--要增强的对象-->
<bean id="service" class="cn.dawn.day12aop02.SomeServiceImpl"></bean>
<!--增强的内容-->
<bean id="afteradvice" class="cn.dawn.day12aop02.LoggerAfter"></bean>
<!--代理工厂bean-->
<bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--要增强的对象-->
<property name="target" ref="service"></property>
<!--增强的内容-->
<property name="interceptorNames" value="afteradvice"></property>
</bean> </beans>

  单测方法:

package cn.dawn.day12aop02;

        import cn.dawn.day12aop02.SomeServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180305 {
@Test
/*aop代理工厂bean后置增强*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day12aop02.xml");
SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
service.doSome();
}
}

@No.3:环绕增强:

  需要环绕增强的类:SomeServiceImpl

package cn.dawn.day13aop03;

/**
* Created by Dawn on 2018/3/8.
*/
public class SomeServiceImpl {
public void doSome() {
System.out.println("do something");
}
}

  环绕增强内容的:

package cn.dawn.day13aop03;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; /**
* Created by Dawn on 2018/3/8.
*/
/*环绕增强需要实现MethodInterceptor这个接口*/
public class MethodAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("前置增强");
/*它可以使前置增强和后置增强分开,同时实现前置和后置*/
methodInvocation.proceed();
System.out.println("后置增强");
return null;
}
}

  配置文件:

<?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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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">
<!--要增强的对象-->
<bean id="service" class="cn.dawn.day13aop03.SomeServiceImpl"></bean>
<!--增强的内容-->
<bean id="methodAdvice" class="cn.dawn.day13aop03.MethodAdvice"></bean>
<!--代理工厂bean-->
<bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--要增强的对象-->
<property name="target" ref="service"></property>
<!--增强的内容-->
<property name="interceptorNames" value="methodAdvice"></property>
</bean> </beans>

  单测方法:

<?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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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">
<!--要增强的对象-->
<bean id="service" class="cn.dawn.day13aop03.SomeServiceImpl"></bean>
<!--增强的内容-->
<bean id="methodAdvice" class="cn.dawn.day13aop03.MethodAdvice"></bean>
<!--代理工厂bean-->
<bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--要增强的对象-->
<property name="target" ref="service"></property>
<!--增强的内容-->
<property name="interceptorNames" value="methodAdvice"></property>
</bean> </beans>

@No.4:异常增强:

  异常增强是主要用在他出错的时候,进行记录用的,此处模拟起来简单的就是除0异常和空指针异常

  写一个类,它里面的方法存在异常,运行时异常

  SomeServiceImpl类

package cn.dawn.day14aop04;

/**
* Created by Dawn on 2018/3/8.
*/
public class SomeServiceImpl {
public void doSome() {
System.out.println("do something");
String abc=null;
System.out.println(abc.toString());;
}
}

  此处模拟的是空指针异常,当然异常种类很多,不一一模拟了

  实现ThrowsAdvice接口的类

package cn.dawn.day14aop04;

import org.springframework.aop.ThrowsAdvice;

/**
* Created by Dawn on 2018/3/8.
*/
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Exception ex){
System.out.println("网络中断XXXXX-错误号05289");
}
}

  他这个接口里面没有方法要重写,很是奇怪,那是随便写什么defgabc的都可以吗?不是的,翻他的源码,他在上面的注释里提供了模板,只有按照他模板写的才能读取到,此处我用了他的其中一个模板方法

  配置文件中:

<?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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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">
<!--要增强的对象-->
<bean id="service" class="cn.dawn.day14aop04.SomeServiceImpl"></bean>
<!--增强的内容-->
<bean id="myThrowsAdvice" class="cn.dawn.day14aop04.MyThrowsAdvice"></bean>
<!--代理工厂bean-->
<bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--要增强的对象-->
<property name="target" ref="service"></property>
<!--增强的内容-->
<property name="interceptorNames" value="myThrowsAdvice"></property>
</bean> </beans>

  单测方法:

package cn.dawn.day14aop04;

import cn.dawn.day14aop04.SomeServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180305 {
@Test
/*aop代理工厂bean异常增强*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day14aop04.xml");
SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
try{
service.doSome();
}catch (Exception ex){
ex.printStackTrace();
} }
}

    看到这儿的try-catch了吧,他的作用是让单测能通过单元测试,能变成对勾,能执行到结束,不会因为模拟的异常而中断   

--------------------------End-------------------------

基于代理工厂Bean实现aop的四种增强总结完毕

SSM-Spring-11:Spring中使用代理工厂Bean实现aop的四种增强的更多相关文章

  1. sping练习,在Eclipse搭建的Spring开发环境中,使用工厂方式创建Bean对象,将创建的Bean对象输出到控制台。

    相关 知识 >>> 相关 练习 >>> 实现要求: 在Eclipse搭建的Spring开发环境中,使用工厂方式创建Bean对象,将创建的Bean对象输出到控制台.要 ...

  2. spring AOP 代理机制、执行过程、四种实现方式及示例详解

    1.加载过程 spring首先检测配置文件中的代理配置,然后去加载bean; 如果配置文件中没有配置代理,自然代理不会生效,如果配置了代理,但是代理还没有生效,那么有可能是加载顺序的问题,即在检测到代 ...

  3. Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配

    一.Spring IOC 容器支持自动装配 Bean,所谓自动装配是指,不需要通过 <property> 或 <constructor-arg> 为 Bean 的属性注入值的过 ...

  4. Python中xml、字典、json、类四种数据的转换

    最近学python,觉得python很强很大很强大,写一个学习随笔,当作留念注:xml.字典.json.类四种数据的转换,从左到右依次转换,即xml要转换为类时,先将xml转换为字典,再将字典转换为j ...

  5. Webform中Repeater控件--绑定嵌入C#代码四种方式

    网页里面嵌入C#代码用的是<% %>,嵌入php代码<?php ?> 绑定数据的四种方式: 1.直接绑定 <%#Eval("Code") %> ...

  6. JS中的this是什么,this的四种用法

    在Javascript中,this这个关键字可以说是用的非常多,说他简单呢也很简单,说他难呢也很难,有的人开发两三年了,自己好像也说不清this到底是什么.下面我们来看看: 1.在一般函数方法中使用 ...

  7. 解决 spring boot 线程中使用@Autowired注入Bean的方法,报java.lang.NullPointerException异常

    问题描述 在开发中,因某些业务逻辑执行时间太长,我们常使用线程来实现.常规服务实现类中,使用 @Autowired 来注入Bean,来调用其中的方法.但如果在线程类中使用@Autowired注入的Be ...

  8. spring和springmvc中,Configuration注解Bean重复加载

    问题:bean重复加载1.如下代码所示,开启Configuration注解,实现Bean代码注入,发现bean重复加载 @Configuration public class EhCacheConfi ...

  9. pageContext中page、request、session、application四种范围变量的用法。

    在PageContext中有很多作用域 第一种:PageContext.PAGE_SCOPE适用于当前页面的作用域,其接受数据的代码是pageContext.getAttribute();访问页面也是 ...

随机推荐

  1. 深度剖析linux内核万能--双向链表,Hash链表模版

    我们都知道,链表是数据结构中用得最广泛的一种数据结构,对于数据结构,有顺序存储,数组就是一种.有链式存储,链表算一种.当然还有索引式的,散列式的,各种风格的说法,叫法层出不穷,但是万变不离其中,只要知 ...

  2. Linux 内核配置机制(make menuconfig、Kconfig、makefile)讲解

    前面我们介绍模块编程的时候介绍了驱动进入内核有两种方式:模块和直接编译进内核,并介绍了模块的一种编译方式--在一个独立的文件夹通过makefile配合内核源码路径完成 那么如何将驱动直接编译进内核呢? ...

  3. GDI+ 读取jpg图片每个像素的值

    // 读取jpg图像像素rgb值.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #in ...

  4. Linux常用命令(第二版) --压缩解压缩命令

    压缩解压缩命令: ----------.gz---------- 1.压缩 gzip[GNU zip]: /bin/gzip 格式: gzip 选项 [文件] #压缩文件,压缩后扩展名为.gz,Lin ...

  5. Oracle E-Business Suite Maintenance Guide Release 12.2(Patching Procedures)

    更多内容参考: http://docs.oracle.com/cd/E51111_01/current/acrobat/122ebsmt.zip Preparing for Patching For ...

  6. 地产IT人福利:帆软地产BI解决方案全解析

    解决方案下载地址 帆软大型地产集团项目解决方案 下载地址:http://pan.baidu.com/s/1pJGeqKF帆软地产BI解决方案之KPI考核系统 下载地址:http://pan.baidu ...

  7. 最新App Store审核10大被拒理由

    最近,苹果在官网给出了截至2015年2月份应用被拒绝的十大理由,其中50%以上的应用被拒绝都是因为这10个原因,其中7个理由和2014年相同,其中排名前三的原因分别是:需要补充更多信息.存在明显的bu ...

  8. ZooKeeper客户端事件串行化处理

    为了提升系统的性能,进一步提高系统的吞吐能力,最近公司很多系统都在进行异步化改造.在异步化改造的过程中,肯定会比以前碰到更多的多线程问题,上周就碰到ZooKeeper客户端异步化过程中的一个死锁问题, ...

  9. Java不走弯路教程(4.Client-Server模式(1)-Server)

    4.Client-Server模式(1)-Server 在上一章中,我们完成了MyDataBase.java的编写,类似于一个简单的数据库功能,提供了用户验证,查询操作. 在本章中,我们将继续扩展这个 ...

  10. Qt Creator 更改默认构建目录到工程目录下

    Qt Creator 更改默认构建目录到工程目录下 步骤 工具->选项->构建和运行->概要->Default build directory->去掉第一个". ...