spring 获取 properties的值方法

在spring.xml中配置

很奇怪的是,在context-param 加载的spring.xml 不能使用 ${xxx}

必须交给DispatcherServlet 管理的 springMVC.xml才能用?

要交给springMVC的DispatcherServlet去扫描,而不是spring的监听器ContextLoaderListener去扫描,就可以比较方便的使用“${xxx}”去注入。

1、使用 $ 获取属性

@Value("${user.name}")

private String userName;

<!--方法1-->
 <context:property-placeholder location="classpath*:info/info.properties" />
<!--方法2-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="locations">
<list>
<value>classpath:info/info.properties</value>
</list>
</property>
</bean>

2、使用 #获取属性

@Value("#{user.name}")

private String userName;

<bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<array>
<value>classpath:configure.properties</value>
</array>
</property>
</bean>

spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别的更多相关文章

  1. Spring获取properties文件中的属性

    1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 applicatio ...

  2. Spring 获取propertise文件中的值

    Spring 获取propertise文件中的值 Spring 获取propertise的方式,除了之前的博文提到的使用@value的注解注入之外,还可以通过编码的方式获取,这里主要说的是要使用Emb ...

  3. 向properties文件中写入信息(针对获取properties文件失败的总结)

    前段时间项目需要将某个属性动态的写入项目发布路径下的properties文件中;但是实际发布时发现找不到maven项目resource路径下的project.properties文件,调试多次代码如下 ...

  4. SpringMVC+HibernateValidator,配置在properties文件中的错误信息回显前端页面出现中文乱码

    问题: 后台在springMVC中使用hibernate-validator做参数校验的时候(validator具体使用方法见GOOGLE),用properties文件配置了校验失败的错误信息.发现回 ...

  5. 封装的方法--读取任何路径下的properties文件中的值

    概述:我们在做项目时,经常需要从某个properties文件中读取properties文件中的值.现在我封装了一下方法,直接读取配置文件中的值. 代码如下所示: /** * Created by qi ...

  6. spring通过静态方法获得properties文件的值

    获得spring bean方法 @Component public class BeanUtils implements ApplicationContextAware { private stati ...

  7. C#可以获取Excel文件中Sheet的名字

    C#可以获取Excel文件中Sheet的名字吗 C#可以获取Excel文件中Sheet的名字吗 我试过WPS的表格可以 可以 要代码么 百度都有 [深圳]Milen(99696619)  14:13: ...

  8. Java 获取*.properties配置文件中的内容 ,常见的两种方法

    import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...

  9. Java Bean 获取properties文件的读取

    实际的开发过程中,将一些配置属性从java代码中提取到properties文件中是个很好的选择,降低了代码的耦合度.下面介绍两种通过spring读取properties文件的方法,以ip地址配置为例. ...

随机推荐

  1. 判断页面是app打开还是浏览器打开。cookie

    有个需求需要对页面判断不同的打开方式来最里面的链接进行不同调整, 这样就要分四种情况,app,浏览器X安卓系统,苹果系统,起初是对页面url地址带有的参数(安卓)跟用户代理(苹果)返回值判断navig ...

  2. POJ3045--Cow Acrobats(theory proving)

    Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the ...

  3. 【译】Surface中你也许不知道的五件事

    Bring up the Quick Link Menu - Select the Windows Key + X or right click the Start Button to bring u ...

  4. SpringMVC随笔之——@responsebody【引用snowolf博文】

    首先说RESTful 风格是什么 :(RESTful 风格:把请求参数变成请求路径的一种风格.) OK,一句话总结完毕 @responsebody表示该方法的返回结果直接写入HTTP response ...

  5. HDU2732一个让我debug了一晚上的题目

    思路都理解了,清晰了,就是代码不对,还是有些小地方自己注意不到,即使就在你的眼前也不易发现的那种 Description: 也是一个最大流的构图,没相出来,或者说想简单了也是标记点1 至 n * m是 ...

  6. js-随机图片

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  7. Palindrome II

    Problem Statement Given a string s, partition s such that every substring of the partition is a pali ...

  8. Loop List

    Loop List is very common in interview. This article we give a more strict short statement about its ...

  9. Ubuntu18.04 - 实现鼠标右键新建文件功能!

    Ubuntu18.04安装完毕后,你会发现,如果在桌面或其它地方,像在Windows下鼠标右键,新建一个文件,那么真的不行,没有那个选项!这个功能其实非常有用,怎么实现呢?新建一个你要右键新建类型文件 ...

  10. 文件上传下载、socketserver(并发)、解读socketserver源码

    1.文件上传/下载 学习了socket套接字,我们现在可以写一个文件上传/下载的程序,如下示例: 分析上边代码,我们发现,client发送上传文件相关信息的字典序列化之后,server又给client ...