您的位置:首页 > 编程语言 > Java开发

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版本多了很多新的特新,支持泛型,支持注解。。。

对于注解的方式,我想现在虽然趋于普遍,但是对于原理一定还是要了解清楚,毕竟对底层越熟,人生的机会越多。

在此,仅以本人的学习理解记录下来
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐