springboot之AOP配置

package com.didi.controller;

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.springframework.stereotype.Component;

@Aspect
@Component
public class SleepHelper {

@Before("sleep()")
public void beforeSleep(){
    System.out.println("睡前刷牙---------");
}

@Pointcut("execution(* com.didi.domain.*.*(..))")
public void sleep(){

};

@After("sleep()")
public void wakeUp(){
    System.out.println("醒来洗漱--------------");
}

}

package com.didi.domain;

import org.springframework.stereotype.Component;

@Component
public class Man {

public void study(){
    System.out.println("我正在学C++");
}

}

package com.didi;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtils implements ApplicationContextAware {

private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
    System.out.println("容器保存成功-----------");
}

public static ApplicationContext getApplicationContext(){
    return applicationContext;
}

}

package com.didi.controller;

import com.didi.SpringUtils;
import com.didi.domain.Man;
import org.hibernate.service.spi.InjectService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class AopController {

@RequestMapping("aopMapping")
public void aop(){
    Man man = (Man)SpringUtils.getApplicationContext().getBean("man");
    man.study();
}

}
访问:http://localhost:9006/aopMapping
控制台打印:
睡前刷牙———
我正在学C++
醒来洗漱————–

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot配置AOP(面向切面编程),可以通过以下步骤进行: 1. 首先,在你的Spring Boot项目中添加所需的依赖。在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 2. 创建一个切面类,用于定义你的切面逻辑。切面类需要使用`@Aspect`注解进行标记,并且通常需要在类中定义多个切点和通知。例如: ```java @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.demo.service.*.*(..))") public void beforeAdvice(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature()); } @After("execution(* com.example.demo.service.*.*(..))") public void afterAdvice(JoinPoint joinPoint) { System.out.println("After method: " + joinPoint.getSignature()); } } ``` 上述切面类中,`@Before`注解表示在目标方法执行之前执行,`@After`注解表示在目标方法执行之后执行。`execution(* com.example.demo.service.*.*(..))`表示匹配`com.example.demo.service`包下的所有类的所有方法。 3. 在Spring Boot应用程序的主类上添加`@EnableAspectJAutoProxy`注解,启用AOP代理。例如: ```java @SpringBootApplication @EnableAspectJAutoProxy public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4. 运行你的Spring Boot应用程序,并观察控制台输出。你应该能够看到在匹配的方法执行前后打印的日志信息。 这就是在Spring Boot配置AOP的基本步骤。你可以根据需要定义更多的切点和通知,以实现更复杂的切面逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值