`
szlxh002
  • 浏览: 33727 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Spring基于@AspectJ AOP例子

 
阅读更多

1.使用前准备

 

      Spring在修理@Aspect注解表达式时,需要将Spring的asm模块加到类路径中。asm是轻量级的字节码处理框架,因为java的反射机制无法获取入参名,Spring就利用asm处理@AspectJ中所描述的方法入参名。

      此外还需要加入aspectj.weaver和aspectj.tools类包。

 

2.配置使用@AspectJ切面

自动代理的配置

<aop:aspectj-autoproxy proxy-target-class="true"/> 或<aop:aspectj-autoproxy/>

 

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

 3.通过注解对类的进行设置

 @Before

@After

@AfterReturning

@Around

@AfterThrowing

 

package com.baobaotao.aspectj.advanced;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class TestAspect {
	//-------------复合运算----------
//	@Before("!target(com.baobaotao.NaiveWaiter) "+
//			"&& execution(* serveTo(..)))")
//	public void notServeInNaiveWaiter() {
//		System.out.println("--notServeInNaiveWaiter() executed!--");
//	}
//	@After("within(com.baobaotao.*) "
//			+ " && execution(* greetTo(..)))")
//	public void greeToFun() {
//		System.out.println("--greeToFun() executed!--");
//	}
//	
//	@AfterReturning("target(com.baobaotao.Waiter) || "+
//			        " target(com.baobaotao.Seller)")
//	public void waiterOrSeller(){
//		System.out.println("--waiterOrSeller() executed!--");
//	}
	
//	//------------引用命名切点----------//
//	@Before("TestNamePointcut.inPkgGreetTo()")
//	public void pkgGreetTo(){
//		System.out.println("--pkgGreetTo() executed!--");
//	}
//
//	@Before("!target(com.baobaotao.NaiveWaiter) && "
//			+"TestNamePointcut.inPkgGreetTo()")
//	public void pkgGreetToNotNaiveWaiter(){
//		System.out.println("--pkgGreetToNotNaiveWaiter() executed!--");
//	}
//
    //------------访问连接点对象----------//
//	@Around("execution(* greetTo(..)) && target(com.baobaotao.NaiveWaiter)")
//	public void joinPointAccess(ProceedingJoinPoint pjp) throws Throwable{
//		System.out.println("------joinPointAccess-------");
//		System.out.println("args[0]:"+pjp.getArgs()[0]);		
//		System.out.println("signature:"+pjp.getTarget().getClass());
//		pjp.proceed();
//		System.out.println("-------joinPointAccess-------");
//	}
	
  //------------绑定连接点参数----------//
//	@Before("target(com.baobaotao.NaiveWaiter) && args(name,num,..)")
//	public void bindJoinPointParams(int num,String name){
//	   System.out.println("----bindJoinPointParams()----");
//	   System.out.println("name:"+name);
//	   System.out.println("num:"+num);
//	   System.out.println("----bindJoinPointParams()----");
//	}

  //------------绑定代理对象----------//
//	@Before("execution(* greetTo(..)) && this(waiter)")
//	@Before("this(waiter)")
//	public void bindProxyObj(Waiter waiter){
//	   System.out.println("----bindProxyObj()----");
//	   System.out.println(waiter.getClass().getName());
//	   System.out.println("----bindProxyObj()----");
//	}
	
	  //------------绑定类标注对象----------//
//	@Before("@within(m)")
//	public void bindTypeAnnoObject(Monitorable m){
//	   System.out.println("----bindTypeAnnoObject()----");
//	   System.out.println(m.getClass().getName());
//	   System.out.println("----bindTypeAnnoObject()----");
//	}
    //------------绑定抛出的异常----------//
//	@AfterReturning(value="target(com.baobaotao.SmartSeller)",returning="retVal")
//	public void bingReturnValue(int retVal){
//	   System.out.println("----bingReturnValue()----");
//	   System.out.println("returnValue:"+retVal);
//	   System.out.println("----bingReturnValue()----");
//	}
	
//    //------------绑定抛出的异常----------//
	@AfterThrowing(value="target(com.baobaotao.SmartSeller)",throwing="iae")
	public void bindException(IllegalArgumentException iae){
	   System.out.println("----bindException()----");
	   System.out.println("exception:"+iae.getMessage());
	   System.out.println("----bindException()----");
	}	

}

 

 

其中相关的类

 

 

public interface Waiter {
	@NeedTest
	public void greetTo(String clientName);	
	public void serveTo(String clientName);
}


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Monitorable {

}



@Monitorable
public class NaiveWaiter implements Waiter {
	@NeedTest
	public void greetTo(String clientName) {
		System.out.println("NaiveWaiter:greet to "+clientName+"...");
	}	
	@NeedTest
	public void serveTo(String clientName){
		System.out.println("NaiveWaiter:serving "+clientName+"...");
	}
	public void smile(String clientName,int times){
		System.out.println("NaiveWaiter:smile to  "+clientName+ times+"times...");
	}	
}


public class NaughtyWaiter implements Waiter {
	public void greetTo(String clientName) {
		System.out.println("NaughtyWaiter:greet to "+clientName+"...");
	}	
	public void serveTo(String clientName){
		System.out.println("NaughtyWaiter:serving "+clientName+"...");
	}
	public void joke(String clientName,int times){
        	System.out.println("NaughtyWaiter:play "+times+" jokes to "+clientName+"...");
	}
}


public interface Seller {
  int sell(String goods,String clientName);
}
public class SmartSeller implements Seller {

	public int sell(String goods,String clientName) {
		System.out.println("SmartSeller: sell "+goods +" to "+clientName+"...");
		return 100;
	}
	
	public void checkBill(int billId){
		if(billId == 1) throw new IllegalArgumentException("iae Exception");
		else throw new RuntimeException("re Exception");
	}
}



public class TestNamePointcut {
	@Pointcut("within(com.baobaotao.*)")
	private void inPackage(){}
	
	@Pointcut("execution(* greetTo(..)))")
    protected void greetTo(){}

    @Pointcut("inPackage() and greetTo()")
    public void inPkgGreetTo(){}
}

 

 

3.测试例子

 

package com.baobaotao.aspectj.advanced;

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

import com.baobaotao.SmartSeller;
import com.baobaotao.Waiter;

public class AdvancedTest {
	public static void main(String[] args) {
		String configPath = "com/baobaotao/aspectj/advanced/beans.xml";
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
		Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter");
		//NaiveWaiter naiveWaiter1 = (NaiveWaiter) ctx.getBean("naiveWaiter");
		Waiter naughtyWaiter = (Waiter) ctx.getBean("naughtyWaiter");
//		naiveWaiter.greetTo("John");
//		naiveWaiter.serveTo("John");
//		naughtyWaiter.greetTo("Tom");
//		naughtyWaiter.serveTo("Tom");
		
        //--通过joinPoint接口访问连接点上下文信息
//		naiveWaiter.greetTo("John");
		
		//--绑定连接点参数
		//((NaiveWaiter)naiveWaiter).smile("John",2);
		//naiveWaiter1.smile("John",2);
		
		//--绑定代理对象
//		naiveWaiter.greetTo("John");
		
		//--绑定类注解
//		((NaiveWaiter)naiveWaiter).greetTo("John");

		//绑定返回值
//		SmartSeller seller = (SmartSeller) ctx.getBean("seller");
//		seller.sell("Beer","John");	
		
		//绑定异常
		SmartSeller seller = (SmartSeller) ctx.getBean("seller");
		seller.checkBill(2);
		//seller.checkBill(1);
	}
}

 

 

 

分享到:
评论

相关推荐

    Spring @AspectJ 实现AOP 入门例子

    NULL 博文链接:https://samter.iteye.com/blog/410618

    基于框架的Web开发-基于AspectJ的AOP.doc

    2 一个简单的基于@AspectJ的AOP例子 2.1 创建Performance接口 创建Performance(表演、演出)接口,接口包含一个方法perform(),包名为aop(本章的类均放在aop包中)。 2.2 创建Performance接口的实现类Ballet 创建...

    Spring 使用AspectJ 实现 AOP之前置通知小例子

    Spring 使用AspectJ 实现 AOP之前置通知小例子,实际跑过,验证可信。

    Spring实现AOP的多种方式 切点函数

    里面包括4个例子:(1)Spring实现AOP方式之一:基于XML配置的Spring AOP (2)Spring实现AOP方式之二:使用注解配置 Spring AOP (3)Spring AOP : AspectJ Pointcut 切点 (4)Spring AOP : Advice 声明 (通知注解)

    Spring AOP + AspectJ in XML 配置示例

    NULL 博文链接:https://tuoxinquyu.iteye.com/blog/1465155

    spring aop 自定义切面示例

    spring aop 自定义切面示例 aspectj 需要相应的架包支持

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1....

    spring MVC AOP注解方式如何拦截controller 例子

    有人问 Sping AOP用AspectJ注解的方式拦截不到SpringMVC的controller方法? 我这里提供了一种解决方法,仅供参考

    Spring 2.0 开发参考手册

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 ...

    Struts Spring Hibernate 整合 OpenSessionInView 例子

    为了练手培训,给大家准备的 Open Session In View 的简单例子,纯代码,大家可以参考,其中主要说了六部分内容: ...5.通过 spring aop(aspectJ) 声明事务 6.通过formular 映射参数表,指定两个死的变量

    Spring.3.x企业应用开发实战(完整版).part2

    第7章 基于@AspectJ和Schema的AOP 7.1 Spring对AOP的支持 7.2 JDK 5.0注解知识快速进阶 7.2.1 了解注解 7.2.2 一个简单的注解类 7.2.3 使用注解 7.2.4 访问注解 7.3 着手使用@AspectJ 7.3.1 使用前的准备 7.3.2 一个...

    Spring中文帮助文档

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...

    Spring API

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...

    spring chm文档

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 ...

    Spring3.x企业应用开发实战(完整版) part1

    第7章 基于@AspectJ和Schema的AOP 7.1 Spring对AOP的支持 7.2 JDK 5.0注解知识快速进阶 7.2.1 了解注解 7.2.2 一个简单的注解类 7.2.3 使用注解 7.2.4 访问注解 7.3 着手使用@AspectJ 7.3.1 使用前的准备 7.3.2 一个...

    SpringAOP切面实例讲解及应用场景(通俗易懂)

    下面是一个在spring mvc中关于切面如何使用的例子,可以指直观的理解切面到底有什么作用 1、引用 AspectJ jar 包依赖 pom.xml 文件添加依赖 org.aspectj aspectjrt 1.9.2 2、创建两个新的包 ...

    Spring+3.x企业应用开发实战光盘源码(全)

     第7章:对如何使用基于AspectJ配置AOP的知识进行了深入的分析,这包括使用XML Schema配置文件、使用注解进行配置等内容。  第8章:介绍了Spring所提供的DAO封装层,这包括Spring DAO的异常体系、数据访问模板等...

    陈开雄 Spring+3.x企业应用开发实战光盘源码.zip

     第7章:对如何使用基于AspectJ配置AOP的知识进行了深入的分析,这包括使用XML Schema配置文件、使用注解进行配置等内容。  第8章:介绍了Spring所提供的DAO封装层,这包括Spring DAO的异常体系、数据访问模板等...

    java-samples:Java中框架使用的一些示例

    :使用spring jdbc的示例 :使用spring web和servlet api 3的示例spring-aop-example :使用spring aop和AspectJ的示例待办事项:Spring Boot,Spring Web MVC 完整的例子complete-example :包含所有这些元素的微型...

Global site tag (gtag.js) - Google Analytics