1. Add rely pom.xml
<!-- springboot configuration依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-configuration-processor</artifactId>
<optional> true </optional>
</dependency>
2. In the resources to build a config package (package name at random of course), to build a remote.properties in the bag (the old rules, random file name)
3. Write test content in the configuration file
remote.testname=张三
remote.testpass=123456
4. Write an entity class, attributes, and profiles corresponding to
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 inttest sessions; }
5. do things in the profile information call class
@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; } }
PS: Some small partners to obtain profiles appeared in Chinese garbage problem, please visit the following blog
https://www.cnblogs.com/zhainan-blog/p/11460488.html