標籤:code beans tran 通知 invoke pos bat aspect war
在某些類中, 什麼時機, 做什麼事情
切入點(point-cut): 在某些類中(Class<?>[] itfc = new Class<?>[] { IStudentService.class })
通知: 什麼時機, 做什麼事情(InvocationHandler的invoke方法)
切面: 切入點 + 通知
織入(weaver): Proxy.newProxyInstance: 把切入點和通知施加到具體的商務邏輯上的過程
XML配置AOP步驟:
1,準備了一個實現介面的bean, 還有一個事務的片段化方法的類;
2,把這兩個類配置到Spring容器中;
3,配置springAOP
1)引入aop命名空間;
2)配置AOP,格式如下
<aop:config>
<aop:point-cut expression="" id="sample" />
<aop:aspect ref="">
<aop:before method="" pointcut-ref="sample">
<aop:after-returning method="" pointcut-ref="sample">
<aop:after-throwing method="" pointcut-ref="sample">
</aop:aspect>
</aop:config>
1] aop:config: AOP配置
2] aop:point-cut: AOP切入點
3] aop:aspect: AOP切面配置
4] aop:* 都是一個AOP切面
4,在介面檔案中配置一個商務邏輯對象的介面,DI注入,並在test方法中直接調用該介面的具體商務邏輯
//介面:package com.xk.spring.kp04_aop.aop.s01_xml;public interface IStudentService { public void save(Student stu); public void update(Student stu);}
//Dao 實作類別(service) 實現商務邏輯package com.xk.spring.kp04_aop.aop.s01_xml;public class StudentServiceImpl implements IStudentService { @Override public void save(Student stu) { System.out.println("調用Dao的save方法...."); } @Override public void update(Student stu) { System.out.println("調用Dao的update方法..."); @SuppressWarnings("unused") int i = 10/0; }}
//事物管理:將代碼中的汙染源抽出一個類,專門處理事物package com.xk.spring.kp04_aop.aop.s01_xml;public class TransactionManager { /** * 事物開始 */ public void begin(){ System.out.println("TransactionManager.begin()"); } /** * 事物提交 */ public void commit(){ System.out.println("TransactionManager.commit()"); } /** * 事物復原 */ public void rollback(Throwable e){ System.out.println("TransactionManager.rollback()"); System.out.println("rollback()"); } /** * 事物結束 */ public void finished(){ System.out.println("TransactionManager.close()"); }}
//Bean//將bean注入就是指將資料注入Springpackage com.xk.spring.kp04_aop.aop.s01_xml;public class Student {private String name;private Integer age;public Student() {}public Student(String name, Integer age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}}
//測試類別package com.xk.spring.kp04_aop.aop.s01_xml;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfigurationpublic class AOPXMLTest { @Autowired IStudentService seriver; @Test public void testAOPXML() throws Exception { seriver.save(new Student("張三", 18)); seriver.update(new Student("張4", 18)); }}
<!-- xml配置aop --><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="service" class="com.xk.spring.kp04_aop.aop.s01_xml.StudentServiceImpl" /> <bean id="manager" class="com.xk.spring.kp04_aop.aop.s01_xml.TransactionManager" /> <aop:config> <!-- 需要匯入兩個依賴包 com.springsource.org.aopalliance-1.0.0.jar 和com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar --> <aop:pointcut expression="execution(* com.xk.spring.kp04_aop.aop.s01_xml.*Service.*(..))" id="stuService" /> <aop:aspect ref="manager"> <aop:before method="begin" pointcut-ref="stuService" /> <!-- 復原:在復原的時候 throwing是要在TransactionManager類中配置的 參數"(throwable e)" --> <aop:after-throwing method="rollback" pointcut-ref="stuService" throwing="e" /> </aop:aspect> </aop:config></beans><!-- 錯誤:Pointcut is not well-formed: expecting ‘name pattern‘ at character position 配置aop報錯:原因是配置切點運算式的時候報錯了, 星號後面沒有加空格: <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* project.mybatis.service.*.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="omsMTransactionAdvice" /> </aop:config> 其中,切入點運算式的使用規則: 1、execution(): 運算式主體。 2、第一個*號:表示傳回型別,*號表示所有的類型。 3、包名:表示需要攔截的包名,後面的兩個句點表示當前包和當前包的所有子包,com.sample.service.impl包、子孫包下所有類的方法。 4、第二個*號:表示類名,*號表示所有的類。 5、*(..):最後這個星號表示方法名,*號表示所有的方法,後面括弧裡面表示方法的參數,兩個句點表示任何參數。 -->
xml的方式配置AOP:Aspect Oriented Programming