您的位置:首页 > 产品设计 > UI/UE

解决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

<!-- 自动扫描且只扫描@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" />




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐