SpringBoot project combat (8): four ways to read properties file
Although @Value is to go to the default configuration, it should be noted that there are spring.profiles.active=prod|dev If this switch is used, only the properties under the corresponding configuration file can be read and placed in the application.properties file, it will return empty.
Spring's @PropertySource and @Value annotation examples
@Component(value = "mockConfigTest") public class MockConfigTest { @Value("#{'${server.name}'.split(',')}") private List<String> servers; @Value("#{'${server.id}'.split(',')}") private List <Integer> serverId; @Value("${server.host:127.0.0.1}") private String noProKey; @Autowired private Environment environment; public void readValues() { System.out.println("Services Size : " + servers.size()); for(String s : servers) System.out.println(s); System.out.println("ServicesId Size : " + serverId.size()); for (Integer i: serverId) System.out.println(i); System.out.println("Server Host : " + noProKey); String property = environment.getProperty("server.jdbc"); System.out.println("Server Jdbc : " + property); } public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfigTest.class); MockConfigTest mockConfigTest = (MockConfigTest) annotationConfigApplicationContext.getBean("mockConfigTest"); mockConfigTest.readValues(); } }
more>>