配置属性加载的顺序java
数字小的优先级越高,即数字小的会覆盖数字大的参数值。spring
@Bean public PropertyPlaceholderConfigurer propertiess() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[]{new ClassPathResource("sys.properties")}; ppc.setLocations(resources); ppc.setIgnoreUnresolvablePlaceholders(true); return ppc; }
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:sys.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
@Bean public CommandLineRunner commandLineRunner() { return (args) -> { System.setProperty("name", "demo"); }; }
经过 @PropertySource 配置json
@PropertySource("classpath:sys.properties") @Configuration public class DemoConfig { }
// 只有使用注解 @PropertySource 的时候能够用,不然会获得 null。 @Autowired private Environment env; public String getUrl() { return env.getProperty("demo.jdbc.url"); }
@Value("${demo.jdbc.url}") private String url;
@Configuration @ConfigurationProperties(prefix = "demo.db") @Data public class DataBase { String url; String username; String password; }