锘??xml version="1.0" encoding="utf-8" standalone="yes"?>99久久免费精品国产72精品九九 ,国产伦精品一区二区三区照片,牛牛视频精品一区二区不卡http://www.aygfsteel.com/SunRiver/category/17334.htmlTopics about J2EE XML AJAX SOA DB etc.zh-cnWed, 28 Feb 2007 03:10:57 GMTWed, 28 Feb 2007 03:10:57 GMT60A simple use of Spring AOPhttp://www.aygfsteel.com/SunRiver/archive/2006/11/16/81508.htmlSun RiverSun RiverThu, 16 Nov 2006 05:29:00 GMThttp://www.aygfsteel.com/SunRiver/archive/2006/11/16/81508.htmlhttp://www.aygfsteel.com/SunRiver/comments/81508.htmlhttp://www.aygfsteel.com/SunRiver/archive/2006/11/16/81508.html#Feedback0http://www.aygfsteel.com/SunRiver/comments/commentRss/81508.htmlhttp://www.aygfsteel.com/SunRiver/services/trackbacks/81508.html Spring 涓殑 AOP 綆鍗曚嬌鐢?/span>

聽聽聽

聽聽聽 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 錛屾渶濂借繕鏄鐪嬬湅婧愮爜錛屽紑婧愬氨鏄ソ鍟婏紒



Sun River 2006-11-16 13:29 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 壶关县| 千阳县| 康平县| 巨野县| 东莞市| 怀来县| 芦溪县| 威海市| 阿合奇县| 孟州市| 双牌县| 砚山县| 平谷区| 东兴市| 淳化县| 苍梧县| 武冈市| 姜堰市| 邳州市| 庄河市| 镇雄县| 阳春市| 阜新市| 砚山县| 右玉县| 洛阳市| 云霄县| 泸州市| 高平市| 罗江县| 新沂市| 巩义市| 大关县| 商南县| 泽州县| 晋城| 三门县| 肇庆市| 濮阳市| 浠水县| 桐柏县|