在Spring Boot项目中,`@SpringBootTest` 和 `@RunWith(SpringRunner.class)` 是用于测试类的注解

在Spring Boot项目中,@SpringBootTest@RunWith(SpringRunner.class) 是用于测试类的注解,它们一起开启了基于Spring应用上下文的集成测试。@Autowired 注解用于自动装配依赖到 IService 上,这是通过Spring AOP(面向切面编程)实现的。

testAop() 方法中,当我们调用 service.getClass() 时,实际返回的是Service接口的一个代理类 (Service1$$EnhancerBySpringCGLIB$$f107fe5e)。这通常是Spring AOP创建的,它包含了Service1的实际逻辑并可能添加了一些额外的功能,如切面通知(如事务管理、日志记录等)。

关于spring-boot-starter-aop的配置,有两个关键点:

  1. 默认情况下,Spring Boot会启用AspectJ自动代理 (proxyTargetClass=false),这意味着它会在运行时动态创建代理类。如果你希望使用CGLIB(Code Generation Library)代替(即生成字节码),可以设置spring.aop.proxy-target-class 属性为 true。例如,在你的@Configuration类中,你可以这样声明:
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AopConfig {
   
   
    // ...
}
  1. 如果proxy-target-class属性未显式设置,Spring Boot会查找该属性是否存在,如果存在且值为 true,则按照指定方式启用代理。@ConditionalOnProperty 则允许你基于特定的配置条件来决定某个配置的生效与否。

要了解更多关于Spring Boot AOP的细节,可以查阅Spring-Boot-Starter-AOP的官方文档或者深入分析@EnableAspectJAutoProxy@Aspect注解的工作原理。
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

package com.programb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
   
   
    public static void main(String[] args) {
   
   
        SpringApplication.run(Application.class, args);
    }

}

package com.programb.aop;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


@Component
@Order(1)
public class AfterStartRunner implements CommandLineRunner {
   
   

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public void run(String... args) {
   
   
        logger.debug("after start debug...");
        logger.info("after start info...");
    }
}

package com.programb.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 日志切面
 */
@Aspect
@Component
public class LogAspect {
   
   
    @Pointcut("execution(public * com.xncoding.aop.controller.*.*(..))")
    public void webLog(){
   
   }

    @Before("webLog()")
    public void deBefore(JoinPoint joinPoint) throws Throwable {
   
   
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        System.out.println("URL : " + request.getRequestURL().toString());
        System.out.println("HTTP_METHOD : " + request.getMethod());
        System.out.println("IP : " + request.getRemoteAddr());
        System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bol5261

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值