[英]Spring Dependency Injection via @Configuration, @Bean and @Component
[英]Spring Util:Properties Injection via Annotations into a bean
如果我在Spring XML中設置了2個.properties文件,那么:
<util:properties id="serverProperties" location="file:./applications/MyApplication/server.properties"/>
<util:properties id="someConfig" location="file:./applications/MyApplication/config.properties"/>
如何通過注釋將這些屬性文件注入到帶有java.util.Properties
的bean java.util.Properties
?
如何通過Spring注釋獲取特定屬性?
干杯!
@Autowired
@Qualifier("serverProperties")
private Properties serverProperties;
@Autowired
@Qualifier("someConfig")
private Properties otherProperties;
要么
@Resource(name = "serverProperties")
private Properties serverProperties;
@Resource(name = "someConfig")
private Properties otherProperties;
通常,@ Autowired用於Spring中的類型自動裝配,而@Resource用於按名稱。 @ Autowired + @ Qualifier可以兼作自動裝配,但它真正意味着可以通過微調類型來實現類型自動裝配。
因為這個問題有很多熱門話題。 我認為使用SpEL(Spring Expression Language)指出另一個選項是值得的 - 如果你需要特定的屬性,可以使用特定bean屬性的@Value注釋注入它們;
class SomeClass {
@Value("#{serverProperties['com.svr.prop']}")
private String aServerCfgProperty;
@Value("#{someConfig['another.config.setting']}")
private String someOtherProperty;
}
你不需要使用索引語法['index.val']
你可以直接得到它;
@Value("#{someConfig}")
private Properties someConfig
@Value("#{serverProperties}")
private Properties svrProps;
我發現這很有用,並且不再使用通過@ Resource / @ Autowired直接注入的屬性對象。
將@Value
與索引的Properties對象一起使用的另一個好理由是,如果項目中的.properties文件也很好,某些IDE(例如IntelliJ)可以重構實際的屬性名稱。 如果你想在屬性文件中進行包含/嵌套/替換而不使用Spring的PropertiesPlaceholderConfigurer
類(遺憾的是它沒有公開它的屬性 - 使用SpEL索引['key']
],那么另一個提示是使用類似EProperties (擴展本機Java Properties對象)之類的東西。 ['key']
bean需要是Map<>
的實例,即Java Properties對象所做的擴展map ...
最后,SpEL的另一個優點是你可以直接訪問bean的屬性。 所以說,例如,如果上面的例子中的SomeClass
是一個Spring bean,例如someClass
那么在AnotherBeanClass中我們可以擁有;
@Value("#{someClass.someOtherProperty}")
private String injectedBeanProp
你也可以調用一個getter方法:
@Value("#{someClass.getSomeOtherProperty()}")
private String injectedBeanProp
您可以使用@PropertySource
@Configuration
@PropertySource(name = "someName", value = {"classpath:a.properties", "classpath:b.properties"})
public class MyConfiguration {
}
XMl文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:component-scan base-package="com.sha.home" />
<mvc:annotation-driven/>
<util:properties id="dbProp" location="classpath:db.properties" />
<!-- <context:property-placeholder location="classpath:db.properties"/> -->
</beans>
在java文件中@Value(“#{dbProp}”)私有屬性dbProperties;
的System.out.println( “urllll” + dbProperties.getProperty( “jdbc.url”));
大部分時間我將所有屬性封裝到一個實用程序中並在我的應用程序中使用。 這樣,您無需擔心/管理應用層中的每個屬性文件。 自動裝配的setProps(...)將所有加載的util:properties讀入到props列表中。
import java.util.List;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AppProperiesProcessor {
private List<Properties> props;
private Properties mergedProperties;
@Autowired
public final void setProps(List<Properties> props) {
this.props = props;
}
public String getProp(final String keyVal) {
if (null == this.mergedProperties) {
this.mergedProperties = new Properties();
for (Properties prop : this.props) {
this.mergedProperties.putAll(prop);
}
}
return mergedProperties.getProperty(keyVal);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.