spring-01java
一、spring的簡介
Spring 是一個開源框架,爲簡化企業級應用開發而生。Spring 可使簡單的 JavaBean 實現之前只有 EJB 才能實現的功能。Spring 是一個 IOC 和 AOP 容器框架。
IOC容器:控制反轉, 反轉控制, 反向控制
將對象的生命週期控制權利,交給Spring容器管理。原有開發方式,對象生命週期由程序員經過代碼控制.。如: new關鍵字. setter方法調用, xxx = null.
1)控制反轉中控制指的是:控制類的對象
2)控制反轉中反轉指的是轉交給 Spring 負責
3)IoC 最大的做用:解耦(程序員不須要管理對象.解除了對象管理和程序員之間的耦合)
二、Spring的DI:依賴注入
當一個類(A)中須要依賴另外一個類(B)對象時,把 B 賦值給 A 的過程就叫作依賴注入
三、spring的注入的種類和方式
1)構造方法注入(經過構造方法的參數,爲對象的屬性賦值)
2)setter注入(Spring容器針對bean對象的property實現屬性注入.面向getter方法和setter方法實現的屬性數據賦值. 每次爲屬性賦值,調用的是setXxxx方法)
3)靜態工廠的方法注入
4)實例工廠的方法注入程序員
1 //字符串注入 2 private String msg; 3 //int類型注入 4 private int flag; 5 //String類型數組注入 6 private String[] array; 7 //int類型數組注入 8 private int[] array2; 9 //list注入 10 private List<String> list; 11 //其餘對象類型 12 private List<UserDao> uds; 13 //set 14 private Set<String> sets; 15 //map 16 private Map<String, String> maps; 17 //Properties 18 private Properties prop; 19 20 public Properties getProp() { 21 return prop; 22 } 23 24 public void setProp(Properties prop) { 25 Set<Object> keys = prop.keySet(); 26 for (Object obj : keys) { 27 String key = (String)obj; 28 System.out.println(key + " " +prop.getProperty(key)); 29 } 30 this.prop = prop; 31 } 32 33 public Map<String, String> getMaps() { 34 return maps; 35 } 36 37 public void setMaps(Map<String, String> maps) { 38 Set<String> keys = maps.keySet(); 39 for (String key : keys) { 40 System.out.println(key+" "+ maps.get(key)); 41 } 42 this.maps = maps; 43 } 44 45 public Set<String> getSets() { 46 return sets; 47 } 48 49 public void setSets(Set<String> sets) { 50 for (String set : sets) { 51 System.out.println(set); 52 } 53 this.sets = sets; 54 } 55 56 public List<UserDao> getUds() { 57 return uds; 58 } 59 60 public void setUds(List<UserDao> uds) { 61 for (UserDao ud : uds) { 62 System.out.println(ud); 63 } 64 this.uds = uds; 65 } 66 67 public List<String> getList() { 68 return list; 69 } 70 71 public void setList(List<String> list) { 72 for (String lists : list) { 73 System.out.println(lists); 74 } 75 this.list = list; 76 } 77 78 public int[] getArray2() { 79 return array2; 80 } 81 82 public void setArray2(int[] array2) { 83 for (int i : array2) { 84 System.out.println(i); 85 } 86 this.array2 = array2; 87 } 88 89 public String[] getArray() { 90 return array; 91 } 92 93 public void setArray(String[] array) { 94 for (String arrays : array) { 95 System.out.println(arrays); 96 } 97 this.array = array; 98 } 99 100 public int getFlag() { 101 return flag; 102 } 103 104 public void setFlag(int flag) { 105 System.out.println(flag); 106 this.flag = flag; 107 } 108 109 public String getMsg() { 110 return msg; 111 } 112 113 public void setMsg(String msg) { 114 System.out.println(msg); 115 this.msg = msg; 116 }
1 <!-- 屬性注入用property標籤 value值能夠是String + 八種基本數據類型--> 2 <property name="msg" value="字符串注入"></property> 3 <property name="flag" value="6666"></property> 4 <property name="array"> 5 <array> 6 <value>dilraba</value> 7 <value>tom</value> 8 <value>java</value> 9 </array> 10 </property> 11 <property name="array2"> 12 <array> 13 <value>111</value> 14 <value>222</value> 15 <value>333</value> 16 </array> 17 </property> 18 <property name="list"> 19 <list> 20 <value>fasdfas</value> 21 <value>fdfafgf</value> 22 </list> 23 </property> 24 <property name="uds"> 25 <list> 26 <bean class="com.boom.dao.impl.UserDaoImpl"></bean> 27 </list> 28 </property> 29 <property name="sets"> 30 <set> 31 <value>sets1</value> 32 <value>sets2</value> 33 </set> 34 </property> 35 <property name="maps"> 36 <map> 37 <entry key="key1"> 38 <value>value1</value> 39 </entry> 40 41 <entry key="key2"> 42 <value>value2</value> 43 </entry> 44 </map> 45 </property> 46 <property name="prop"> 47 <props> 48 <prop key="prop1">prop11</prop> 49 <prop key="prop2">prop22</prop> 50 </props> 51 </property></bean>
屬性注入小結(必需要有set方法)
屬性注入經過property標籤,注入String+八種基本數據類型用value標籤,注入的是IOC容器本身的對象用ref,若是注入實體類對象,用bean標籤緩存一下。
Spring IOC注入方式用得最多的是(1)(2)種,經過Spring建立的對象默認是單例,若是須要建立多實例對象能夠在<bean>標籤後面添加一個屬性:=spring
<bean name="..." class="..." scope="prototype">
四、面向切面編程(AOP)
在面向對象編程(oop)思想中,咱們將事物縱向抽成一個個的對象。而在面向切面編程中,咱們將一個個的對象某些相似的方面橫向抽成一個切面,對這個切面進行一些如權限控制、事物管理,記錄日誌等公用操做處理的過程就是面向切面編程的思想。AOP 底層是動態代理,若是是接口採用 JDK 動態代理,若是是類採用CGLIB 方式實現動態代理。
一些俗語的理解:
切面(aspect):動態代理對象
鏈接點(joinpoint):AOP能夠是方法,屬性,構造方法,均可以攔截。spring中僅支持對方法的攔截
切點(pointcut):動態代理攔截的目標,如動態代理的invoke
通知(advice):前置,後置,環繞
Spring 切面能夠應用五種類型的通知
1)方法的前置通知(before)
實現接口:MethodBeforeAdvice
功能:在目標方法(被代理的對象的方法)調用前,作什麼
方法:void before(Method method, Object[] args, Object proxy)
主要應用於:記錄日誌 | 開啓事務 | 其餘功能處理
2)方法的後置通知(after / after-returning)
after:在方法執行以後調用的通知,不管方法執行是否成功。
after-returning:僅當方法成功完成後執行的通知
3)方法異常通知(after-throwing)
實現接口:ThrowsAdvice
功能:在方法拋出異常退出時執行的通知
方法:void afterThrowing(RuntimeException e)
4)方法環繞通知(around)
實現接口:MethodInterceptor
方法:Object invoke(MethodInvocation arg0) throws Throwable
功能:在方法執行以前和以後調用的通知express
目標(target):被代理的對象
織入(weave):目標和攔截對象捏合到一塊兒的過程
springAOP:面向切面編程,是將縱向執行的流程。進行橫向的切面,增長額外的功能。
切入的設計目標:使程序進行插拔式的開發,切的controller和service層之間。
代理模式:爲真正處理業務的對象,提供附屬功能的類型,稱爲代理。在不改變真正處理業務對象的代碼前提下,爲方法邏輯。增長新的功能的方式。
靜態代理:代理對象專門爲某一個對象或類型,提供針對性的服務支撐。功能全面強大,針對性強。缺陷是通用性下降。
動態代理:代理對象爲某一類事務提供輔助功能支撐。通用性高,針對性不足。
靜態代理代碼實現
①定義接口:租賃接口,誰實現了這個接口,表明具備租賃能力,對外出租給第三方編程
1 package com.boom.staticproxy; 2 /** 3 * 租賃接口,誰實現了這個接口,表明具備租賃能力,對外出租給第三方 4 * @project_name proxy 5 * @class_name Rent 6 * @author Dilraba 7 */ 8 public interface Rent { 9 //定義一個房源 10 public void rent(); 11 12 }
②業主,房屋的持有者,實現了租賃接口數組
1 package com.boom.staticproxy; 2 /** 3 * 業主, 房屋的持有者 4 * @project_name proxy 5 * @class_name Host 6 * @author Dilraba 7 */ 8 public class Host implements Rent{ 9 10 //出租的具體詳情 11 @Override 12 public void rent() { 13 System.out.println("籤合同,交錢..."); 14 } 15 16 17 }
③代理對象,俗稱二手房東等緩存
1 package com.boom.staticproxy; 2 /** 3 * 代理對象 4 * @project_name proxy 5 * @class_name ProxyObject 6 * @author Dilraba 7 */ 8 public class ProxyObject implements Rent { 9 10 private Host host; 11 12 public ProxyObject() { 13 } 14 public ProxyObject(Host host) { 15 this.host = host; 16 } 17 18 @Override 19 public void rent() { 20 System.out.println("帶顧客看房子...."); 21 host.rent(); 22 System.out.println("後期的一些亂七八糟事情。。。。"); 23 } 24 25 }
④模擬客戶租房子框架
1 package com.boom.staticproxy; 2 /** 3 * 模擬客戶租房子 4 * @project_name proxy 5 * @class_name StaticProxy 6 * @author Dilraba 7 */ 8 public class TestProxy { 9 public static void main(String[] args) { 10 //建立客戶 11 Host t = new Host(); 12 //建立代理對象(二手房東) 13 ProxyObject po = new ProxyObject(t); 14 po.rent(); 15 } 16 }
靜態代理代碼實現
①定義接口ide
1 package com.boom.dynamicproxy; 2 /** 3 * 租賃接口,誰實現了這個接口,表明具備租賃能力,對外出租給第三方 4 * @project_name proxy 5 * @class_name Rent 6 * @author Dilraba 7 */ 8 public interface Rent { 9 //定義一個房源 10 public void rent(); 11 12 }
②實現接口oop
1 package com.boom.dynamicproxy; 2 /** 3 * 業主, 房屋的持有者 4 * @project_name proxy 5 * @class_name Host 6 * @author Dilraba 7 */ 8 public class Host implements Rent{ 9 10 //出租的具體詳情 11 @Override 12 public void rent() { 13 System.out.println("籤合同,交錢..."); 14 } 15 16 17 }
③動態代理:實現了InvocationHandler調用其invoke方法
1 package com.boom.dynamicproxy; 2 3 import java.lang.reflect.InvocationHandler; 4 import java.lang.reflect.Method; 5 6 /** 7 * 代理對象 8 * @project_name proxy 9 * @class_name DynamicProxy 10 * @author Dilraba 11 */ 12 public class DynamicProxy implements InvocationHandler { 13 14 private Object obj; 15 public DynamicProxy() { 16 17 } 18 19 public DynamicProxy(Object obj) { 20 super(); 21 this.obj = obj; 22 } 23 24 /** 25 * @param proxy - 代理對象 26 * @param method - 真實的方法, 就是要調用的最終方法. 業主出租訪問的方法. 27 * @param args - 真實方法要執行的時候,須要的參數. 28 */ 29 @Override 30 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 31 System.out.println("Advice...Before"); 32 System.out.println(proxy.getClass().getName()); 33 Object obj = method.invoke(this.obj, args); 34 System.out.println("Advice...After"); 35 return obj; 36 } 37 38 }
④測試
1 package com.boom.dynamicproxy; 2 3 import java.lang.reflect.Proxy; 4 5 /** 6 * 7 * @project_name proxy 8 * @class_name TestProxy 9 * @author Dilraba 10 */ 11 public class TestProxy { 12 public static void main(String[] args) { 13 Host host = new Host(); 14 DynamicProxy dp = new DynamicProxy(host); 15 Rent rent = (Rent)Proxy.newProxyInstance(host.getClass().getClassLoader(), host.getClass().getInterfaces(), dp); 16 System.out.println(rent);//就是代理對象:com.boom.dynamicproxy.Host@6bc7c054 17 rent.rent(); 18 } 19 }
五、Spring中AOP的開發四種方式(Spring中對於 AOP配置底層基於動態代理)
xml方式配置:在spring任意版本中均可以使用,缺:只能配置一個業務層,若業務層繁多作切面配置時候,配置信息繁瑣,不便於管理。
①springAOP-xml.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- XSD約束的XML配置文件 --> 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd"> 7 <!-- 目標 --> 8 <bean id="userService" class="com.boom.xml.service.impl.UserServiceImpl"></bean> 9 <!-- 配置切面 --> 10 <bean id="myAOP" class="com.boom.xml.aop.MyAOP"></bean> 11 12 <bean id="userServieAOP" class="org.springframework.aop.framework.ProxyFactoryBean"> 13 <property name="target"> 14 <ref bean="userService"/> 15 </property> 16 <property name="proxyInterfaces"> 17 <value>com.boom.xml.service.UserService</value> 18 </property> 19 <property name="interceptorNames"> 20 <list> 21 <value>myAOP</value> 22 </list> 23 </property> 24 </bean> 25 </beans>
②接口和實現類
1 package com.boom.xml.service; 2 3 public interface UserService { 4 5 void addUser(); 6 }
1 package com.boom.xml.service.impl; 2 3 import com.boom.xml.service.UserService; 4 5 public class UserServiceImpl implements UserService { 6 7 @Override 8 public void addUser() { 9 System.out.println("UserService..."); 10 } 11 12 }
③AOP
1 package com.boom.xml.aop; 2 3 import java.lang.reflect.Method; 4 import java.lang.reflect.Proxy; 5 6 import org.aopalliance.intercept.MethodInterceptor; 7 import org.aopalliance.intercept.MethodInvocation; 8 import org.springframework.aop.AfterReturningAdvice; 9 import org.springframework.aop.MethodBeforeAdvice; 10 import org.springframework.aop.framework.ProxyFactoryBean; 11 12 public class MyAOP implements MethodBeforeAdvice, AfterReturningAdvice, MethodInterceptor { 13 14 @Override 15 public Object invoke(MethodInvocation arg0) throws Throwable { 16 System.out.println("AOP聯盟...before"); 17 Object obj = arg0.proceed(); 18 System.out.println("AOP聯盟...after"); 19 return obj; 20 } 21 22 @Override 23 public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { 24 System.out.println("Spring...after"); 25 26 } 27 28 @Override 29 public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { 30 System.out.println("Spring...before"); 31 } 32 33 }
④測試類
1 package com.boom.xml.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.boom.xml.service.UserService; 7 8 9 public class Test { 10 public static void main(String[] args) { 11 //啓動spring框架 12 ApplicationContext ac = new ClassPathXmlApplicationContext("springAOP-xml.xml"); 13 UserService us = (UserService)ac.getBean("userServieAOP"); 14 us.addUser(); 15 16 } 17 }
⑤運行結果
spring aspect:在配置文件裏配置<aop:config>,<aop:advisor>配置前置後置環繞等通知。
①springAOP-springaspect.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- XSD約束的XML配置文件 --> 3 <!-- XSD約束的XML配置文件 4 xmlns - xml namespace 命名空間, 默認命名空間.表明定義標籤不須要寫前綴. 5 xmlns:xxx - 定義命名空間xxx. 使用標籤爲: <xxx:xxx> 6 --> 7 <beans xmlns="http://www.springframework.org/schema/beans" 8 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 9 xmlns:aop="http://www.springframework.org/schema/aop" 10 xmlns:tx="http://www.springframework.org/schema/tx" 11 xsi:schemaLocation="http://www.springframework.org/schema/beans 12 http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop.xsd 15 http://www.springframework.org/schema/tx 16 http://www.springframework.org/schema/tx/spring-tx.xsd"> 17 <!-- 目標 --> 18 <bean id="userService" class="com.boom.springaspect.service.impl.UserServiceImpl"></bean> 19 <!-- 配置切面 --> 20 <bean id="MyAfterAdvice" class="com.boom.springaspect.aop.MyAfterAdvice"></bean> 21 <bean id="MyAroundAdvice" class="com.boom.springaspect.aop.MyAroundAdvice"></bean> 22 <bean id="MyBeforeAdvice" class="com.boom.springaspect.aop.MyBeforeAdvice"></bean> 23 <bean id="MyThrowsAdvice" class="com.boom.springaspect.aop.MyThrowsAdvice"></bean> 24 25 <aop:config> 26 <aop:pointcut expression="execution(* com.boom.springaspect.service.*.*(..))" id="mypointcut"/> 27 <aop:advisor advice-ref="MyBeforeAdvice" pointcut-ref="mypointcut"/> 28 <aop:advisor advice-ref="MyAfterAdvice" pointcut-ref="mypointcut"/> 29 <aop:advisor advice-ref="MyAroundAdvice" pointcut-ref="mypointcut"/> 30 <aop:advisor advice-ref="MyThrowsAdvice" pointcut-ref="mypointcut"/> 31 </aop:config> 32 </beans>
②接口和實現類
1 package com.boom.springaspect.service; 2 3 public interface UserService { 4 5 String addUser(); 6 }
1 package com.boom.springaspect.service.impl; 2 3 import com.boom.springaspect.service.UserService; 4 5 public class UserServiceImpl implements UserService { 6 7 public String addUser() { 8 System.out.println("addUser.........."); 9 return "aaa"; 10 } 11 12 }
③AOP
1 package com.boom.springaspect.aop; 2 3 import java.lang.reflect.Method; 4 5 import org.springframework.aop.MethodBeforeAdvice; 6 7 public class MyBeforeAdvice implements MethodBeforeAdvice { 8 9 @Override 10 public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { 11 System.out.println("Before......."); 12 } 13 14 }
1 package com.boom.springaspect.aop; 2 3 import java.lang.reflect.Method; 4 5 import org.springframework.aop.AfterReturningAdvice; 6 7 public class MyAfterAdvice implements AfterReturningAdvice { 8 9 @Override 10 public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { 11 System.out.println("After...."); 12 } 13 14 }
1 package com.boom.springaspect.aop; 2 3 import org.aopalliance.intercept.MethodInterceptor; 4 import org.aopalliance.intercept.MethodInvocation; 5 6 public class MyAroundAdvice implements MethodInterceptor { 7 8 @Override 9 public Object invoke(MethodInvocation arg0) throws Throwable { 10 System.out.println("AOP......Before"); 11 Object obj = arg0.proceed(); 12 System.out.println("AOP.......After"); 13 return obj; 14 } 15 16 }
1 package com.boom.springaspect.aop; 2 3 import org.springframework.aop.ThrowsAdvice; 4 5 public class MyThrowsAdvice implements ThrowsAdvice { 6 7 public void afterThrowing(RuntimeException e){ 8 System.out.println(e.getClass().getName()); 9 } 10 }
④測試
1 package com.boom.springaspect.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.boom.springaspect.service.UserService; 7 8 9 public class Test { 10 11 public static void main(String[] args) { 12 ApplicationContext ac = new ClassPathXmlApplicationContext("springAOP-springaspect.xml"); 13 UserService us = (UserService)ac.getBean("userService"); 14 us.addUser(); 15 } 16 17 }
⑤運行結果
spring aspectj 方式:(spring 2.0之後引入)是專門作springAOP一個框架/技術,一開始並非spring的,後來被spring收購了。AOP比xml更加通 用,簡化。AspectJ不屬於spring的標準技術,因此切面不須要實現spring中的通知接口。AspctJ的特色是經過方法+配置文件來定義aop
①springAOP-aspectj.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- XSD約束的XML配置文件 --> 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd"> 13 <!-- 目標 --> 14 <bean id="userService" class="com.boom.aspectj.service.impl.UserServiceImpl"></bean> 15 <!-- 配置切面 --> 16 <bean id="myAOP" class="com.boom.aspectj.aop.MyAOPAspectj"></bean> 17 <!-- 配置切點 --> 18 <aop:config> 19 <!-- 指定切面 --> 20 <aop:aspect id="myaop" ref="myAOP"> 21 <!-- 配置通知行爲 --> 22 <aop:before method="doBefore" pointcut="execution( * com.boom.aspectj.service.*.*(..))"/> 23 <aop:after-returning method="doAfter" returning="obj" pointcut="execution( * com.boom.aspectj.service.*.*(..))" /> 24 <aop:after-throwing method="doAfterThrowing" throwing="ex" pointcut="execution( * com.boom.aspectj.service.*.*(..))"/> 25 <aop:around method="doAround" pointcut="execution( * com.boom.aspectj.service.*.*(..))"/> 26 </aop:aspect> 27 </aop:config> 28 </beans>
②接口和實現類
1 package com.boom.aspectj.service; 2 3 public interface UserService { 4 5 void addUser(); 6 }
1 package com.boom.aspectj.service.impl; 2 3 import com.boom.aspectj.service.UserService; 4 5 public class UserServiceImpl implements UserService { 6 7 @Override 8 public void addUser() { 9 System.out.println("UserService..."); 10 } 11 12 }
③AOP
1 package com.boom.aspectj.aop; 2 3 import org.aspectj.lang.ProceedingJoinPoint; 4 /** 5 * AspectJ不屬於spring的標準技術,因此切面不須要實現spring中的通知接口 6 * AspctJ的特色是經過方法+配置文件來定義aop 7 * @project_name springAOP 8 * @class_name MyAOPAspectj 9 * @author Dilraba 10 */ 11 public class MyAOPAspectj { 12 public void doBefore(){ 13 System.out.println("Before..."); 14 } 15 public void doAfter(Object obj){ 16 System.out.println("After..."); 17 } 18 public void doAfterThrowing(RuntimeException ex){ 19 System.out.println(ex.getClass().getName()); 20 System.out.println("doAfterThrowing..."); 21 } 22 public void doAround(ProceedingJoinPoint joinPoint) throws Throwable{ 23 System.out.println("doAround...before"); 24 joinPoint.proceed(); 25 System.out.println("doAround...after"); 26 } 27 }
④測試
1 package com.boom.aspectj.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.boom.aspectj.service.UserService; 7 8 9 public class Test { 10 11 public static void main(String[] args) { 12 ApplicationContext ac = new ClassPathXmlApplicationContext("springAOP-aspectj.xml"); 13 UserService us = (UserService)ac.getBean("userService"); 14 us.addUser(); 15 16 } 17 18 }
⑤運行結果
spring annotation:須要使用Spring的註解和AspectJ的註解(註解就是)
①springAOP-annotation.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- XSD約束的XML配置文件 --> 3 <!-- XSD約束的XML配置文件 4 xmlns - xml namespace 命名空間, 默認命名空間.表明定義標籤不須要寫前綴. 5 xmlns:xxx - 定義命名空間xxx. 使用標籤爲: <xxx:xxx> 6 --> 7 <beans xmlns="http://www.springframework.org/schema/beans" 8 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 9 xmlns:context="http://www.springframework.org/schema/context" 10 xsi:schemaLocation="http://www.springframework.org/schema/beans 11 http://www.springframework.org/schema/beans/spring-beans.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 14 http://www.springframework.org/schema/context 15 http://www.springframework.org/schema/context/spring-context.xsd"> 16 17 <context:component-scan base-package="com.boom.annotation"></context:component-scan> 18 <!-- 開啓註解 --> 19 <context:annotation-config /> 20 <!-- 開啓aspectj代理 --> 21 <aop:aspectj-autoproxy /> 22 </beans>
②接口和實現類
1 package com.boom.annotation.service; 2 3 public interface UserService { 4 void addUser(); 5 }
1 package com.boom.annotation.service.impl; 2 3 import org.springframework.stereotype.Service; 4 5 import com.boom.annotation.service.UserService; 6 7 @Service 8 public class UserServiceImpl implements UserService { 9 10 @Override 11 public void addUser() { 12 System.out.println("addUser........"); 13 14 } 15 16 }
③AOP
1 package com.boom.annotation.aop; 2 3 import org.aspectj.lang.ProceedingJoinPoint; 4 import org.aspectj.lang.annotation.After; 5 import org.aspectj.lang.annotation.Around; 6 import org.aspectj.lang.annotation.Aspect; 7 import org.aspectj.lang.annotation.Before; 8 import org.aspectj.lang.annotation.Pointcut; 9 import org.springframework.stereotype.Component; 10 11 @Aspect 12 @Component 13 public class MyAOP { 14 15 @Pointcut("execution(* com.boom.annotation.service.*.*(..))") 16 public void suibian(){ 17 18 } 19 20 @Before("suibian()") 21 public void doBefore(){ 22 System.out.println("doBefore......"); 23 } 24 25 @After("suibian()") 26 public void doAfter(){ 27 System.out.println("doAfter"); 28 } 29 30 @Around("suibian()") 31 public void around(ProceedingJoinPoint joinPoint)throws Throwable{ 32 System.out.println("around.....before"); 33 joinPoint.proceed(); 34 System.out.println("around.....after"); 35 } 36 }
④測試
1 package com.boom.annotation.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.boom.annotation.service.UserService; 7 8 9 public class Test { 10 11 public static void main(String[] args) { 12 //spring啓動 13 ApplicationContext ac = new ClassPathXmlApplicationContext("springAOP-annotation.xml"); 14 UserService us = (UserService)ac.getBean(UserService.class); 15 us.addUser(); 16 } 17 18 }
⑤運行結果