Spring学习笔记(20)----------Schema初步学习

先来一个HelloWorld的简单例子,开开胃,爽一下~!

 

 

1、Aop配置文件spring-aop.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop"  
  7.     xsi:schemaLocation=  
  8.         "http://www.springframework.org/schema/beans   
  9.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  10.         http://www.springframework.org/schema/aop  
  11.         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  12.         ">  
  13.       
  14.     <bean id = "helloWorldServiceImpl" class="com.spring.xkey.aop.HelloWorldServiceImpl">  
  15.     </bean>  
  16.       
  17.     <bean id = "aspect" class = "com.spring.xkey.aop.HelloWorldAspect">  
  18.     </bean>  
  19.       
  20.     <aop:config>  
  21.         <aop:pointcut id="pointcut" expression="execution(* com.spring..*.sayHello(..)) and args(user)"/>  
  22.         <aop:aspect ref="aspect">  
  23.             <aop:before pointcut-ref = "pointcut" method="beforeAdvice(java.lang.String)" arg-names="user"/>  
  24.             <aop:after pointcut="execution(* com.spring..*.sayHello(..))" method="afterAdvice"/>  
  25.             <aop:after-returning pointcut="execution(* com.spring..*.sayAfterReturning(..))"   
  26.                                  method="afterReturningAdvice"  
  27.                                  arg-names="value"  
  28.                                  returning="value"/>  
  29.             <aop:after-throwing pointcut="execution(* com.spring..*.sayAfterThrowing(..))"  
  30.                                 method="afterThrowingAdvice"  
  31.                                 arg-names="e"  
  32.                                 throwing="e"/>  
  33.             <aop:around pointcut="execution(* com.spring..*.sayAround(..))"  
  34.                         method="aroundAdvice"  
  35.                         />  
  36.         </aop:aspect>  
  37.     </aop:config>  
  38. </beans>  
<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation=
		"http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		">
	
	<bean id = "helloWorldServiceImpl" class="com.spring.xkey.aop.HelloWorldServiceImpl">
	</bean>
	
	<bean id = "aspect" class = "com.spring.xkey.aop.HelloWorldAspect">
	</bean>
	
	<aop:config>
		<aop:pointcut id="pointcut" expression="execution(* com.spring..*.sayHello(..)) and args(user)"/>
		<aop:aspect ref="aspect">
			<aop:before pointcut-ref = "pointcut" method="beforeAdvice(java.lang.String)" arg-names="user"/>
			<aop:after pointcut="execution(* com.spring..*.sayHello(..))" method="afterAdvice"/>
			<aop:after-returning pointcut="execution(* com.spring..*.sayAfterReturning(..))" 
								 method="afterReturningAdvice"
								 arg-names="value"
								 returning="value"/>
			<aop:after-throwing pointcut="execution(* com.spring..*.sayAfterThrowing(..))"
								method="afterThrowingAdvice"
								arg-names="e"
								throwing="e"/>
			<aop:around pointcut="execution(* com.spring..*.sayAround(..))"
						method="aroundAdvice"
						/>
		</aop:aspect>
	</aop:config>
</beans>

2、HelloWorldService接口

  1. package com.spring.xkey.aop;  
  2.   
  3. public interface HelloWorldService {  
  4.   
  5.     public void sayHello(String username);  
  6.     public boolean sayAfterReturning();   
  7.     public void sayAfterThrowing();  
  8.     public int sayAround(String username,String password);  
  9. }  
package com.spring.xkey.aop;

public interface HelloWorldService {

	public void sayHello(String username);
	public boolean sayAfterReturning(); 
	public void sayAfterThrowing();
	public int sayAround(String username,String password);
}


3、HelloWorldServiceImpl类

  1. package com.spring.xkey.aop;  
  2. import java.lang.RuntimeException;  
  3.   
  4. public class HelloWorldServiceImpl  implements HelloWorldService{  
  5.     public void sayHello(String username){  
  6.         System.out.println("My first Aop test "+username);  
  7.     }  
  8.     public boolean sayAfterReturning(){  
  9.         System.out.println("after returning");  
  10.         return true;  
  11.     }  
  12.       
  13.     public void sayAfterThrowing(){  
  14.         System.out.println("After throwing");  
  15.         throw new RuntimeException();  
  16.     }  
  17.       
  18.     public int sayAround(String username,String password){  
  19.         System.out.println("Around::: "+username+", "+password);  
  20.         int val = 1;  
  21.         return val;  
  22.     }  
  23. }  
package com.spring.xkey.aop;
import java.lang.RuntimeException;

public class HelloWorldServiceImpl  implements HelloWorldService{
	public void sayHello(String username){
		System.out.println("My first Aop test "+username);
	}
	public boolean sayAfterReturning(){
		System.out.println("after returning");
		return true;
	}
	
	public void sayAfterThrowing(){
		System.out.println("After throwing");
		throw new RuntimeException();
	}
	
	public int sayAround(String username,String password){
		System.out.println("Around::: "+username+", "+password);
		int val = 1;
		return val;
	}
}


4、具体实现AOP编程的HelloWorldAspect类

  1. package com.spring.xkey.aop;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4. import org.aspectj.lang.ProceedingJoinPoint;  
  5.   
  6.   
  7. public class HelloWorldAspect {  
  8.       
  9.     public void beforeAdvice(String user){  
  10.         //System.out.println(jp.getArgs()[0].toString());  
  11.         System.out.println("before advice " + user);  
  12.     }  
  13.       
  14.     public void afterAdvice(JoinPoint jp){  
  15.         System.out.println("after advice "+jp.getArgs()[0].toString());  
  16.     }  
  17.       
  18.     public void afterReturningAdvice(Object value){  
  19.         System.out.println("afterReturning advice "+value);  
  20.           
  21.     }  
  22.     public void afterThrowingAdvice(Exception e){  
  23.         System.out.println("after throwing advice exception:" + e);   
  24.     }  
  25.       
  26.     public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable{  
  27.         /**System.out.println(pjp.toLongString()); 
  28.         System.out.println(pjp.getSignature().toString()); 
  29.         System.out.println(pjp.getKind().toString()); 
  30.         System.out.println(pjp.getArgs()[0].toString()); 
  31.         System.out.println("name+psd");*/  
  32.         Object [] obj = pjp.getArgs();  
  33.         String username = (String) obj[0];  
  34.         String password = (String) obj[1];  
  35.         System.out.println(username+","+password);  
  36.         Object retVal;  
  37.         if(username.equals("xkey") && password.equals("color")){  
  38.             retVal = pjp.proceed();  
  39.         }else{  
  40.             retVal = pjp.proceed(new Object[]{"ideal","*******"});  
  41.         }  
  42.         System.out.println("name+psd");  
  43.         if(retVal == null) System.out.println("false");  
  44.         else{  
  45.             System.out.println(retVal.toString());  
  46.         }  
  47.         return 2;  
  48.     }  
  49. }  
package com.spring.xkey.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;


public class HelloWorldAspect {
	
	public void beforeAdvice(String user){
		//System.out.println(jp.getArgs()[0].toString());
		System.out.println("before advice " + user);
	}
	
	public void afterAdvice(JoinPoint jp){
		System.out.println("after advice "+jp.getArgs()[0].toString());
	}
	
	public void afterReturningAdvice(Object value){
		System.out.println("afterReturning advice "+value);
		
	}
	public void afterThrowingAdvice(Exception e){
		System.out.println("after throwing advice exception:" + e); 
	}
	
	public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable{
		/**System.out.println(pjp.toLongString());
		System.out.println(pjp.getSignature().toString());
		System.out.println(pjp.getKind().toString());
		System.out.println(pjp.getArgs()[0].toString());
		System.out.println("name+psd");*/
		Object [] obj = pjp.getArgs();
		String username = (String) obj[0];
		String password = (String) obj[1];
		System.out.println(username+","+password);
		Object retVal;
		if(username.equals("xkey") && password.equals("color")){
			retVal = pjp.proceed();
		}else{
			retVal = pjp.proceed(new Object[]{"ideal","*******"});
		}
		System.out.println("name+psd");
		if(retVal == null) System.out.println("false");
		else{
			System.out.println(retVal.toString());
		}
		return 2;
	}
}


5、测试类

  1. package com.spring.xkey.aop;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class AopTest {  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.         // TODO Auto-generated method stub  
  13.         ApplicationContext context =  
  14.             new ClassPathXmlApplicationContext("com/spring/xkey/aop/spring-aop.xml");  
  15.         HelloWorldService hello = (HelloWorldService)context.getBean("helloWorldServiceImpl");  
  16.         hello.sayHello("xkey");  
  17.         System.out.println("===================================");  
  18. //      hello.sayAfterReturning();  
  19. //      System.out.println("===================================");  
  20. //      hello.sayAfterThrowing();  
  21. //      System.out.println("===================================");  
  22.         Object val = (Object)hello.sayAround("xkey", "colo");  
  23.         System.out.println(val.toString());  
  24.     }  
  25. }  
package com.spring.xkey.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext context =
			new ClassPathXmlApplicationContext("com/spring/xkey/aop/spring-aop.xml");
		HelloWorldService hello = (HelloWorldService)context.getBean("helloWorldServiceImpl");
		hello.sayHello("xkey");
		System.out.println("===================================");
//		hello.sayAfterReturning();
//		System.out.println("===================================");
//		hello.sayAfterThrowing();
//		System.out.println("===================================");
		Object val = (Object)hello.sayAround("xkey", "colo");
		System.out.println(val.toString());
	}
}


 

6、AOP Around(环绕通知)可以控制返回对象,即你可以返回一个与目标对象完全不同的返回值,虽然这很危险,但是你却可以办到。目标方法的返回值 就是 环绕通知的返回值。proceed() 方法可以在通知体内调用一次、多次 或根本不用调用;还可以控制执行方法的参数值。具体见aroundAdvice方法的实现。


7、给个更直观的结果

  1. before advice xkey  
  2. My first Aop test xkey  
  3. after advice xkey  
  4. ===================================  
  5. xkey,colo  
  6. Around::: ideal, ********  
  7. name+psd  
  8. In the AroundAdvice the return value is 1  
  9. After AroundAdvice modify return value is 2  
before advice xkey
My first Aop test xkey
after advice xkey
===================================
xkey,colo
Around::: ideal, ********
name+psd
In the AroundAdvice the return value is 1
After AroundAdvice modify return value is 2


 

这里使用AOP Around就可以用来进行一个身份验证,通过面向切面编程(AOP)将验证部分独立出来,使得代码的耦合度降低,如果使用面向对象思想(OOP)就应该是独立写一个身份验证的方法,然后当需要使用该方法再去new 一个身份验证方法的类,然后传递参数通过验证类中的方法去验证,这样代码的耦合度就变强了。


 

 

通过Scheme配置实现AOP步骤

   步骤一、编写业务类:

public class AspectBusiness {
   //
切入点
    public String delete(String obj) {
        System.out.println("==========调用切入点:" + obj + "说:你敢删除我!===========\n");
        return obj + ":瞄~";
    }

    public String add(String obj) {
        System.out.println("================这个方法不能被切。。。============== \n");
        return obj + ":瞄~ 嘿嘿!";
    }

    public String modify(String obj) {
        System.out.println("=================这个也设置加入切吧====================\n");
        return obj + ":瞄改瞄啊!";
    }

}

步骤二、编写切面类:切面类中,包含了所有的通知

public class AspectAdvice {

     //
前置通知
   public void doBefore(JoinPoint jp) {
        System.out.println("===========进入before advice============ \n");

        System.out.print("准备在" + jp.getTarget().getClass() + "对象上用");
        System.out.print(jp.getSignature().getName() + "方法进行对 '");
        System.out.print(jp.getArgs()[0] + "'进行删除!\n\n");

        System.out.println("要进入切入点方法了 \n");
    }


     // 后置通知
     // @param jp
     //            连接点
     // @param result
     //          返回值
    
    public void doAfter(JoinPoint jp, String result) {
        System.out.println("==========进入after advice=========== \n");
        System.out.println("切入点方法执行完了 \n");

        System.out.print(jp.getArgs()[0] + "在");
        System.out.print(jp.getTarget().getClass() + "对象上被");
        System.out.print(jp.getSignature().getName() + "方法删除了");
        System.out.print("只留下:" + result + "\n\n");
    }


     // 环绕通知
    // @param pjp
     //           连接点
   
    public object doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("===========进入around环绕方法!=========== \n");

        // 调用目标方法之前执行的动作
        System.out.println("调用方法之前: 执行!\n");

        // 调用方法的参数
        Object[] args = pjp.getArgs();
        // 调用的方法名
        String method = pjp.getSignature().getName();
        // 获取目标对象
        Object target = pjp.getTarget();
        // 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行
        Object result = pjp.proceed();

        System.out.println("输出:" + args[0] + ";" + method + ";" + target + ";" + result + "\n");
        System.out.println("调用方法结束:之后执行!\n");
     return  result;
    }

 
     //异常通知
  
    public void doThrow(JoinPoint jp, Throwable e) {
        System.out.println("删除出错啦");
    }

}

步骤四、配置文件的编写:

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="    
          http://www.springframework.org/schema/beans    
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
          http://www.springframework.org/schema/context    
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/aop    
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    default->

    <!-- ==============================利用spring 利用aspectj来配置AOP================================ -->

    <!-- 声明一个业务类 -->
    <bean id="aspectBusiness" class="aop.schema.AspectBusiness" />

    <!-- 声明通知类 -->
    <bean id="aspectAdvice" class="aop.schema.advice.AspectAdvice" />


   <aop:config>
        <aop:aspect id="businessAspect" ref="aspectAdvice">
            <!-- 配置指定切入的对象 -->
            <aop:pointcut id="point_cut" expression="execution(* aop.schema.*.*(..))" />
            <!-- 只匹配add方法作为切入点
            <aop:pointcut id="except_add" expression="execution(* aop.schema.*.add(..))" />
             -->

            <!-- 前置通知 -->
            <aop:before method="doBefore" pointcut-ref="point_cut" />
            <!-- 后置通知 returning指定返回参数 -->
            <aop:after-returning method="doAfter"
                pointcut-ref="point_cut" returning="result" />
            <aop:around method="doAround" pointcut-ref="point_cut"/>
            <aop:after-throwing method="doThrow" pointcut-ref="point_cut" throwing="e"/>
        </aop:aspect>
    </aop:config>

</beans>

步骤五、测试类:

public class Debug {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("aop/schema_aop.xml");
        AspectBusiness business = (AspectBusiness) context.getBean("aspectBusiness");
        business.delete("猫");
    }

}

转载于:https://my.oschina.net/sfsimon/blog/659426

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值