Properties 配置文件參數 注入bean中

需求敘述: Properties 配置文件參數的獲取,以前日誌中有提到獲取項目配置文件地址,讀取properties獲取參數。如今須要將config.properties文件的參數注入到BaseController類,以便全部繼承的子類均可以獲取使用。javascript

1.配置文件的加載前端

a.

<!-- 使用spring提供的PropertyPlaceholderConfigurer讀取數據庫配置信息.properties -->
         <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                   <property name="locations">
                            <list>
                                     <!—
    這裏的classpath能夠認爲是項目中的src-
    屬性名是 locations,使用子標籤<list></list>能夠指定多個數據庫的配置文件,這裏指定了一個
->
                                     <value>classpath:resource/config/jdbc.properties</value>
                            </list>
                   </property>
         </bean>

此時的數據庫配置文件項目路徑是這樣的 輸入圖片說明java

b.

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="locations">

      <list>

         <value>/WEB-INF/config_test/jdbc.properties</value>

      </list>

    </property>

  </bean>

此時jdbc.properties文件的位置以下圖所示 輸入圖片說明spring

c.

或者放一塊兒:數據庫

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="locations">

      <list>

        <value>classpath:jdbc.properties</value>

        <value>/WEB-INF/config_test/jdbc.properties</value>

      </list>

    </property>

  </bean>

2.properties裏面的信息讀進來 對象中(可自定義): 如String: ``` <bean id="fileUrl" class="java.lang.String"> <constructor-arg value="${fileUrl}"/> </bean>this

3.BaseController父類

@Autowired//注入 private String fileUrl;spa

public String getFileUrl() {
	return fileUrl;
}

public void setFileUrl(String fileUrl) {
	this.fileUrl = fileUrl;
}
前端使用:
<a onclick="javascript:window.open('${fileUrl}')" type="button" class="btn btn-success btn-sm">生成報表</a>
相關文章
相關標籤/搜索