独角兽企业重金招聘Python工程师标准>>>
需求叙述: 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>