Controller使用注解@Value加载配置文件 --SpringMVC

Controller使用注解@Value加载配置文件默认是获取不到值的但是在Service和DAO里可以使用注解@Value加载配置的值,因为SpringMVC使用的DispatcherServlet,跟Spring ContextLoaderListener是两个不同的容器!需要在web.xml里重新加载一次properties的配置文件,才能使用@Value获取到配置文件中的值。

//contrller 读取配置文件
@Value("${system.username}")
private String username;

配置如下:

    <!-- spring mvc DispatcherServlet -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            classpath*:/config/framework/app-springmvc.xml
            <!--加载app-properties的配置  -->
            classpath*:/config/framework/app-properties.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

properties配置如下:

    <bean id="configProperties" class="com.fooduo.sys.utils.dbproper.DatabaseProperties">
        <property name="locations">
            <list>
            <!--可以配置多个 -->
                <value>classpath:/properties/jdbc.properties</value>
                <value>classpath:/properties/system.properties</value>
            </list>
        </property>
        <property name="fileEncoding">
            <value>UTF-8</value>
        </property>
    </bean>

如需把配置文件存放到数据库进行管理 可以查阅上一篇文章!!!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值