javascript
Spring 从零開始-05
最終能到Spring的AOP編程了,AOP的概念特別的多。所以須要你在開始之前有點了解,然后通過代碼慢慢學習!
— 切面(Aspect):一個關注點的模塊化,這個關注點實現可能另外橫切多個對象。事務管理是J2EE應用中一個非常好的橫切關注點樣例。方面用Spring的Advisor或攔截器實現。
— 連接點(Joinpoint):程序運行過程中明白的點,如方法的調用或特定的異常被拋出。
— 通知(Advice):在特定的連接點,AOP框架運行的動作。
各種類型的通知包含“around”、“before”和“throws”通知。通知類型將在以下討論。
很多AOP框架包含Spring都是以攔截器做通知模型。維護一個“圍繞”連接點的攔截器鏈。
— 切入點(Pointcut):指定一個通知將被引發的一系列連接點的集合。AOP框架必須同意開發人員指定切入點。比如。使用正則表達式。
— 引入(Introduction):加入方法或字段到被通知的類。Spring同意引入新的接口到不論什么被通知的對象。
比如,你能夠使用一個引入使不論什么對象實現IsModified接口,來簡化緩存。
— 目標對象(Target Object):包含連接點的對象,也被稱作被通知或被代理對象。
— AOP代理(AOP Proxy):AOP框架創建的對象,包含通知。在Spring中。AOP代理能夠是JDK動態代理或CGLIB代理。
— 織入(Weaving):組裝方面來創建一個被通知對象。這能夠在編譯時完畢(比如使用AspectJ編譯器)。也能夠在運行時完畢。
Spring和其它純Java AOP框架一樣,在運行時完畢織入。
頭非常暈是不是,沒關系。先記住,然后說說為什么使用AOP編程,AOP-Aspect Oriented Programming面向切面編程,既然是編程,那么就說明Spring僅僅是AOP的一種實現方式。
舉個樣例,比方你買了房子是不是每一個月都要交電費,那你是不是這樣實現
public class House{基礎方法();交電費();交水費();防小偷();... }這樣買個房子的代碼竟然須要關心非常多其它方法,并且基本上與你正常的業務無關,假設哪天又添加了其它的功能,是不是又要改代碼。所以我們希望將多余的方法拿出,在須要的時候再織入。
Spring就給我們提供了這種功能。
Spring對AOP的支持能夠使用xml編寫、使用注解還有AspectJ(當然還有經典的使用代理,這個方案比較復雜所以就不說了)。
先說xml
上面的代碼就是我們須要織入的代碼
public interface TestInter {public void show();public void hh(); } public class TestService implements TestInter{private String name;@Overridepublic void show() {// TODO Auto-generated method stubSystem.out.println(name);}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic void hh() {// TODO Auto-generated method stub}} public class app1 {public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ac = new ClassPathXmlApplicationContext("com/aop/aop.xml");TestInter testservice = (TestInter) ac.getBean("testsetvice1");testservice.show();}} <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="testsetvice1" class="com.aop.TestService" ><property name="name"><value>spirit</value></property></bean><bean id="check" class="com.aop.Check"/><aop:config><aop:aspect id="as" ref="check"><aop:pointcut id="beforepoint" expression="execution(* com.aop.TestService.show(..))" /><aop:before method="CheckSecurity" pointcut-ref="beforepoint" /></aop:aspect> </aop:config> </beans>xml中定義須要在中完畢配置,aop:aspect定義的是切面也就是我們的check,aop:pointcut定義切入點,表達式expression=”execution(* com.aop.TestService.show(..))”,注意*后面有個一空格,show()中的..表示接受不論什么參數,aop:before定義的是前置通知。也就是在切入點之前織入通知,還有aop:after后置通知,aop:around圍繞通知,aop:after-throwing當拋出異常時插入通知和aop:after-returning返回時織入。
當中:After advice :當某連接點退出的時候運行的通知(不論是正常返回還是異常退出)。ApplicationContext中在里面使用元素進行聲明。
After return advice:在某連接點正常完畢后運行的通知,不包含拋出異常的情況。
ApplicationContext中在里面使用元素進行聲明。
單獨說一下圍繞通知。須要加入參數ProceedingJoinPoint,并運行proceed()方法。表示圍繞通知的方法運行。
如今我們看到的通知都是沒有參數,假設須要加入參數,以CheckSecurity為例。
public void CheckSecurity(String value){System.out.println("----checkSecurity----"); // System.out.println("---check method arg "+name+"---");} <aop:config><aop:aspect id="as" ref="check"><aop:pointcut id="beforepoint" expression="execution(* com.aop.TestService.show(String)) and args(trouble)" /><aop:before method="CheckSecurity" arg-names="trouble" pointcut-ref="beforepoint" /><aop:around method="round" pointcut-ref="beforepoint" /></aop:aspect> </aop:config> public class TestService implements TestInter{private String name;@Overridepublic void show(String trouble) {// TODO Auto-generated method stubSystem.out.println(name);}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic void hh() {// TODO Auto-generated method stubSystem.out.println("hh");}}使用切面還能引入新的功能。
types-matching表示的是原實現類,記得加+,default-impl是接口的實現。implement-interface是新實現類接口定義
以下說一下注解裝配
注解裝配主要使用的是Aspect的語法。
@Aspect表示注入的切面,@Pointcut注解表示切點。表達式與xml的方法一致,@Before表示前置通知還有其它如同xml的元素。括號里表示切點,切點已經由@Pointcut定義。
public class test {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("com/annotation/bean.xml");User user = (User) ac.getBean("user");user.show();} } public class User {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void show(){System.out.println("name is "+this.name);} }在xml中加入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy/> <bean id="user" class="com.annotation.User"> <property name="name"> <value>spirit</value> </property> <property name="age"> <value>23</value> </property> </bean> <bean id="check" class="com.annotation.check"/> </beans>假設注入須要參數的話
@Pointcut("execution(* com.annotation.User.show(String,String)) and args(testArg1,testArg2)")private void showMethod(String testArg1,String testArg2){}@Before("showMethod(testArg1,testArg2)")public void beforeShow(String testArg1,String testArg2){System.out.println("before show ");}好了差點兒相同就這樣了,老規矩上代碼。
http://download.csdn.net/detail/wsrspirit/8870455
總結
以上是生活随笔為你收集整理的Spring 从零開始-05的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTTP 错误 403.14 - For
- 下一篇: Android Studio打包APK时