AOP:Aspect Oriented programming(面向切面编程)
面向切面编程(也叫面向方面编程):
Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是Spring框架中的一个重要内容。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
主要的功能是:
日志记录,性能统计,安全控制,事务处理,异常处理等等。
在spring中有两种配置AOP的方式,使用Annotation或者XML。
1. 使用Annotation进行配置
1) 在XML文件中加入配置信息,使spring支持AOP的Annotation方式,并且在XML编写中给予提示。
范例:
</beans>
2) 建立拦截类并且用@Aspect标注该类,表示该类为拦截类。并且用@Component标注该类,使spring 帮我们管理该拦截器。
3) 建立处理方法,使用常见的Annotation标示处理方式。常用的处理方式有:
a. @PointCut 建立一个切面
b. @before 在调用方法之前,调用此标注的处理方法
c. @AfterReturning 在调用方法结束之后,调用此Annotation标注的处理方法
d. @AfterThrowing 在调用方法抛出异常之后,调用此Annotation标注的处理方法
e. @After 在调用方法结束之后,调用此Annotation标注的方法
f. @Around 在调用方法开始和结束之后,都会调用此方法
4) 范例:
2. 使用XML配置AOP
在关键字上与Annotation类似,给出一个XML范例…
面向切面编程(也叫面向方面编程):
Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是Spring框架中的一个重要内容。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
主要的功能是:
日志记录,性能统计,安全控制,事务处理,异常处理等等。
在spring中有两种配置AOP的方式,使用Annotation或者XML。
1. 使用Annotation进行配置
1) 在XML文件中加入配置信息,使spring支持AOP的Annotation方式,并且在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: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">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<aop:aspectj-autoproxy /> <!—使其AOP支持Annotation -->
</beans>
2) 建立拦截类并且用@Aspect标注该类,表示该类为拦截类。并且用@Component标注该类,使spring 帮我们管理该拦截器。
3) 建立处理方法,使用常见的Annotation标示处理方式。常用的处理方式有:
a. @PointCut 建立一个切面
b. @before 在调用方法之前,调用此标注的处理方法
c. @AfterReturning 在调用方法结束之后,调用此Annotation标注的处理方法
d. @AfterThrowing 在调用方法抛出异常之后,调用此Annotation标注的处理方法
e. @After 在调用方法结束之后,调用此Annotation标注的方法
f. @Around 在调用方法开始和结束之后,都会调用此方法
4) 范例:
@Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.bjsxt.service..*.add(..))") //表示com.bjsxt.service下面任意包中的任意类的add方法都包含在此切面之中
public void myMethod(){};
@Before("myMethod()")
public void before() {
System.out.println("method before");
}
@Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable { //使用Around的固定用法
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
}
}
2. 使用XML配置AOP
在关键字上与Annotation类似,给出一个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: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">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
<aop:config>
<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="before" pointcut="execution(public * com.bjsxt.service..*.add(..))" />
</aop:aspect>
</aop:config>
</beans>