Spring学习(23)--- AOP之Introductions应用

  • 简介允许一个切面声明一个实现指定接口的通知对象,并且提供了一个接口实现类来代表这些对象
  • 由<aop:aspect>中的<aop:declare-parents>元素声明该元素用于声明所匹配的类型拥有一个新的parents(因此得名)

配置:

<aop:aspect id="usageTrackerAspect" ref="usageTracking">
          <aop:declare-parents types-matching="com.xyz.myapp.service.*+" 
                                 implement-interface="com.xyz.myapp.service.tracking.UsageTracked"
                                 default-impl="com.xyz.myapp.service.tracking.DefaultUsageTracked"/>
</aop:aspect>
public void recordUsage(){
	usageTracked.incrementUseCount();
}
UsageTracked usageTracked = (UsageTracked) context.getBean("myservice");

例子:

新建接口Fit及实现类FitImpl

package com.aop.schema;

public interface Fit {
	void filter();
}
package com.aop.schema;

public class FitImpl implements Fit {

	@Override
	public void filter() {
		System.out.println("FitImpl.filter");

	}

}

切面类(同上节,不做修改了):

 

package com.aop.schema.advice;

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();
		}
	}
}

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.advice.MyAspect"></bean>
     <aop:config>
          <aop:aspect id="myAspectAOP" ref="myAspect">
            <!-- com.aop.schema.advice.*+ 指的是advice包下的所有类 -->
            <aop:declare-parents types-matching="com.aop.schema.advice.*+" 
                                 implement-interface="com.aop.schema.Fit"
                                 default-impl="com.aop.schema.FitImpl"/>
          </aop:aspect>
     </aop:config>
 
</beans>

单元测试:

package com.aop.schema;

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

import com.aop.schema.advice.ApsectBiz;

public class UnitTest {

	@Test
	public void test(){
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-aop.xml");
		Fit fit = (Fit)context.getBean("myAspect");
		fit.filter();
	}
}

测试结果:

七月 11, 2015 1:15:13 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@31b3c607: startup date [Sat Jul 11 13:15:13 CST 2015]; root of context hierarchy
七月 11, 2015 1:15:13 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-aop.xml]
FitImpl.filter

PS:说明为指定的类型强制指定了一个父类。

 

posted @ 2015-07-11 13:19  Json_wangqiang  阅读(599)  评论(0编辑  收藏  举报