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>
相关文章
相关标签/搜索