yml 和 properties 注入屬性方式
阿新 • • 發佈:2018-12-30
guns
1.yml 方式
yml 檔案guns 下直接寫
conversionUrl: http://172.16.10.90/cgi-bin/soapcgi-dir
在需要注入屬性的Java檔案下直接進行注入
@Value("${guns.conversionUrl}")
private String ITEM_URL;
2.properties 方式
1、在applicationContext.xml配置檔案中,引入<util />名稱空間。
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
2、配置註解元件掃描,用註解來自動注入
<context:component-scan base-package="com.packagename" ></context:component-scan>
3、在classpath路徑下建立屬性檔案,如sys.properties
test=sysdata
4、 讓Spring載入屬性檔案,在applicationContext.xml 中配置
<util:properties id="sys" location="classpath:sys.properties"/>
5、建立java檔案,讓Spring注入從資原始檔中讀取到的屬性的值,如下
@Component public class SysConf { @Value("#{sys.test}") private String test; @Value("#{sys.test}") public void setTest(String test){ test = test; } @Value("#{sys}") public void setSysConf(Properties sys){ test= sys.getProperty("test"); } }