开发者社区> 谙忆> 正文

【Spring】Spring基础配置-AOP

简介: 转载请注明出处:http://blog.csdn.net/qq_26525215 本文源自【大学之旅_谙忆的博客】 分析 AOP: 面向切面编程,相对于OOP面向对象编程。
+关注继续查看

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自大学之旅_谙忆的博客

分析

AOP: 面向切面编程,相对于OOP面向对象编程。
OOP: Object Oriented Programming,面向对象的程序设计。

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)

本示例演示基于注解拦截和基于方法拦截两种方式,演示一种模拟记录操作的日志系统的实现。

其中注解式拦截能够很好地控制要拦截的粒度和获得更丰富的信息,Spring本身在事务处理(@Transcational)和数据缓存(@Cacheable等)上面都使用此种形式的拦截。

pom.xml的配置不再累赘写上。
不知道写的朋友请见此篇博客:
http://blog.csdn.net/qq_26525215/article/details/53010442

示例

添加Spring aop支持及AspectJ依赖

<!-- spring aop 支持-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>

        <!-- aspectj支持 -->
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>

编写拦截规则的注解

package cn.hncu.p1_3_3_aop;

import java.lang.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/9.
 * Time: 上午 11:11.
 * Explain:注解类
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
    String name();
}

编写使用注解的被拦截类

package cn.hncu.p1_3_3_aop;

import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/9.
 * Time: 上午 11:16.
 * Explain:使用注解的被拦截类
 */
@Service
public class DemoAnnotationService {
    @Action(name = "@Action---DemoAnnotationService.add操作")
    public void add(){
        System.out.println("DemoAnnotationService.add...");
    }
}

编写使用方法规则的被拦截类

package cn.hncu.p1_3_3_aop;

import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/9.
 * Time: 上午 11:23.
 * Explain:使用方法规则的被拦截类
 */
@Service
public class DemoMethodService {
    @Action(name="@Action---DemoMethodService.add操作")
    public void add(){
        System.out.println("DemoMethodService.add...");
    }
}

编写切面

package cn.hncu.p1_3_3_aop;

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;

import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/9.
 * Time: 上午 11:24.
 * Explain:切面
 */
@Aspect
//通过@Aspect注解声明这是一个切面
@Component
//通过@Component让此切面成为Spring容器管理的Bean
public class LogAspect {

    //注意这两个@Pointcut写法的区别!!!
    // 一个是拦截注解(写了@Action注解的方法都会被拦截),一个是拦截类方法
    @Pointcut("@annotation(cn.hncu.p1_3_3_aop.Action)")//通过@PointCut注解声明切点
    //@Pointcut("execution(* cn.hncu.p1_3_3_aop.DemoAnnotationService..*(..))")
    //配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
    public void annotatiomPointCut(){ }

    //拦截注解
    @After("annotatiomPointCut()")//通过@After注解声明一个建言,并使用@PointCut定义的切点
    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("execution(* cn.hncu.p1_3_3_aop.DemoMethodService.*(..))")
    //通过@Before注解声明一个建言,此建言直接使用拦截规则作为参数
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        System.out.println("方法规则式拦截:" + method.getName()+"  "+action.name());
    }


}

编写配置类

package cn.hncu.p1_3_3_aop;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/9.
 * Time: 上午 11:42.
 * Explain:配置类
 */
@Configuration
@ComponentScan("cn.hncu.p1_3_3_aop")
@EnableAspectJAutoProxy //使用@EnableAspectJAutoProxy注解开启Spring对AspectJ代理的支持
public class AopConfig {
}

运行类

package cn.hncu.p1_3_3_aop;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/9.
 * Time: 上午 11:41.
 * Explain:运行类
 */
public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
        DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
        demoAnnotationService.add();

        DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
        demoMethodService.add();

        context.close();
    }
}

运行结果

项目链接—具体包:
https://github.com/chenhaoxiang/Java/tree/master/springBoot/src/main/java/cn/hncu/p1_3_3_aop

本文章由[谙忆]编写, 所有权利保留。

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自大学之旅_谙忆的博客

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Spring AOP在项目中的典型应用场景
Spring AOP在项目中的典型应用场景
13 0
Spring 的AOP 简介,面向切面编程AOP
Spring 的AOP 简介,面向切面编程AOP
21 0
Spring JDBC-实施Spring AOP事务注意事项及案例分析
Spring JDBC-实施Spring AOP事务注意事项及案例分析
30 0
【Spring】核心部分之AOP:通过列举代码例子,从底层刨析,深入源码,轻轻松松理解Spring的核心AOP,AOP有这一篇足以
【Spring】核心部分之AOP:通过列举代码例子,从底层刨析,深入源码,轻轻松松理解Spring的核心AOP,AOP有这一篇足以
14 0
BXA
Spring AOP实现面向切面编程
AOP(Aspect-Oriented Programming面向切面编程)是一种用于解决传统 OOP(Object-Oriented Programming 面向对象编程)难以解决的问题的编程思想。AOP 能够将系统中的横切关注点(例如日志、安全、事务等),从主逻辑中分离出来使得代码更加清晰、模块化、易于扩展和维护。
33 0
Spring--快速入门AOP
Spring--快速入门AOP
20 0
【Spring】AOP 统一问题处理
1. 什么是 Spring AOP 2. 为什么要用 AOP 3. AOP 组成 3.1 切面(Aspect) 3.2 连接点(Join Point) 3.3 切点(Pointcut) 3.4 通知(Advice) 4. Spring AOP 实现 4.1 添加 AOP 框架支持 4.2 定义切面和切点 4.2.1 切点表达式说明 4.2.2 表达式示例 4.3 定义相关通知 5. Spring AOP 实现原理 5.1 织入(Weaving):代理的生成时机 5.2 动态代理 5.2.1 JDK 动态代理实现 5.2.2 CGLIB 动态代理实现 5.2.3 JDK 和 CGLIB 的区别
20 0
面试官问 Spring AOP 中两种代理模式的区别,我懵逼了
基本介绍 代理模式是一种结构性设计模式。为对象提供一个替身,以控制对这个对象的访问。即通过代理对象访问目标对象,并允许在将请求提交给对象前后进行一些处理。 被代理的对象可以是远程对象、创建开销大的对象或需要安全控制的对象。 代理模式主要有三种不同的形式: 静态代理:由程序员创建代理类或特定工具自动生成源代码再对其编译。在程序运行前代理类的 .class 文件就已经存在了 动态代理(JDK 代理、接口代理):在程序运行时运用反射机制动态创建而成,动态就是在程序运行时生成的,而不是编译时。 cglib 代理(可以在内存动态的创建对象,而不是实现接口,属于动态代理的范畴)
30 0
【Java框架型项目从入门到装逼】第二节 - Spring框架 AOP的丧心病狂解说,你喜欢露娜的月下无限连吗?
【Java框架型项目从入门到装逼】第二节 - Spring框架 AOP的丧心病狂解说,你喜欢露娜的月下无限连吗?
51 0
【SSM】Spring AOP 统一问题处理(重点:Spring AOP 实现原理)
本文重点介绍AOP 的定义、Spring AOP与AOP的关系、AOP 组成、Spring AOP 实现 和 Spring AOP 实现原理(对象代理)
19 0
+关注
谙忆
GitHub: https://github.com/chenhaoxiang
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
相关实验场景
更多