1. 程式人生 > >spring通過代理工廠建立代理物件

spring通過代理工廠建立代理物件

學習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 AdminBeanadminBean;】 ),當然要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版本多了很多新的特新,支援泛型,支援註解。。。

對於註解的方式,我想現在雖然趨於普遍,但是對於原理一定還是要了解清楚,畢竟對底層越熟,人生的機會越多。

在此,僅以本人的學習理解記錄下來