Springmvc获取properties中的内容实践

本文介绍了在Springmvc中如何正确地获取properties配置文件的内容,通过实测解决了其他教程存在的问题,提供了一种有效的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springmvc获取properties中的内容实践
关于spring和springmvc的帖子很多,很多技术极少也很详细,也有关于获取properties内容的介绍。但是主流的几个帖子,我都尝试了,有些还是有问题,经过实测,给出其中一种方法。
1.spring-mvc.xml加入如下内容

xmlns:util="http://www.springframework.org/schema/util

http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!-- 引入配置文件-->
<util:properties id="settings" location="classpath:conf.properties"/>
<context:component-scan base-package="com.sohuporndetect"/>
<!-- com.sohuporndetect 注入类所在的包名-->
2.注入类

package com.sohuporndetect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
* Created by jjc on 2017/7/24.
*/
@Service
public class Conf {
@Value("#{settings['a']}")
public String aa;
@Value("#{settings['b']}")
public String bb;
}

3.con.properties

a=123
b=aaaaa
注意,等号右边字符串不需要使用双引号

4.使用---在某个类中注入
@Autowired
private Conf config;
在非静态代码块下可以使用如下方式调用;
@Autowired
private Conf config;









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值