閱讀695 返回首頁    go 阿裏雲 go 技術社區[雲棲]


第三章 AOP 基於Schema的AOP

        基於Schema定義的切麵和前現兩種方式定義的切麵,內容上都差不多,隻是表現形式不一樣而已。


3.7.1一般增強的使用


a、目標類
public class Target {
	public void say(){
		System.out.println("say...");
	}
	
	public String getName(int id,String name){
		System.out.println("getName...");
		return "MR"+name+id;
	}
	
	public void around(){
		System.out.println("around...");
	}
	
	public void targetThrow(){
		System.out.println("targetThrow");
		throw new RuntimeException("我是一個運行期異常...");
	}
}


b、POJO(增強所在的類)
public class Pojo {
	public void before() {
		System.out.println("前置增強");
	}

	public void afterReturning(String retName, int id, String name) {
		System.out.println("後置增強,返回值為:"+retName+" 入參為:"+id+"-"+name);
	}
	
	public Object around(ProceedingJoinPoint point) throws Throwable{
		System.out.println("方法執行之前");
		Object object = point.proceed();
		System.out.println("方法執行之後");
		return object;
	}
	
	public void throwEx(Exception ex){
		System.out.println("拋出異常增強,異常信息:"+ex.getMessage());
	}
	
	public void finalEx(){
		System.out.println("Final增強");
	}
}


c、aop命名空間與Schema方式配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p"
	xmlns:context="https://www.springframework.org/schema/context"
	xmlns:aop="https://www.springframework.org/schema/aop"
	xsi:schemaLocation="https://www.springframework.org/schema/beans 
		https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		https://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context-3.0.xsd
        https://www.springframework.org/schema/aop 
        https://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<!-- 目標類 -->
	<bean  ></bean>
	
	<!-- 增強所在的類 -->
	<bean  ></bean>
	
	<!-- 配置基於schema的切麵 -->
	<aop:config proxy-target->
		<!-- 確定增強所在類,並引入 -->
		<aop:aspect ref="advisor">
			<!—
                          前置增強 
                          method是配置增強所在類中的方法
                        -->
			<aop:before method="before" pointcut="target(cn.framelife.spring.schema.Target)"/>
		
			<!—
                          後置增強 
                          Returning 是返回值,必須和method中的參數名是一樣的
                          在Schema配置中,多個切點函數的與操作是and,或操作是or
                        -->
			<aop:after-returning method="afterReturning" pointcut="execution(* cn.framelife.spring.schema..getName(..)) and args(id,name)" returning="retName" arg-names="retName,id,name"/>
		
			<!-- 環繞增強 -->
			<aop:around method="around" pointcut="execution(* cn.framelife.spring.schema..around(..))"/>
		
			<!—
                         拋出異常增強 
                         throwing是異常對象,必須和method中的參數是一樣的
                        -->
			<aop:after-throwing method="throwEx" pointcut="execution(* cn.framelife.spring.schema..targetThrow(..))" throwing="ex"/>
			
			<!-- Final增強 -->
			<aop:after method="finalEx" pointcut="execution(* cn.framelife.spring.schema..targetThrow(..))"/>
	</aop:config>
</beans>

d、測試

ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		Target target = (Target) context.getBean("target");
		target.say();
		target.getName(10, "Zhang");
		target.around();
		target.targetThrow();

e、結果

前置增強
say...
前置增強
getName...
後置增強,返回值為:MRZhang10 入參為:10-Zhang
前置增強
方法執行之前
around...
方法執行之後
前置增強
targetThrow
拋出異常增強,異常信息:我是一個運行期異常...
Final增強
Exception in thread "main" java.lang.RuntimeException: 我是一個運行期異常...

3.7.2引介增強的使用


我們還是使用3.6.2@DeclareParents中的例子:Waiter為目標類,然後讓目標類擁有ISeller接口的功能:

https://blog.csdn.net/p_3er/article/details/9269407



a、兩個接口與兩個類


目標類與其接口:

  1. public interface IWaiter {  
  2.     public void service();  
  3. }  

  1. @Component  
  2. public class Waiter implements IWaiter {  
  3.     @Override  
  4.     public void service() {  
  5.         System.out.println("service");  
  6.     }  
  7. }  

運行期織入到目標類的功能類與其接口:

  1. public interface ISeller {  
  2.     public void sell();  
  3. }  

  1. public class Seller implements ISeller {  
  2.     @Override  
  3.     public void sell() {  
  4.         System.out.println("sell");  
  5.     }  
  6. }  

b、配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p"
	xmlns:context="https://www.springframework.org/schema/context"
	xmlns:aop="https://www.springframework.org/schema/aop"
	xsi:schemaLocation="https://www.springframework.org/schema/beans 
		https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		https://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context-3.0.xsd
        https://www.springframework.org/schema/aop 
        https://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<!-- 目標類 -->
	<bean  ></bean>
	
	<!-- 增強所在的類 -->
	<bean  ></bean>
	
	<aop:config proxy-target->
		<!—
雖然引介增強不需要在增強所在的類中定義一個方法用於增強的實現,但<aop:aspect ref="advisor">中的ref屬性依然要指定一個增強Bean
-->

		<aop:aspect ref="advisor">
			<!—
引介增強 
	types-matching 目標類
	implement-interface 要織入目標類的接口
	default-impl 織入接口的實現類
-->
			<aop:declare-parents 
				types-matching="cn.framelife.spring.schema.IWaiter+" 
				implement-interface="cn.framelife.spring.schema.ISeller"
				default-impl="cn.framelife.spring.schema.Seller"/>
		</aop:aspect>
	</aop:config>
</beans>


c、測試

ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		IWaiter waiter = (IWaiter) context.getBean("waiter");
		waiter.service();
		ISeller seller = (ISeller)waiter;
		seller.sell();

d、結果

service
sell

最後更新:2017-04-03 16:48:31

  上一篇:go boa服務器 說明
  下一篇:go struts標簽格式化時間&lt;s:date name=&quot;neowaveBloodPressure.monitorTime&quot; format=&quot;yyyy-MM-dd HH:mm:ss&q