Spring学习笔记_27——@EnableLoadTimeWeaving

@EnableLoadTimeWeaving

1. 介绍

在Spring框架中,@EnableLoadTimeWeaving 是一个注解,它用于启用加载时织入(Load-Time Weaving, LTW)

LWT[Spring学习笔记_26------LWT-CSDN博客]

2. 场景

  • AOP:在Spring框架中,LTW通常与Spring AOP结合使用,以提供更强大的AOP功能。例如,可以定义切面来拦截特定的方法调用,并在这些方法调用前后执行额外的操作。
  • ORM:在一些对象关系映射(ORM)框架中,如Hibernate,LTW可以用来延迟加载实体属性或集合,而无需在运行时显式地配置代理。

3. 源码

java 复制代码
/**
 * @author Chris Beams
 * @since 3.1
 * @see LoadTimeWeaver
 * @see DefaultContextLoadTimeWeaver
 * @see org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(LoadTimeWeavingConfiguration.class)
public @interface EnableLoadTimeWeaving {
	AspectJWeaving aspectjWeaving() default AspectJWeaving.AUTODETECT;
	enum AspectJWeaving {
		ENABLED,    // 开启LTW支持
		DISABLED,   // 不开启LTW支持
		AUTODETECT; // 检测类路径下的META-INF目录下是否存在aop.xml文件,如果存在,则开启LTW支持,否则不开启LTW支持
	}
}

4. Demo

  • 配置类
java 复制代码
package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving;

@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
public class AppConfig {
}
  • 切面类
java 复制代码
package com.example.aspect;

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

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.service.UserService.login(..))")
    public void logBeforeLogin(JoinPoint joinPoint) {
        System.out.println("User is logging in: " + joinPoint.getSignature());
    }

    @After("execution(* com.example.service.UserService.logout(..))")
    public void logAfterLogout(JoinPoint joinPoint) {
        System.out.println("User has logged out: " + joinPoint.getSignature());
    }
}
  • 控制器类
java 复制代码
package com.example.controller;

import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/login")
    public String login() {
        userService.login();
        return "Logged in";
    }

    @GetMapping("/logout")
    public String logout() {
        userService.logout();
        return "Logged out";
    }
}
  • service 类
java 复制代码
package com.example.service;

public class UserService {

    public void login() {
        System.out.println("User is logging in...");
    }

    public void logout() {
        System.out.println("User is logging out...");
    }
}
  • AOP配置文件aop.xml
XML 复制代码
<aspectj>
    <weaver>
        <!-- Specify which packages to weave -->
        <include within="com.example..*"/>
    </weaver>
    <aspects>
        <!-- Specify which aspects to use -->
        <aspect name="com.example.aspect.LoggingAspect"/>
    </aspects>
</aspectj>
  • 启动
bash 复制代码
java -javaagent:/path/to/spring-instrument.jar -jar example.jar
相关推荐
敲代码的小王!2 小时前
MD5加密算法和BCrypt密码加密算法
java·算法·安全
李长渊哦6 小时前
使用Druid连接池优化Spring Boot应用中的数据库连接
数据库·spring boot·后端
web135085886356 小时前
【Spring Boot】Spring AOP动态代理,以及静态代理
spring boot·后端·spring
罗政7 小时前
冒险岛079 V8 整合版源码搭建教程+IDEA启动
java·ide·intellij-idea
架构默片7 小时前
【JAVA工程师从0开始学AI】,第五步:Python类的“七十二变“——当Java的铠甲遇见Python的液态金属
java·开发语言·python
zzyh1234568 小时前
springcloud的组件及作用
后端·spring·spring cloud
不只会拍照的程序猿8 小时前
从插入排序到希尔排序
java·开发语言·数据结构·算法·排序算法
尚学教辅学习资料8 小时前
基于SpringBoot的图书借阅小程序+LW参考示例
spring boot·后端·小程序·java毕设·图书借阅
一 乐8 小时前
高校体育场管理系统系统|体育场管理系统小程序设计与实现(源码+数据库+文档)
前端·javascript·数据库·spring boot·高校体育馆系统
山海不说话8 小时前
从零搭建微服务项目Base(第5章——SpringBoot项目LogBack日志配置+Feign使用)
spring boot·后端·spring·spring cloud·微服务·logback