解决controller无法使用@value注解读取properties文件的问题
2017-07-19 15:20
706 查看
我是在controller用,因为子父容器关系,要读取到配置文件在spring的容器中,直接在controller是跨容器,所以先注入到service层,然后再controller层注入service,就可以调用service的public属性成员变量。
在Spring初始化时,就把properties文件装载到Spring的上下文中。
Spring父容器负责所有其他非@Controller注解的Bean的注册,而SpringMVC只负责@Controller注解的Bean的注册,使得他们各负其责、明确边界。
Properties
props = new Properties();
try {
props = PropertiesLoaderUtils.loadAllProperties("application.properties");
} catch (IOException e) {
e.printStackTrace();
}
String url = props.getProperty("portalUrl");
项目中是这么使用的为controller层注入properties
在Spring初始化时,就把properties文件装载到Spring的上下文中。
Spring父容器负责所有其他非@Controller注解的Bean的注册,而SpringMVC只负责@Controller注解的Bean的注册,使得他们各负其责、明确边界。
Properties
props = new Properties();
try {
props = PropertiesLoaderUtils.loadAllProperties("application.properties");
} catch (IOException e) {
e.printStackTrace();
}
String url = props.getProperty("portalUrl");
项目中是这么使用的为controller层注入properties
<!-- 自动扫描且只扫描@Controller --> <context:component-scan base-package="com.topstcn.web,com.winner.kl.web" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan> <!-- 单独为webapplicationContext注入placeholder --> <context:property-placeholder ignore-unresolvable="true" location="classpath:application.properties" />
相关文章推荐
- spring使用@Value注解读取.properties文件时出现中文乱码问题的解决
- 解决“Spring 使用MapperScannerConfigurer无法读取*.properties文件"的问题
- spring使用@Value标签读取*.properties文件的中文乱码问题的解决
- spring使用@Value标签读取.properties文件的中文乱码问题的解决
- spring使用@Value标签读取.properties文件的中文乱码问题的解决
- Myeclipse中文件已经上传到server文件夹下,文件也没有被占用,可是页面中无法读取和使用问题的解决方法
- 解决Spring配置文件无法读取properties属性问题
- springmvc通过@Value注解读取Properties配置文件的值,junit测试可以取到值,但是在业务中无法读取
- c# 读取其他程序正打开的文件的时“正由另一进程使用,因此该进程无法访问该文件。"的问题解决方法
- Myeclipse中文件已经上传到服务器目录下,文件也没有被占用,但是页面中无法读取和使用问题的解决方法
- 解决Java读取properties文件的中文问题的新办法(不使用native2ascii.exe及其他工具)
- 解决IntelliJ IDEA无法读取配置*.properties文件的问题
- 解决“使用驱动器F中的光盘前需要将其格式化 双击后提示:文件目录损坏且无法读取”问题
- 解决Java读取properties文件的中文问题的新办法(不使用native2ascii.exe及其他工具)
- 一个使用FFmpeg库读取3gp视频的例子-Android中使用FFmpeg媒体库(三).so文件编译过程问题的解决
- (转载)解决IIS7虚拟目录出现HTTP 错误 500.19(由于权限不足而无法读取配置文件)的问题
- 解决VMWare 9中提示“windows无法从无人参与应答文件读取<ProductKey>设置问题”
- 解决Java读取properties文件中文乱码的问题
- 解决IIS7虚拟目录出现HTTP 错误 500.19(由于权限不足而无法读取配置文件)的问题
- chm文件的索引无法使用问题解决方法