[英]<util:properties> equivalent in java based configuration for spring
基於 XML 的 spring 配置的基於 Java 的配置中的等價物是什么
<util:properties id="mapper" location="classpath:mapper.properties" />
然后能夠在如下代碼中使用這個特定的屬性對象:
@Resource(name = "mapper")
private Properties myTranslator;
看着文檔,我看了看
@PropertySource
注釋,但在我看來,特定的屬性文件將無法從 Environment 對象中單獨訪問。
很簡單,聲明一個PropertiesFactoryBean
。
@Bean(name = "mapper")
public PropertiesFactoryBean mapper() {
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource("com/foo/jdbc-production.properties"));
return bean;
}
在這里的文檔中,您會注意到在他們制作<util:properties>
,他們曾經使用PropertiesFactoryBean
這樣
<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:com/foo/jdbc-production.properties"/>
</bean>
如上所示,將其轉換為 Java 配置非常簡單。
在 Spring Boot 5+ 中,src/main/resources 下的 application.properties 是約定。 我發現如果您需要放置自己的自定義屬性,例如“student.class.name”,那么 Spring 將自動在 Eclipse 中驗證它並使用警告標記該條目,並提供為其創建元數據的選項。 單擊它,它將在 META-INF 下創建一個 JSON 文件。 用那個。 然后你可以使用 @Value("${student.class.name}" 注釋注入屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.