Spring学习(一)Aop两种方法实现日志记录demo 切片编程

AOP

面向切片编程,Spring中使用切片编程的目的是为了实现解耦,AOP可以实现一组类共享相同行为。

使用注解式拦截代码

maven依赖

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <!--AOP支持-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <!--AspectJ支持-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.5</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.5</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
  • 编写注解
package day0123.AOP01;

import java.lang.annotation.*;

/**
 * @Author Braylon
 * @Date 2020/1/23 20:40
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
    String info();
}

  • 被拦截类
package day0123.AOP01;

import org.springframework.stereotype.Service;

/**
 * @Author Braylon
 * @Date 2020/1/23 20:43
 */
@Service
public class DemoAnnotationService {
    @Action(info = "注解式拦截的业务操作")
    public void service1() {
        //业务操作
        System.out.println("注解式拦截的业务逻辑");
    }
}

方法规则拦截代码

被拦截类

package day0123.AOP01;

import org.springframework.stereotype.Service;

/**
 * @Author Braylon
 * @Date 2020/1/23 20:47
 */
@Service
public class DemoMethodService {
    public void service1() {
        //业务逻辑
        System.out.println("方法规则拦截业务逻辑");
    }
}

切面

package day0123.AOP01;

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;

/**
 * @Author Braylon
 * @Date 2020/1/23 20:49
 */
@Aspect
@Component
public class LogAspect {
    @Pointcut("@annotation(day0123.AOP01.Action)")
    public void annotationPointCut(){};

    @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.info());
    }

    @Before("execution(* day0123.AOP01.DemoMethodService.*(..))")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法规则拦截" + method.getName());
    }
}

配置类

package day0123.AOP01;

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

/**
 * @Author Braylon
 * @Date 2020/1/23 20:58
 */
@Configuration
@ComponentScan("day0123.AOP01")
@EnableAspectJAutoProxy //开启Spring对AspectJ的支持
public class conf {}

测试

package day0123.AOP01;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Author Braylon
 * @Date 2020/1/23 20:47
 */
public class actionTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(conf.class);
        DemoAnnotationService demoannotationservice = context.getBean(DemoAnnotationService.class);
        DemoMethodService demomethodservice = context.getBean(DemoMethodService.class);

        demoannotationservice.service1();
        demomethodservice.service1();

        context.close();
    }
}

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值