配置屬性加載的順序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; }