您的位置:首页 > 编程语言 > Java开发

Spring基础配置(AOP切面编程)

2018-03-08 11:37 573 查看
一、AOP切面编程的简要说明

AOP:面向切面编程,相对于OOP面向对象编程

Spring的AOP的存在目的就是为了解耦。AOP可以让一组类共享相同行为。在OOP中只能

通过继承类和实现接口,来使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类 上,AOP弥补了OOP的不足

Spring 支持AspectJ的注解式切面编程

1、使用@Aspect声明是一个切面

2、使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点)作为参数

3、其中@After、@Before、@Around参数的拦截规则作为切点(PointCut),为了使切点复用,可使

@PointCut专门定义拦截规则,然后在@After、@Before、@Around的参数中调用。

4、其中符合条件的每一个被拦截处为连接点(JoinPoint)

二、示例说明

package service.aop;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
/*
* 拦截规则(切点)的注解
*/
public @interface Action {
String name();
}

//编写使用注解的被拦截类
package service.aop;
import org.springframework.stereotype.Service;
@Service
public class DemoAnnotationService {
@Action(name="注解拦截式add()")
public void add(){}
}

//编写使用方法的被拦截类
package service.aop;
import org.springframework.stereotype.Service;
@Service
public class DemoMethodService {
public void add(){}
}

package service.aop;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
/*
* AOP:面向切面编程,相对于OOP面向对象编程
* Spring的AOP的存在目的就是为了解耦。AOP可以让一组类共享相同行为。在OOP中只能
* 通过继承类和实现接口,来使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OOP的不足
* Spring 支持AspectJ的注解式切面编程
* 1、使用@Aspect声明是一个切面
* 2、使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点)作为参数
* 3、其中@After、@Before、@Around参数的拦截规则作为切点(PointCut),为了使切点复用,可使
*    @PointCut专门定义拦截规则,然后在@After、@Before、@Around的参数中调用。
* 4、其中符合条件的每一个被拦截处为连接点(JoinPoint)
*/
//使用@Aspect声明是一个切面
@Aspect
//通过@Component让此切面成为Spring容器管理的Bean
@Component
public class LogAspect {
//@PointCut专门定义拦截规则,然后在@After、@Before、@Around的参数中调用。
@Pointcut("@annotation(service.aop.Action)")
public void annotationPointCut(){};
//使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点)作为参数
@After("annotationPointCut()")
public void after(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature) joinPoint.getSignature();
Method method=signature.getMethod();
Action action=method.getAnnotation(Action.class);
System.out.println("注解式拦截"+action.name());
}
//通过@Before注解声明一个建言,此建言直接使用拦截规则作为参数
@Before("execution(* service.aop.DemoMethodService.*(..))")
public void before(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature) joinPoint.getSignature();
Method method=signature.getMethod();
System.out.println("方法式拦截,"+method.getName());

}
}

package service.aop;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("service.aop")
//使用@EnableAspectJAutoProxy注解开启Spring对AspectJ代理的支持
@EnableAspectJAutoProxy
public class AopConfig {

}

package service.aop;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AopConfig.class);
DemoAnnotationService demoAnnotationService=context.getBean(DemoAnnotationService.class);
DemoMethodService demoMethodService=context.getBean(DemoMethodService.class);
demoAnnotationService.add();
demoMethodService.add();
context.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: