spring通过代理工厂创建代理对象
2013-08-02 16:04
375 查看
学习spring有几天了,感觉自己对aop掌握不是很熟,自己就写了几个test
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--初识笔记:property 中的name代表的是上层bean(类)中的filed (filed可以理解为字段,或者是说此处的filed应该是某个类的对象
【private AdminBean
adminBean;】 ),当然要ref(关联)就必须有setXXX-->
<!--目标对象-->
<bean id="personService" class="org.xiaoyi.service.impl.PersonServiceImpl"></bean>
<bean id="adminbean" class="org.xiaoyi.bean.AdminBean"></bean> <!--ioc管理,包含纯洁的权限验证方法-->
<bean id="logbean" class="org.xiaoyi.bean.LogBean"></bean>
<!--ioc管理,包含纯洁的记录日志方法-->
<!--权限验证前置通知-->
<bean id="adminInterceptor" class="org.xiaoyi.interceptor.AdminInterceptor">
<!--ioc管理,implements MethodBeforeAdvice,使该类变成前置通知类,其中有before(Methodmethod,Object[] args, Object target){}-->
<property name="adminBean" ref="adminbean"></property> <!--关联 bean:adminbean ,作用是:为了在通知方法before()中调用关联bean的权
限验证-->
</bean>
<!--日志记录的后置通知-->
<bean id="logInterceptor" class="org.xiaoyi.interceptor.LogInterceptor"> <!--implements AfterReturningAdvice-->
<property name="logBean" ref="logbean"></property>
</bean>
<!--通过代理工厂创建代理对象-->
<bean id="targetProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--ioc管理spring自己的类,其作用在于把通知和业务绑定,使其作
为一个代理,让通知和业务形成一个整体,升华后就是事物-->
<!--代理目标-->
<property name="target" ref="personService"></property>
<!--关联personService,作用是:为了在创建代理的时候让代理得到personService中的业务
方法-->
<!--代理的接口-->
<property name="proxyInterfaces" value="org.xiaoyi.service.PersonService"></property> <!--从事物的底层来看,上面已经关联personService,它就
应该拿的到对应的接口( ps.getClass().getInterfaces())
,此处又把接口传过去。。。不理解,也许是spring底层需要
-->
<!--代理通知-->
<property name="interceptorNames"> <!--实施绑定-->
<list>
<value>adminInterceptor</value>
<value>logInterceptor</value>
</list>
</property>
</bean>
</beans>
虽然3.0以上的spring版本多了很多新的特新,支持泛型,支持注解。。。
对于注解的方式,我想现在虽然趋于普遍,但是对于原理一定还是要了解清楚,毕竟对底层越熟,人生的机会越多。
在此,仅以本人的学习理解记录下来
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--初识笔记:property 中的name代表的是上层bean(类)中的filed (filed可以理解为字段,或者是说此处的filed应该是某个类的对象
【private AdminBean
adminBean;】 ),当然要ref(关联)就必须有setXXX-->
<!--目标对象-->
<bean id="personService" class="org.xiaoyi.service.impl.PersonServiceImpl"></bean>
<bean id="adminbean" class="org.xiaoyi.bean.AdminBean"></bean> <!--ioc管理,包含纯洁的权限验证方法-->
<bean id="logbean" class="org.xiaoyi.bean.LogBean"></bean>
<!--ioc管理,包含纯洁的记录日志方法-->
<!--权限验证前置通知-->
<bean id="adminInterceptor" class="org.xiaoyi.interceptor.AdminInterceptor">
<!--ioc管理,implements MethodBeforeAdvice,使该类变成前置通知类,其中有before(Methodmethod,Object[] args, Object target){}-->
<property name="adminBean" ref="adminbean"></property> <!--关联 bean:adminbean ,作用是:为了在通知方法before()中调用关联bean的权
限验证-->
</bean>
<!--日志记录的后置通知-->
<bean id="logInterceptor" class="org.xiaoyi.interceptor.LogInterceptor"> <!--implements AfterReturningAdvice-->
<property name="logBean" ref="logbean"></property>
</bean>
<!--通过代理工厂创建代理对象-->
<bean id="targetProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--ioc管理spring自己的类,其作用在于把通知和业务绑定,使其作
为一个代理,让通知和业务形成一个整体,升华后就是事物-->
<!--代理目标-->
<property name="target" ref="personService"></property>
<!--关联personService,作用是:为了在创建代理的时候让代理得到personService中的业务
方法-->
<!--代理的接口-->
<property name="proxyInterfaces" value="org.xiaoyi.service.PersonService"></property> <!--从事物的底层来看,上面已经关联personService,它就
应该拿的到对应的接口( ps.getClass().getInterfaces())
,此处又把接口传过去。。。不理解,也许是spring底层需要
-->
<!--代理通知-->
<property name="interceptorNames"> <!--实施绑定-->
<list>
<value>adminInterceptor</value>
<value>logInterceptor</value>
</list>
</property>
</bean>
</beans>
虽然3.0以上的spring版本多了很多新的特新,支持泛型,支持注解。。。
对于注解的方式,我想现在虽然趋于普遍,但是对于原理一定还是要了解清楚,毕竟对底层越熟,人生的机会越多。
在此,仅以本人的学习理解记录下来
相关文章推荐
- mybatis+spring中的Mapper通过非自动注入方式获取Mapper代理对象(Spring 工厂)
- spring创建bean的三种方式-通过构造器,通过静态工厂方法,通过实例工厂方法
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- Spring进阶之路(9)-Spring AOP面向切面编程概念以及通过JDK代理生成AOP代理对象
- Spring进阶之路(10)-Advice简介以及通过cglib生成AOP代理对象
- Spring aop事务代理对象通过TransactionInterceptor处理目标方法事务过程,cglib方式
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- Spring创建对象工厂功能的简单实现
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- Spring AOP高级——源码实现(3)AopProxy代理对象之JDK动态代理的创建过程
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- spring学习(五)—通过注解方式创建对象和注入属性
- Spring AOP 源码分析 - 创建代理对象
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- spring 托管 工厂创建对象
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- 【Spring源码--AOP的实现】(一)AopProxy代理对象的创建
- 关于spring.net的面向切面编程 (Aspect Oriented Programming with Spring.NET)-使用工厂创建代理(Using the ProxyFactoryObject to create AOP proxies)
- Spring aop 通过获取代理对象实现事务切换