Springboot中利用AOP是实现日志拦截记录

目的:

利用SpringBoot做日志记录。

说明:

在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发,然后把切面功能和核心业务功能 “编织” 在一起,这就叫AOP。日志记录和核心业务功能分开,方便日志的管理维护。

实现步骤:

1. 核心代码

@Component("coreService")
public class CoreService {

    public void coreMathod() {
        // 仅仅只是实现了核心的业务功能
        System.out.println("实现核心的业务功能");
         
    }

2. 日志代码


import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
class Logger{

    @Before("execution(* service.service())")
    public void before(){
        System.out.println("前置日志输出");
    }

    @After("execution(* service.service())")
    public void after(){
        System.out.println("后置日志输出");
    }
}

3. applicationContext.xml 中配置自动注入,设置包名扫描这两个 Bean:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="service" />
    <context:component-scan base-package="logservice" />

    <aop:aspectj-autoproxy/>
</beans>

4. 测试

public class Service01 extends ServiceEBase {
	@Autowired
    public CoreService coreService;

	public EiInfo demoService() {
        //ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/*.xml");
	//        ApplicationContext context = new 		ClassPathXmlApplicationContext("applicationContext.xml");
	//CoreService coreService= (CoreService ) context.getBean("coreService", CoreService .class);
	//   也可以用自动注入  @Autowired            
        coreService.service();
        

    }
}

    
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Spring Boot使用AOP的步骤如下: 1. 定义切面 在Spring Boot,可以使用`@Aspect`注解定义切面类。例如: ```java @Aspect @Component public class MyAspect { @Before("execution(* com.example.demo.service.UserService.*(..))") public void beforeMethod(JoinPoint joinPoint) { System.out.println("方法执行前:" + joinPoint.getSignature().getName()); } @After("execution(* com.example.demo.service.UserService.*(..))") public void afterMethod(JoinPoint joinPoint) { System.out.println("方法执行后:" + joinPoint.getSignature().getName()); } } ``` 在上面的示例,我们定义了一个名为`MyAspect`的切面类,并使用`@Before`和`@After`注解定义了两个方法。这两个方法分别在目标方法执行前和执行后输出日志。`execution(* com.example.demo.service.UserService.*(..))`表示拦截`com.example.demo.service.UserService`类的所有方法。 2. 启用AOP 在启动类上添加`@EnableAspectJAutoProxy`注解,启用AOP功能。例如: ```java @SpringBootApplication @EnableAspectJAutoProxy public class MyApp { // ... } ``` 3. 在目标方法上使用注解 在需要拦截的方法上添加注解。例如: ```java @Service public class UserService { @MyAnnotation public void addUser(User user) { // ... } public void deleteUser(int id) { // ... } } ``` 在上面的示例,我们在`addUser()`方法上添加了`@MyAnnotation`注解,表示该方法需要被拦截。而`deleteUser()`方法没有被添加注解,不会被拦截。 当程序运行时,如果调用了带有`@MyAnnotation`注解的方法,切面定义的`beforeMethod()`方法和`afterMethod()`方法就会被执行,输出相应的日志。 需要注意的是,切面类和注解都应该被扫描到。可以使用`@ComponentScan`注解指定需要扫描的包。例如: ```java @SpringBootApplication @ComponentScan(basePackages = {"com.example.demo.aspect", "com.example.demo.service"}) @EnableAspectJAutoProxy public class MyApp { // ... } ``` 这样,Spring Boot就会自动扫描`com.example.demo.aspect`和`com.example.demo.service`两个包下的类,并将其应用到目标方法上。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值