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

使用Spring的注解方式实现AOP

 
阅读更多

spring对AOP的实现提供了很好的支持。下面我们就使用Spring的注解来完成AOP做一个例子。

首先,为了使用Spring的AOP注解功能,必须导入如下几个包。aspectjrt.jar,aspectjweaver.jar,cglib-nodep.jar.

然后我们写一个接口

  1. package com.bird.service;  
  2.   
  3. public interface PersonServer {  
  4.   
  5.     public void save(String name);  
  6.     public void update(String name, Integer id);  
  7.     public String getPersonName(Integer id);  
  8.       
  9. }  
package com.bird.service;

public interface PersonServer {

	public void save(String name);
	public void update(String name, Integer id);
	public String getPersonName(Integer id);
	
}


和一个接口实现类

 

  1. package com.bird.service.impl;  
  2.   
  3. import com.bird.service.PersonServer;  
  4.   
  5. public class PersonServiceBean implements PersonServer{  
  6.       
  7.     @Override  
  8.     public void save(String name) {  
  9.           
  10.         System.out.println("我是save方法");  
  11.     //  throw new RuntimeException();  
  12.     }  
  13.   
  14.     @Override  
  15.     public void update(String name, Integer id) {  
  16.           
  17.         System.out.println("我是update()方法");  
  18.     }  
  19.   
  20.     @Override  
  21.     public String getPersonName(Integer id) {  
  22.           
  23.         System.out.println("我是getPersonName()方法");  
  24.         return "xxx";  
  25.     }  
  26.   
  27. }  
package com.bird.service.impl;

import com.bird.service.PersonServer;

public class PersonServiceBean implements PersonServer{
	
	@Override
	public void save(String name) {
		
		System.out.println("我是save方法");
	//	throw new RuntimeException();
	}

	@Override
	public void update(String name, Integer id) {
		
		System.out.println("我是update()方法");
	}

	@Override
	public String getPersonName(Integer id) {
		
		System.out.println("我是getPersonName()方法");
		return "xxx";
	}

}


下面使用Spring注解方式对这个Bean进行方法拦截

 

  1. package com.bird.service;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4. import org.aspectj.lang.annotation.After;  
  5. import org.aspectj.lang.annotation.AfterReturning;  
  6. import org.aspectj.lang.annotation.AfterThrowing;  
  7. import org.aspectj.lang.annotation.Around;  
  8. import org.aspectj.lang.annotation.Aspect;  
  9. import org.aspectj.lang.annotation.Before;  
  10. import org.aspectj.lang.annotation.Pointcut;  
  11.   
  12. /** 
  13.  * 切面 
  14.  * @author Bird 
  15.  * 
  16.  */  
  17. @Aspect  
  18. public class MyInterceptor {  
  19.     @Pointcut("execution(* com.bird.service.impl.PersonServiceBean.*(..))")  
  20.     private void anyMethod(){}//定义一个切入点  
  21.       
  22.     @Before("anyMethod() && args(name)")  
  23.     public void doAccessCheck(String name){  
  24.         System.out.println(name);  
  25.         System.out.println("前置通知");  
  26.     }  
  27.       
  28.     @AfterReturning("anyMethod()")  
  29.     public void doAfter(){  
  30.         System.out.println("后置通知");  
  31.     }  
  32.       
  33.     @After("anyMethod()")  
  34.     public void after(){  
  35.         System.out.println("最终通知");  
  36.     }  
  37.       
  38.     @AfterThrowing("anyMethod()")  
  39.     public void doAfterThrow(){  
  40.         System.out.println("例外通知");  
  41.     }  
  42.       
  43.     @Around("anyMethod()")  
  44.     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
  45.         System.out.println("进入环绕通知");  
  46.         Object object = pjp.proceed();//执行该方法  
  47.         System.out.println("退出方法");  
  48.         return object;  
  49.     }  
  50. }  
package com.bird.service;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * 切面
 * @author Bird
 *
 */
@Aspect
public class MyInterceptor {
	@Pointcut("execution(* com.bird.service.impl.PersonServiceBean.*(..))")
	private void anyMethod(){}//定义一个切入点
	
	@Before("anyMethod() && args(name)")
	public void doAccessCheck(String name){
		System.out.println(name);
		System.out.println("前置通知");
	}
	
	@AfterReturning("anyMethod()")
	public void doAfter(){
		System.out.println("后置通知");
	}
	
	@After("anyMethod()")
	public void after(){
		System.out.println("最终通知");
	}
	
	@AfterThrowing("anyMethod()")
	public void doAfterThrow(){
		System.out.println("例外通知");
	}
	
	@Around("anyMethod()")
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("进入环绕通知");
		Object object = pjp.proceed();//执行该方法
		System.out.println("退出方法");
		return object;
	}
}
  1. @Pointcut("execution(* com.bird.service.impl.PersonServiceBean.*(..))")  
@Pointcut("execution(* com.bird.service.impl.PersonServiceBean.*(..))")

这句话是方法切入点,execution为执行的意思,*代表任意返回值,然后是包名,.*意思是包下面的所有子包。(..)代

 

表各种方法.

然后下面的注解就比较简单了,就是在使用方法前和中,还有环绕拦截/

然后在Spring的配置文件中继续配置Bean,需要打开AOP命名空间

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xsi:schemaLocation="  
  6.        http://www.springframework.org/schema/beans   
  7.        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.        http://www.springframework.org/schema/aop   
  9.        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  10.           
  11.         <aop:aspectj-autoproxy/>  
  12.     <bean id="personServiceBean" class="com.bird.service.impl.PersonServiceBean"/>  
  13.     <bean id="myInterceptor" class="com.bird.service.MyInterceptor"/>  
  14.      
  15. </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: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">
		
		<aop:aspectj-autoproxy/>
	<bean id="personServiceBean" class="com.bird.service.impl.PersonServiceBean"/>
    <bean id="myInterceptor" class="com.bird.service.MyInterceptor"/>
   
</beans>


然后建立一个Junit测试

 

  1. package junit.test;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import com.bird.service.PersonServer;  
  8.   
  9. public class SpringAOPTest {  
  10.       
  11.     @Test  
  12.     public void inteceptorTest(){  
  13.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beanAop.xml");  
  14.         PersonServer bean = (PersonServer)ctx.getBean("personServiceBean");  
  15.         bean.save(null);  
  16.     }  
  17.       
  18.   
  19. }  
package junit.test;

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

import com.bird.service.PersonServer;

public class SpringAOPTest {
	
	@Test
	public void inteceptorTest(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beanAop.xml");
		PersonServer bean = (PersonServer)ctx.getBean("personServiceBean");
		bean.save(null);
	}
	

}


测试结果为

 

  1. 2012-3-12 18:08:39 org.springframework.context.support.AbstractApplicationContext prepareRefresh  
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6: display name [org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6]; startup date [Mon Mar 12 18:08:39 CST 2012]; root of context hierarchy  
  3. 2012-3-12 18:08:40 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
  4. 信息: Loading XML bean definitions from class path resource [beanAop.xml]  
  5. 2012-3-12 18:08:40 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory  
  6. 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6]: org.springframework.beans.factory.support.DefaultListableBeanFactory@b0bad7  
  7. 2012-3-12 18:08:40 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons  
  8. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@b0bad7: defining beans [org.springframework.aop.config.internalAutoProxyCreator,personServiceBean,myInterceptor]; root of factory hierarchy  
  9. null  
  10. 前置通知  
  11. 进入环绕通知  
  12. 我是save方法  
  13. 后置通知  
  14. 退出方法  
  15. 最终通知  
2012-3-12 18:08:39 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6: display name [org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6]; startup date [Mon Mar 12 18:08:39 CST 2012]; root of context hierarchy
2012-3-12 18:08:40 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beanAop.xml]
2012-3-12 18:08:40 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6]: org.springframework.beans.factory.support.DefaultListableBeanFactory@b0bad7
2012-3-12 18:08:40 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@b0bad7: defining beans [org.springframework.aop.config.internalAutoProxyCreator,personServiceBean,myInterceptor]; root of factory hierarchy
null
前置通知
进入环绕通知
我是save方法
后置通知
退出方法
最终通知




 

<!-- Baidu Button BEGIN -->

 

<!-- Baidu Button END --> <!--172.16.140.13--> <!-- Baidu Button BEGIN --> <!-- Baidu Button END -->

分享到:
评论

相关推荐

    使用Spring的注解方式实现AOP的细节

    以上就是Spring注解方式实现AOP的一些核心细节。通过这种方式,我们可以方便地在不修改原有代码的情况下,为服务添加额外的功能,实现代码的解耦和复用。不过,需要注意的是,过度使用AOP可能会导致代码可读性和可...

    spring注解方式实现aop

    通过以上步骤,我们可以使用Spring注解方式实现AOP,将关注点如日志、缓存等与业务逻辑分离,提高代码的可读性和可维护性。在实际项目中,可以灵活运用这些知识来优化代码结构,降低复杂性。希望本教程对理解和应用...

    基于注解实现SpringAop

    基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop

    Spring注解方式实现AOP demo

    本篇将通过注解方式探讨如何在Spring中实现AOP,基于提供的资源,我们可以看到一个实际的Demo项目结构。 首先,我们来看项目的基本结构: 1. `bin`目录:编译后的Java类文件会放在这里。 2. `.settings`目录:包含...

    使用Spring的注解方式实现AOP入门

    本教程将引导您入门Spring的注解式AOP实现。 首先,我们需要理解AOP的基本概念。AOP的核心是切面(Aspect),它封装了特定的关注点,如日志记录。切点(Pointcut)定义了在何时应用这些关注点,通常是一个方法调用...

    Spring基于注解实现AOP

    本篇文章将深入探讨如何在Spring中通过注解实现AOP。 首先,了解AOP的基本概念。面向切面编程是一种编程范式,它允许程序员定义“切面”,这些切面包含了跨越多个对象的行为或责任。切点是这些行为插入到主业务逻辑...

    注解方式实现AOP编程

    在Spring框架中,AOP的实现有两种主要方式:一种是基于XML配置,另一种是基于注解。本篇将主要讨论如何通过注解方式来实现AOP编程。 首先,我们需要了解Spring中的核心注解。`@Aspect`是定义一个切面的注解,通常会...

    Spring 基于注解方式实现AOP

    下面我们将深入探讨如何使用注解来实现Spring AOP。 首先,我们需要了解AOP的基本概念。AOP是一种编程范式,旨在将横切关注点(如日志、事务管理、安全性等)从核心业务逻辑中分离出来。在Spring中,切面由通知...

    以注解方式模拟Spring IoC AOP

    Spring提供了两种主要的AOP实现方式:基于代理(Proxy-based)和基于注解(Annotation-based)。 - **基于代理的AOP**:Spring使用JDK动态代理或CGLIB动态代理创建目标对象的代理,代理对象在调用目标方法前后执行...

    Spring通过注解实现AOP

    在Spring中,AOP主要通过两种方式实现:一种是基于XML配置,另一种是基于注解。这里我们重点讲解基于注解的方式。Spring支持的注解包括`@Aspect`、`@Before`、`@After`、`@Around`、`@Pointcut`等。 1. **@Aspect**...

    使用Spring配置文件实现AOP

    这种方式虽然相比注解方式略显繁琐,但对于大型项目或者需要精细控制AOP配置的情况,仍然是一个很好的选择。通过深入理解和实践,我们可以更好地利用Spring AOP来优化我们的应用程序,提高代码的可读性和可维护性。

    使用Spring的注解方式实现AOP实例

    使用Spring的注解方式实现AOP实例 使用Spring的注解方式实现AOP实例是指通过使用Spring框架提供的注解方式来实现Aspect-Oriented Programming(面向方面编程)的功能。AOP是面向对象编程的一种补充,能够将跨越多个...

    Spring学习笔记(15)----使用Spring的注解方式实现AOP

    通过以上讲解,我们已经掌握了如何使用Spring的注解方式实现AOP的基本步骤。实际开发中,可以根据需要定制各种通知类型,灵活地应用在业务逻辑中,提高代码的可读性和可维护性。同时,Spring的AOP功能也可以与Spring...

    spring aop注解方式、xml方式示例

    Spring AOP提供了注解和XML两种方式来实现切面编程。注解方式更加简洁,易于理解和维护,适用于大多数情况。而XML配置方式则在复杂场景下更具灵活性,如需要动态调整切面配置时。在实际项目中,可以根据需求选择适合...

    Spring 使用AspectJ 实现 AOP(基于xml文件、基于注解)

    本教程将探讨如何在Spring中结合AspectJ实现AOP,包括基于XML配置和基于注解的方式。 **一、AOP基本概念** AOP的核心概念有切面(Aspect)、连接点(Join Point)、通知(Advice)、切点(Pointcut)和引入...

    spring aop 自定义注解保存操作日志到mysql数据库 源码

    2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、可在现有源码上快速进行功能扩展 4、spring boot,mybatis,druid,spring aop的使用

    springboot spring aop 拦截器注解方式实现脱敏

    总结一下,通过上述步骤,我们已经在Spring Boot应用中利用Spring AOP和注解方式实现了数据脱敏。这个拦截器可以在不修改原有业务代码的情况下,确保敏感信息在响应给客户端之前得到处理,提高了应用的安全性。同时...

    spring注解aop配置详解

    通过以上介绍,我们可以看到Spring的注解AOP配置是如何让代码更简洁、更易于理解和维护的。结合实际的项目需求,我们可以灵活地使用这些注解来实现各种企业级功能,如日志、事务控制等,从而提高代码的复用性和模块...

    JavaEE spring半自动实现AOP代理

    在Spring中,AOP代理有两种实现方式:JDK动态代理和CGLIB代理。JDK代理适用于实现了接口的类,而CGLIB代理则适用于未实现接口的类。 1. **JDK动态代理**: - Spring通过实现`java.lang.reflect.InvocationHandler`...

Global site tag (gtag.js) - Google Analytics