在某些類中, 什么時機, 做什么事情
?切入點(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 {@Overridepublic void save(Student stu) {System.out.println("調用Dao的save方法....");}@Overridepublic 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注入就是指將數據注入Spring
package 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) @ContextConfiguration public class AOPXMLTest {@AutowiredIStudentService seriver;@Testpublic 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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://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:pointcutexpression="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、*(..):最后這個星號表示方法名,*號表示所有的方法,后面括弧里面表示方法的參數,兩個句點表示任何參數。 -->
?