尝试创建带有@Configuration 注释的类,就像在docs 中一样
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value = "classpath:myprops.properties")
public class MyConfiguration {
}
从MyProperties 类中删除@PropertySource 并将其放在MyConfiguration 中。
更新:
使其工作的步骤:
1) 转到https://start.spring.io。生成项目并添加:Spring Configuration Processor 和 Lombok(启用注释处理)
2) 添加或检查:
- myprops.properties 到资源(在 application.properties 旁边)
- MyConfiguration 和 MyProperties 旁边有 main 方法的类
package com.example.demostackannotations;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value = "classpath:myprops.properties")
public class MyConfiguration {
}
package com.example.demostackannotations;
import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
@Getter
@ConfigurationProperties
public class MyProperties {
private final String prop1;
private final Integer prop2;
private final String prop3;
@ConstructorBinding
public MyProperties(String prop1, Integer prop2, String prop3) {
this.prop1 = prop1;
this.prop2 = prop2;
this.prop3 = prop3;
}
}
- @ConfigurationPropertiesScan 到带有 main 方法的类
@SpringBootApplication
@ConfigurationPropertiesScan
public class DemoStackAnnotationsApplication {
public static void main(String[] args) {
ApplicationContext app = SpringApplication.run(DemoStackAnnotationsApplication.class, args);
MyProperties props = app.getBean(MyProperties.class);
System.out.println(props.getProp1());
System.out.println(props.getProp2());
System.out.println(props.getProp3());
}
}
<?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>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-stack-annotations</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-stack-annotations</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>