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