聽聽聽
聽聽聽 AOP
浣滀負
Spring
榪欎釜杞婚噺綰х殑瀹瑰櫒涓緢閲嶈鐨勪竴閮ㄥ垎錛屽緱鍒拌秺鏉ヨ秺澶氱殑鍏蟲敞錛?/span>
Spring
鐨?/span>
Transaction
灝辨槸鐢?/span>
AOP
鏉ョ鐞嗙殑錛屼粖澶╁氨閫氳繃綆鍗曠殑渚嬪瓙鏉ョ湅鐪?/span>
Spring
涓殑
AOP
鐨勫熀鏈嬌鐢ㄦ柟娉曘?/span>
聽
聽
棣栧厛紜畾灝嗚
Proxy
鐨勭洰鏍囷紝鍦?/span>
Spring
涓粯璁ら噰鐢?/span>
JDK
涓殑
dynamic proxy
錛屽畠鍙兘澶熷疄鐜版帴鍙g殑浠g悊錛屽鏋滄兂瀵圭被榪涜浠g悊鐨勮瘽錛岄渶瑕侀噰鐢?/span>
CGLIB
鐨?/span>
proxy
銆傛樉鐒訛紝閫夋嫨
鈥?/span>
緙栫▼鍒版帴鍙?/span>
鈥?/span>
鏄洿鏄庢櫤鐨勫仛娉曘?/span>
聽
涓嬮潰鏄皢瑕佷唬鐞嗙殑鎺ュ彛錛?/span>
public interface FooInterface {
聽聽聽 public void printFoo();
聽聽聽 public void dummyFoo();
}
浠ュ強鍏朵竴涓畝鍗曠殑瀹炵幇錛?/span>
public class FooImpl implements FooInterface {
聽聽聽 public void printFoo() {
聽聽聽聽聽聽聽 System.out.println("In FooImpl.printFoo");
聽聽聽 }
聽聽聽 public void dummyFoo() {
聽聽聽聽聽聽聽 System.out.println("In FooImpl.dummyFoo");
聽聽聽 }
}
聽
鎺ヤ笅鏉ュ垱寤轟竴涓?/span>
Advice
錛屽湪
Spring
涓敮鎸?/span>
Around,Before,After returning
鍜?/span>
Throws
鍥涚
Advice
錛岃繖閲屽氨浠ョ畝鍗曠殑
Before Advice
涓句緥錛?/span>
聽
public class PrintBeforeAdvice implements MethodBeforeAdvice {
聽聽聽 public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
聽聽聽聽聽聽聽 System.out.println("In PrintBeforeAdvice");
聽聽聽 }
}
聽
鏈変簡鑷繁鐨?/span>
business interface
鍜?/span>
advice
錛屽墿涓嬬殑灝辨槸濡備綍鍘昏閰嶅畠浠簡錛岄鍏堝埄鐢?/span>
ProxyFactory
浠ョ紪紼嬫柟寮忓疄鐜幫紝濡備笅錛?/span>
聽
public class AopTestMain {
聽聽聽 public static void main(String[] args) {
聽聽聽聽聽聽聽 FooImpl fooImpl = new FooImpl();
聽聽聽聽聽聽聽 PrintBeforeAdvice myAdvice = new PrintBeforeAdvice();
聽聽聽聽聽
聽聽聽聽聽聽 聽ProxyFactory factory = new ProxyFactory(fooImpl);
聽聽聽聽聽聽聽 factory.addBeforeAdvice(myAdvice);
聽聽聽聽聽聽聽 FooInterface myInterface = (FooInterface)factory.getProxy();
聽聽聽聽聽聽聽 myInterface.printFoo();
聽聽聽聽聽聽聽 myInterface.dummyFoo();
聽聽聽 }
}
聽
聽
鐜板湪鎵ц紼嬪簭錛岀濂囩殑緇撴灉灝卞嚭鐜頒簡錛?/span>
聽
聽 In PrintBeforeAdvice
聽 In FooImpl.printFoo
聽 In PrintBeforeAdvice
聽 In FooImpl.dummyFoo
聽
聽聽
铏界劧榪欐牱鑳戒綋浼氬埌
Spring
涓?/span>
AOP
鐨勭敤娉曪紝浣嗚繖鍐充笉鏄煎緱鎺ㄨ崘鐨勬柟娉曪紝鏃㈢劧浣跨敤浜?/span>
Spring
錛屽湪
ApplicationContext
涓閰嶆墍闇瑕?/span>
鐨?/span>
bean
鎵嶆槸鏈浣崇瓥鐣ワ紝瀹炵幇涓婇潰鐨勫姛鑳藉彧闇瑕佸啓涓畝鍗曠殑
applicationContext
灝卞彲浠ヤ簡錛屽涓嬶細
聽
聽 <?xml version="1.0" encoding="UTF-8"?>
聽 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
聽聽聽 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
聽聽聽 <description>The aop application context</description>
聽聽聽 <bean id="fooTarget" class="FooImpl"/>
聽聽聽 <bean id="myAdvice" class="PrintBeforeAdvice"/>
聽聽 聽<bean id="foo" class="org.springframework.aop.framework.ProxyFactoryBean">
聽聽聽聽 <property name="proxyInterfaces">
聽聽聽聽聽聽 <value>FooInterface</value>
聽聽聽聽 </property>
聽聽聽聽聽<property name="target">
聽聽聽聽聽聽 <ref local="fooTarget"/>
聽聽聽聽 </property>
聽聽聽聽 <property name="interceptorNames">
聽聽聽聽聽聽 <list>
聽聽聽聽聽聽聽聽 <value>myAdvice</value>
聽聽聽聽聽聽 </list>
聽聽聽聽 </property>
聽聽聽 </bean>
</beans>
聽
聽聽
褰撶劧錛?/span>
main
涓殑浠g爜涔熻榪涜鐩稿簲鐨勪慨鏀癸細
聽聽聽聽
public static void main(String[] args) {
聽聽聽 ClassPathXmlApplicationContext context = new聽
聽聽聽聽聽聽聽聽聽聽聽聽聽ClassPathXmlApplicationContext("applicationContext.xml");
聽聽聽 FooInterface foo = (FooInterface)context.getBean("foo");
聽聽聽 foo.printFoo();
聽聽聽 foo.dummyFoo();
}
聽
聽聽
鐜板湪榪愯涓涓嬶紝緇撴灉灝嗗拰涓婇潰鐨勮繍琛岀粨鏋滃畬鍏ㄤ竴鏍鳳紝榪欐牱鏄笉鏄洿浼橀泤錛熷綋闇瑕佹洿鏀瑰疄鐜版椂錛屽彧闇瑕佷慨鏀歸厤緗枃浠跺氨鍙互浜嗭紝紼嬪簭涓殑浠g爜涓嶉渶浠諱綍鏀瑰姩銆?/span>
聽
聽聽
浣嗘槸錛岃繖鏃跺欎細鍙戠幇琚?/span>
proxy
鐨?/span>
object
涓殑鎵鏈夋柟娉曡皟鐢ㄦ椂閮藉皢榪愯
advice
涓殑
before
錛岃繖鏄劇劧涓嶈兘婊¤凍緇濆ぇ澶氭暟鎯呭喌涓嬬殑闇瑕侊紝姝ゆ椂錛屽彧
闇鍊熺敤
Advisor
灝卞彲浠ヤ簡錛屽綋鐒惰鍦?/span>
Advisor
涓埄鐢?/span>
pattern
璁劇疆濂藉摢浜涙柟娉曢渶瑕?/span>
advice
錛屾洿鏀?/span>
applicationContext
濡備笅錛?/span>
聽
聽 <?xml version="1.0" encoding="UTF-8"?>
聽 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
聽聽聽 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
聽聽聽 <description>The springeva application context</description>
聽聽 聽<bean id="fooTarget" class="FooImpl"/>
聽聽聽 <bean id="printBeforeAdvice" class="PrintBeforeAdvice"/>
聽聽聽 <bean id="myAdvisor"
聽聽聽聽聽聽聽聽聽 class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
聽聽聽聽聽 <property name="advice">
聽聽聽聽聽聽聽 <ref local="printBeforeAdvice"/>
聽聽聽聽聽 </property>
聽聽聽聽聽 <property name="pattern">
聽聽聽聽聽聽聽 <value>.*print.*</value>
聽聽聽聽聽 </property>
聽聽聽 </bean>
聽聽聽 <bean id="foo" class="org.springframework.aop.framework.ProxyFactoryBean">
聽聽聽聽聽 <property name="proxyInterfaces">
聽聽聽聽聽聽聽聽<value>FooInterface</value>
聽聽聽聽聽 </property>
聽聽聽聽聽 <property name="target">
聽聽聽聽聽聽聽 <ref local="fooTarget"/>
聽聽聽聽聽 </property>
聽聽聽聽聽 <property name="interceptorNames">
聽聽聽聽聽聽聽 <list>
聽聽聽聽聽聽聽聽聽 <value>myAdvisor</value>
聽聽聽聽聽聽聽 </list>
聽聽聽聽聽聽</property>
聽聽聽 </bean>
</beans>
聽
聽聽聽
涓葷▼搴忎笉闇榪涜浠諱綍淇敼錛岃繍琛岀粨鏋滃凡緇忓彉鏍蜂簡
聽聽 In PrintBeforeAdvice
聽聽聽 In FooImpl.printFoo
聽聽聽 In FooImpl.dummyFoo
聽
聽聽
鑷蟲錛屽簲璇ュ凡緇忕悊瑙d簡
Spring
涓?/span>
AOP
鐨勪嬌鐢ㄦ柟娉曪紝褰撶劧
Spring
涓?/span>
AOP
鏈閲嶈鐨勫簲鐢ㄦ槸
Transaction Manager
錛屼婦涓繖鏂歸潰鐨?/span>
applicationContext
渚嬪瓙鐪嬬湅錛?/span>
聽
聽 <?xml version="1.0" encoding="UTF-8"?>
聽 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd">
<beans>
聽聽聽 <bean id="propertyConfigurer"聽聽聽
聽聽聽聽聽聽聽聽 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
聽聽聽聽聽 <property name="location">
聽聽聽聽聽聽聽 <value>/WEB-INF/jdbc.properties</value>
聽聽聽聽聽聽</property>
聽聽 聽</bean>
聽聽聽 <bean id="dataSource"
聽聽聽聽聽聽聽聽聽 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
聽聽聽聽聽 <property name="driverClassName">
聽聽聽聽聽聽聽 <value>${jdbc.driverClassName}</value>
聽聽聽聽聽 </property>
聽聽聽聽聽聽<property name="url">
聽聽聽聽聽聽聽 <value>${jdbc.url}</value>
聽聽聽聽聽 </property>
聽聽聽聽聽 <property name="username">
聽聽聽聽聽聽聽 <value>${jdbc.username}</value>
聽聽聽聽聽 </property>
聽聽聽聽聽 <property name="password">
聽聽聽聽聽聽聽 <value>${jdbc.password}</value>
聽聽聽聽聽 </property>
聽聽聽 </bean>
聽聽 聽<bean id="sessionFactory"
聽聽聽聽聽聽聽聽聽 class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
聽聽聽聽聽 <property name="dataSource">
聽聽聽聽聽聽聽 <ref local="dataSource"/>
聽聽聽聽聽 </property>
聽聽聽聽聽 <property name="mappingResources">
聽聽聽聽聽聽聽 <value>smartmenu.hbm.xml</value>
聽聽聽聽聽 </property>
聽聽聽聽聽 <property name="hibernateProperties">
聽聽聽聽聽聽聽 <props>
聽聽聽聽聽聽聽聽聽 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
聽聽聽聽聽聽聽 </props>
聽聽聽聽聽 </property>
聽聽聽 </bean>
聽
聽聽聽 <bean id="transactionManager"聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽 class="org.springframework.orm.hibernate.HibernateTransactionManager">
聽聽聽聽聽 <property name="sessionFactory">
聽聽聽聽聽聽聽 <ref local="sessionFactory"/>
聽聽聽聽聽 </property>
聽聽聽 </bean>
聽聽聽 <bean id="smartmenuTarget" class="SmartMenuHibernate">
聽聽聽聽聽 <property name="sessionFactory">
聽聽聽聽聽聽聽 <ref local="sessionFactory"/>
聽聽聽聽聽 </property>
聽聽聽 </bean>
聽聽 聽<bean id="smartMenu"
聽聽聽聽聽聽聽 class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
聽聽聽聽聽 <property name="transactionManager">
聽聽聽聽聽聽聽 <ref local="transactionManager"/>
聽聽聽聽聽 </property>
聽聽聽聽聽 <property name="target">
聽聽聽聽聽聽聽 <ref local="smartmenuTarget"/>
聽聽聽聽聽 </property>
聽聽聽聽聽 <property name="transactionAttributes">
聽聽聽聽聽聽聽 <props>
聽聽聽聽聽聽聽聽聽 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
聽聽聽聽聽聽聽聽聽 <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
聽聽聽聽聽聽聽 </props>
聽聽聽聽聽 </property>
聽聽聽 </bean>
聽 </beans>
聽
聽
瑕佹兂褰誨簳鐞嗚В
Spring
鐨?/span>
AOP
錛屾渶濂借繕鏄鐪嬬湅婧愮爜錛屽紑婧愬氨鏄ソ鍟婏紒