pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-configuration-processor</artifactId>
<optional> true </optional>
</dependency>
在resources下建一个config包(当然包名随意), 在包里建一个remote.properties(老规矩, 文件名随意)
配置中写入测试内容
写一个实体类,属性与配置文件对应
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Configuration
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false)
@PropertySource("classpath:config/remote.properties")
@Data
@Component
public class RemoteProperties {
private String testname;
private int testpass;
}
调用配置文件信息
@EnableConfigurationProperties(RemoteProperties.class)
@RestController
public class PageTestController {
@Autowired
RemoteProperties remoteProperties;
@RequestMapping("testProperties")
public String testProperties(){
String str = remoteProperties.getTestname();
int i = remoteProperties.getTestpass();
System.out.println(str);
System.out.println(i);
return str+i;
}
}