xml的方式配置AOP:Aspect Oriented Programming
在某些類中, 什么時(shí)機(jī), 做什么事情
?切入點(diǎn)(point-cut): 在某些類中(Class<?>[] itfc = new Class<?>[] { IStudentService.class })
?通知: 什么時(shí)機(jī), 做什么事情(InvocationHandler的invoke方法)
?切面: 切入點(diǎn) + 通知
?織入(weaver): Proxy.newProxyInstance: 把切入點(diǎn)和通知施加到具體的業(yè)務(wù)邏輯上的過程
XML配置AOP步驟:
?1,準(zhǔn)備了一個(gè)實(shí)現(xiàn)接口的bean, 還有一個(gè)事務(wù)的碎片化方法的類;
?2,把這兩個(gè)類配置到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切入點(diǎn)
? ?3] aop:aspect: AOP切面配置
? ?4] aop:* 都是一個(gè)AOP切面
?4,在接口文件中配置一個(gè)業(yè)務(wù)邏輯對象的接口,DI注入,并在test方法中直接調(diào)用該接口的具體業(yè)務(wù)邏輯
//接口: package com.xk.spring.kp04_aop.aop.s01_xml;public interface IStudentService {public void save(Student stu);public void update(Student stu); } //Dao 實(shí)現(xiàn)類(service) 實(shí)現(xiàn)業(yè)務(wù)邏輯 package com.xk.spring.kp04_aop.aop.s01_xml;public class StudentServiceImpl implements IStudentService {@Overridepublic void save(Student stu) {System.out.println("調(diào)用Dao的save方法....");}@Overridepublic void update(Student stu) {System.out.println("調(diào)用Dao的update方法...");@SuppressWarnings("unused")int i = 10/0;} } //事物管理:將代碼中的污染源抽出一個(gè)類,專門處理事物 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()");}/*** 事物結(jié)束*/public void finished(){System.out.println("TransactionManager.close()");} } //Bean //將bean注入就是指將數(shù)據(jù)注入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><!-- 需要導(dǎo)入兩個(gè)依賴包 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" /><!-- 回滾:在回滾的時(shí)候 throwing是要在TransactionManager類中配置的 參數(shù)"(throwable e)" --><aop:after-throwing method="rollback"pointcut-ref="stuService" throwing="e" /></aop:aspect></aop:config> </beans><!-- 錯(cuò)誤:Pointcut is not well-formed: expecting 'name pattern' at character position 配置aop報(bào)錯(cuò):原因是配置切點(diǎn)表達(dá)式的時(shí)候報(bào)錯(cuò)了, 星號后面沒有加空格: <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* project.mybatis.service.*.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="omsMTransactionAdvice" /> </aop:config> 其中,切入點(diǎn)表達(dá)式的使用規(guī)則: 1、execution(): 表達(dá)式主體。 2、第一個(gè)*號:表示返回類型,*號表示所有的類型。 3、包名:表示需要攔截的包名,后面的兩個(gè)句點(diǎn)表示當(dāng)前包和當(dāng)前包的所有子包,com.sample.service.impl包、子孫包下所有類的方法。 4、第二個(gè)*號:表示類名,*號表示所有的類。 5、*(..):最后這個(gè)星號表示方法名,*號表示所有的方法,后面括弧里面表示方法的參數(shù),兩個(gè)句點(diǎn)表示任何參數(shù)。 -->
?
轉(zhuǎn)載于:https://www.cnblogs.com/huike/p/6636731.html
總結(jié)
以上是生活随笔為你收集整理的xml的方式配置AOP:Aspect Oriented Programming的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeakCanary——直白的展现And
- 下一篇: Autofac IoC容器基本使用步骤【