SSM-Spring-15:Spring中名称自动代理生成器BeanNameAutoProxyCreator
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
名称自动代理生成器:BeanNameAutoProxyCreator
为了更好的测试,我放了俩个接口,俩个实现类:
ISomeService接口:
package cn.dawn.day18auto02; /**
* Created by Dawn on 2018/3/8.
*/
public interface ISomeService {
public void insert();
public void delete();
public void select();
public void update();
}
它的实现类:SomeServiceImpl
package cn.dawn.day18auto02; /**
* Created by Dawn on 2018/3/8.
*/
public class SomeServiceImpl implements ISomeService { public void insert() {
System.out.println("insert ok");
} public void delete() {
System.out.println("delete ok"); } public void select() {
System.out.println("select ok"); } public void update() {
System.out.println("update ok"); }
}
IBookService接口:
package cn.dawn.day18auto02; /**
* Created by Dawn on 2018/3/12.
*/
public interface IBookService {
public void selectAllBooks();
}
它的实现类:BookServiceImpl
package cn.dawn.day18auto02; /**
* Created by Dawn on 2018/3/12.
*/
public class BookServiceImpl implements IBookService {
public void selectAllBooks() {
System.out.println("selectbooks ok");
}
}
一个增强的
package cn.dawn.day18auto02; 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配置文件中:
<?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.day18auto02.SomeServiceImpl"></bean>
<bean id="bookservice" class="cn.dawn.day18auto02.BookServiceImpl"></bean>
<!--增强的内容-->
<bean id="myadvice" class="cn.dawn.day18auto02.LoggerBefore"></bean>
<!--顾问-->
<bean id="myadvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="myadvice"></property>
<!--<property name="mappedNames" value="do*"></property>-->
<!--<property name="pattern" value=".*do.*"></property>-->
<property name="patterns" value=".*e.*"></property>
</bean>
<!--名称自动代理生成器-->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="service,bookservice"></property>
<property name="interceptorNames" value="myadvisor"></property>
</bean> </beans>
此处没有顾问是不行的,名称自动代理生成器BeanNameAutoProxyCreator中的beanNames的值是String类型的数组,所以可以放多个目标对象,用逗号隔开即可
单测方法:
package cn.dawn.day18auto02; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180312 {
@Test
/*aop代理工厂bean异常增强*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day18auto02.xml");
ISomeService service = (ISomeService) context.getBean("service");
IBookService bookservice = (IBookService) context.getBean("bookservice"); service.select();
bookservice.selectAllBooks(); }
}
SSM-Spring-15:Spring中名称自动代理生成器BeanNameAutoProxyCreator的更多相关文章
- spring8——AOP之Bean的自动代理生成器
对于上篇博客http://www.cnblogs.com/cdf-opensource-007/p/6464237.html结尾处提到的两个问题,可以使用spring提供的自动代理生成器解决.自动代理 ...
- SSM-Spring-14:Spring中默认自动代理DefaultAdvisorAutoProxyCreator
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 默认自动代理DefaultAdvisorAutoProxyCreator 本处没有什么要讲的,放原代码 ISo ...
- spring:按照Bean的名称自动装配User
本实例将介绍如何按照Bean 的名称自动装配 User 对象! <bean> 元素的 autowire 属性负责自动装配 <bean> 标签,定义 JavaBean 的属性.这 ...
- Spring AOP: Spring之面向方面编程
Spring AOP: Spring之面向方面编程 面向方面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP). 面向对象将应用程序分解成 各个层次的对象,而AOP将程序分解 ...
- JAVA框架之Spring【Spring事务详解】
spring提供的事务管理可以分为两类:编程式的和声明式的.编程式的,比较灵活,但是代码量大,存在重复的代码比较多:声明式的比编程式的更灵活.编程式主要使用transactionTemplate.省略 ...
- Spring 中如何自动创建代理(spring中的三种自动代理创建器)
Spring 提供了自动代理机制,可以让容器自动生成代理,从而把开发人员从繁琐的配置中解脱出来 . 具体是使用 BeanPostProcessor 来实现这项功能. 这三种自动代理创建器 为:Bean ...
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
随机推荐
- AngularJS进阶(十八)在AngularJS应用中集成科大讯飞语音输入功能
在AngularJS应用中集成科大讯飞语音输入功能 注:请点击此处进行充电! 前言 根据项目需求,需要在首页搜索框中添加语音输入功能,考虑到科大讯飞语音业务的强大能力,遂决定使用科大讯飞语音输入第三方 ...
- Android驱动中的Kconfig文件与Makefile文件
内核源码树的目录下都有两个文档Kconfig(2.4版本是Config.in)和Makefile.分布到各目录的Kconfig构成了一个分布式的内核配置数据库,每个Kconfig分别描述了所属目录源文 ...
- Java线程专栏文章汇总
转载自 http://blog.csdn.net/ghsau/article/details/17609747 JDK5.0之前传统线程 Java线程(一):线程安全与不安全 J ...
- C/C++中如何产生伪随机数
1. C语言中的伪随机数产生函数 本节主要参考自一博文及cppreferrence. 我们知道rand()函数可以用来产生随机数,但是这不是真正意义上的随机数,是一个伪随机数,是根据一个数(我们可以 ...
- charles抓取https请求包
说明: 用charles抓取https请求,会出现SSL Proxying disabled in Proxy Settings这样的提示,如下图.要通过charles抓取数据,还需要进行一些简单的设 ...
- 一个简单的基于 DirectShow 的播放器 2(对话框类)
上篇文章分析了一个封装DirectShow各种接口的封装类(CDXGraph):一个简单的基于 DirectShow 的播放器 1(封装类) 本文继续上篇文章,分析一下调用这个封装类(CDXGrap ...
- PS 图像调整算法——自动色阶 (Auto Levels)
PS 给出的定义: Enhance Per Channel Contrast:Maximizes the tonal range in each channel to produce a more d ...
- Mina源码阅读笔记(六)—Mina异步IO的实现IoFuture
IoFuture是和IoSession紧密相连的一个类,在官网上并没有对它的描述,因为它一般不会显示的拿出来用,权当是一个工具类被session所使用.当然在作用上,这个系列可并不简单,我们先看源码的 ...
- LeetCode(31)-Factorial Trailing Zeroes
题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- LeetCode(29)-Plus One
题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...