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)
二、示例说明
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(); } }
相关文章推荐
- spring aop切面编程基础实例
- Spring Aop切面编程配置与实现
- 关于spring的aop(切面编程)配置总结
- AOP基础知识及AOP切面编程之注释方法、xml配置方法
- 重新学习之spring第二个程序,配置AOP面向切面编程
- Spring配置过程 (二)面向切面编程AOP
- Spring AOP面向切面编程一个简单例子和在配置过程中出现错误
- Spring3.0 学习-AOP面向切面编程_Spring AOP的XML配置模式
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- CGLIB实现AOP代理的测试类,与Spring分离,加深对面向切面编程的理解
- 武林秘籍之Spring AOP 切面编程的简单应用
- Spring的AOP-面向切面编程
- JavaWeb学习笔记-spring-17-AOP-基于schema配置切面
- Spring AOP 面向切面编程相关注解
- spring(五)---aspectj aop编程(xml配置)
- spring面向切面编程AOP(Aspect-orented programming)
- Spring 3.x企业开发(三)之AOP面向切面编程
- 【我看Spring】从一个简单的AOP示例看切面编程
- Spring面向切面编程——Spring实现AOP方式——通过注解实现
- 从头认识Spring-1.2 什么是AOP?为什么需要面向切面编程?