Properties 配置文件参数 注入bean中

需求叙述: Properties 配置文件参数的获取,之前日志中有提到获取项目配置文件地址,读取properties获取参数。现在需要将config.properties文件的参数注入到BaseController类,以便所有继承的子类都可以获取使用。

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>

此时的数据库配置文件项目路径是这样的 输入图片说明

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文件的位置如下图所示 输入图片说明

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>

3.BaseController父类

@Autowired//注入 private String fileUrl;

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>

转载于:https://my.oschina.net/maoguangdong/blog/775943

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值