Spring学习(22)--- AOP之Advice应用(下)

(六)Advice parameters(advice带参数的情况)

例子:

修改MyAspect(添加around_init方法):

package com.aop.schema;

import org.aspectj.lang.ProceedingJoinPoint;

/**
*
* 切面类
*
*/
public class MyAspect {

	public void before(){
		System.out.println("MyAspect.before");
	}
	
	public void afterreturning(){
		System.out.println("MyAspect.afterreturning");
	}
	
	public void afterthrowing(){
		System.out.println("MyAspect.afterthrowing");
	}
	
	public void after(){
		System.out.println("MyAspect.after");
	}
	
	public void around(ProceedingJoinPoint pjp) {
		try {
			System.out.println("MyAspect.around_1");
			Object obj=pjp.proceed();
			System.out.println("MyAspect.around_2");
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	public void around_init(ProceedingJoinPoint pjp,String name,int age) {
		System.out.println(name+"  "+age);
		try {
			System.out.println("MyAspect.around_1");
			Object obj=pjp.proceed();
			System.out.println("MyAspect.around_2");
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}

修改ApsectBiz类(添加init方法):

package com.aop.schema;
/**
*
* 业务类
*
*/
public class ApsectBiz {

	public void biz(){
		System.out.println("ApsectBiz.biz");
		//throw new RuntimeException();  //故意抛出异常
	}
	
	public void init(String name,int age){
		System.out.println("ApsectBiz.init : "+ name +"  " +age);
	}
}

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"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
         
     <bean id="myAspect" class="com.aop.schema.MyAspect"></bean>
      
     <bean id="apsectBiz" class="com.aop.schema.ApsectBiz"></bean>
      
     <aop:config>
          <aop:aspect id="myAspectAOP" ref="myAspect">
          <!--  先注释掉,便于观察结果
            <aop:pointcut id="myPointcut" expression="execution(* com.aop.schema.ApsectBiz.*(..))" />
            <aop:before method="before" pointcut-ref="myPointcut"/>
            <aop:after-returning method="afterreturning" pointcut-ref="myPointcut"/>
            <aop:after-throwing method="afterthrowing" pointcut-ref="myPointcut"/>
            <aop:after method="after" pointcut-ref="myPointcut"/>
            <aop:around method="around" pointcut-ref="myPointcut"/>
           -->
             
            <aop:around method="around_init" pointcut="execution(* com.aop.schema.ApsectBiz.init(String,int)) and args(name,age)"/>
          </aop:aspect>
     </aop:config>
 
</beans>

单元测试:

package com.aop.schema;

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

public class UnitTest {

	@Test
	public void test(){
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-aop.xml");
		ApsectBiz biz = (ApsectBiz)context.getBean("apsectBiz");
		biz.init("Json",25);
	}
}

结果:

七月 09, 2015 11:48:42 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@118e0f0f: startup date [Thu Jul 09 23:48:42 CST 2015]; root of context hierarchy
七月 09, 2015 11:48:42 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-aop.xml]
Json  25
MyAspect.around_1
ApsectBiz.init : Json  25
MyAspect.around_2

 

posted @ 2015-07-09 23:52  Json_wangqiang  阅读(355)  评论(0编辑  收藏  举报