一、先说问题:
IDEA中Lombok不生效。如图:
二、所用版本:
1、64 位操作系统, 基于 x64 的处理器,Windows 11 家庭中文版。
2、JDK 21、apache-maven-3.9.6-bin。
3、IntelliJ IDEA 2024.3.1.1 (Ultimate Edition)
三、构建:
1、生成器选择 Spring Boot(不知道为什么时候变成了Spring Boot,以前是Spring Initializr)
2、类型选择 Maven
3、Java 选择 21
4、添加依赖 Lombok、Spring Web
四、文件结构:
五、文件内容:
只动了三个文件:修改application.properties 为 application.yml;新建PersonInfo、DispInfo。内容如下:
application.yml:
spring:
application:
name: d03
server:
port: 8081
servlet:
session:
timeout: 30
tomcat:
uri-encoding: UTF-8
name: LaoTou
age: 50
personinfo:
name: Tudi
age: 30
PersonInfo:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "personinfo")
public class PersonInfo {
private String name;
private int age;
}
DispInfo:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;
@Component
@SpringBootTest
@RunWith(SpringRunner.class)
public class DispInfo {
@Value("${name}") // 获取配置文件中的 name
private String name;
@Value("${age}") // 获取配置文件中的 age
private int age;
@Autowired // 自动装配。在非测试类中,建议使用其他方法注入
PersonInfo personInfo = new PersonInfo();
@Test // 该注解表示一个测试方法
public void getNameAndAge() {
System.out.println("姓名:"+name+" 年龄:"+age);
}
@Test
public void getPersonInfo() {
System.out.println("姓名:"+personInfo.getName()+" 年龄:"+personInfo.getAge());
}
}
六、pom.xml 文件没动,自动生成的:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.afy</groupId>
<artifactId>d03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>d03</name>
<description>d03</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
七、用过的方法:
1、Lombok 插件已安装;2、java compiler - use compiler 已选 javac;3、enable annotation processiing 已勾选;4、Invalidate Caches 动作做过;5、java compiler与project settings 里JAVA版本一致。
问题没有解决。
八、最后:第一次来此发贴,不知道行情,先20元