`

Spring的AOP_XXXXXX

阅读更多

1、相关概念:
1)Aspect(切面):指横切性关注点的抽象即为切面。与类相似,只是两者的关注点不同,类是对物体特征的抽象,而切面是横切性关注点的抽象。
2)Joinpoint(连接点):指一些被拦截到的点。在Spring中,这些点指的是方法,因为Spring只支持方法类型的连接点,实际上连接点还可以是field或类构造器。
3)Advice(通知):指拦截到连接点之后所要做的事情,分为前置通知,后置通知,异常通知,最终通知和环绕通知。
4)Pointcut(切入点):指对那些Jointpoint进行拦截的定义。
5)Target(目标对象):代理要实现的目标对象。
6)Weave(织入):指将Aspects应用到Target对象并导致proxy对象创建的过程。
7)Introduction(引入):在不修改类代码的前提下,Introduction可以在运行期间为类动态地添加一些方法或Field.

 

2、使用Spring的注解方式实现AOP
首先要在XML文件中添加以下语句:
<?xml version="1.0" encoding="UTF-8"?>
<beans ……
       …… 
       xmlns:aop="
http://www.springframework.org/schema/aop"     
       ……
      
http://www.springframework.org/schema/aop                  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
      
 <aop:aspectj-autoproxy/>
       
 ……

</beans>
然后就可以将类声明为切面,在切面中设置切入点,通知等,如:
@Aspect
//@Aspect声明一个切面  @Component 将切面交给Spring管理

public class MyInterceptor {
	@Pointcut("execution(* com.spring.aop.service.impl.PersonserviceBean.*(..))")
	private void anyMethod(){}//声明一个切入点
	
	@Before("anyMethod() && args(name)")
	public void doAccessCheck(String name){//设置前置通知
		System.out.println("here is before advice!"+name);
	}
	
	@AfterReturning(pointcut="anyMethod()",returning="result")
	public void doAfterReturning(String result){//设置后置通知
		System.out.println("here is AfterReturning advice! "+result);
	}
	
	@After("anyMethod()")
	public void doAfter(){//设置最终通知
		System.out.println("here is After advice!");
	}
	
	@Around("anyMethod()")//设置环绕通知,必须有下面的参数话和调用proceed()方法
	public Object doAroundAdvice(ProceedingJoinPoint pjp)throws Throwable{
		System.out.println("go into the method!");
		Object result = pjp.proceed();
		System.out.println("drop back the method!");
		return result;	
	}
}

 

3、使用Spring的XML配置文件实现AOP
 只需要将声明为切面的Bean在配置文件中声明,在使用配置文件中的切面设置进行具体配置,如:
<?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: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-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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="aspetbean" class="com.fojian.service.MyInterceptor"/><!--将需要做切面的Bean交Spring管理-->
        <aop:config>
         <aop:aspect id="asp" ref="aspetbean"><!--声明切面-->
          <aop:pointcut id="mycut" expression="execution(* com.fojian.service..*.*(..))"/><!--声明切入点-->
          <aop:before pointcut-ref="mycut" method="doAccessCheck"/><!--声明前置通知-->
          <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/><!--声明后置通知-->
   <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/><!--声明例外通知-->
   <aop:after pointcut-ref="mycut" method="doAfter"/><!--声明最终通知-->
   <aop:around pointcut-ref="mycut" method="doBasicProfiling"/><!--声明环绕通知-->
         </aop:aspect>
        </aop:config>
</beans>

分享到:
评论

相关推荐

    Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现.doc

    AopNamespaceHandler 将 aop:xxxxxx 配置标签交给指定的 parser 来处理。例如,aspectj-autoproxy 配置标签的解析是通过 AspectJAutoProxyBeanDefinitionParser 来实现的。 三、config 配置标签的解析 config 配置...

    基于Struts2+Spring+Hibernate+MySql的注册登录系统

    其次,Spring框架是一个全面的后端解决方案,它包含依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)等核心特性。在本系统中,Spring主要负责管理Bean的生命周期,以及提供...

    JavaEE8500offer模板包含详细说明和定义

    3. **xxxxxx协同管理平台6.0** - 使用Struts、Spring和Hibernate框架,实现集团化应用、权限管理和集群部署。通过Struts2的拦截器和Filter实现不同粒度的权限控制,同时避免了对Spring的过度依赖,保持了代码的灵活...

    个人简历3-5年(面试阿里,腾讯)必备模板

    - **框架知识**:熟悉Spring框架的核心功能,如IOC、AOP等概念,并能够使用Spring Boot快速搭建应用程序。 - **数据库技术**:掌握MySQL数据库的使用方法,包括SQL语句的编写、索引优化等。 - **非关系型数据库**:...

    智慧信息化工程解决方案培训资料.docx

    Spring是一个开源的Java应用程序框架,它简化了企业级Java应用的开发,提供依赖注入、AOP(面向切面编程)等功能。 2.4.3. MVC设计模式 Model-View-Controller模式是软件设计中的一种架构模式,用于分离业务逻辑、...

Global site tag (gtag.js) - Google Analytics