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

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>

原文地址:https://www.cnblogs.com/lemon-flm/p/8391239.html

时间: 2024-08-29 07:44:38

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

Spring获取properties文件中的属性

1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 application.properties socket.time.out=1000 3.使用spring代码直接载入配置文件,获取属性信息 代码如下: Resource resource = new ClassPathResource("/application.properties"); Propert

Spring 获取propertise文件中的值

Spring 获取propertise文件中的值 Spring 获取propertise的方式,除了之前的博文提到的使用@value的注解注入之外,还可以通过编码的方式获取,这里主要说的是要使用EmbeddedValueResolverAware接口的使用. 一.准备propertise文件 在资源文件夹下面建立好要测试需要的app.propertise文件,里面写几条测试数据,本文主要如图数据. 二.准备配置文件 <?xml version="1.0" encoding=&qu

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

前段时间项目需要将某个属性动态的写入项目发布路径下的properties文件中;但是实际发布时发现找不到maven项目resource路径下的project.properties文件,调试多次代码如下: /** * 写入properties信息 * * @param key 名称 * @param value 值 */ public static void modifyConfig(String key, String value) { try { // 从输入流中读取属性列表(键和元素对) P

使用Spring注解方式注入properties文件内容,并配合Junit4+Spring做单元测试

先看看工作目录,然后再来讲解 1.建立config.properties,我的config.properties内容如下: author_name=luolin project_info=该项目主要是用于写一些demo 2.配置Spring配置文件,读取properties文件,并设置编码格式.大家从我的项目结构图中可以看到我用了两个Spring的配置文件,其实在spring-context.xml中没有配置其他内容,只是配置扫描com.eya.property这个包,大家可能会有疑问为何包的扫

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

问题: 后台在springMVC中使用hibernate-validator做参数校验的时候(validator具体使用方法见GOOGLE),用properties文件配置了校验失败的错误信息.发现回显给前端页面的时候中文错误信息显示乱码. 封装参数的POJO类 public class UserReqBean { @NotNull(message="{user.name.notnull}") private String userName; } ValErrMsg.propertie

QT模态对话框用法(在UI文件中设置Widget背景图,这个图是一个带阴影边框的图片——酷)

QT弹出模态对话框做法: 1.新建UI文件时,一定要选择基类是QDialog的,我的选择是:Dialog without Buttons(),如下图: 2.然后在使用的时候: MyDialog dlg(this); dlg.exec(); 如果不加this,则会在任务管理器里面产生一个新的EXE. 3.如果对话框的标题是自定义,不想使用系统的标题,这时候需要在代码中加入: setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint ); setAttr

Java获取.properties文件

@SuppressWarnings("rawtypes") public static void getProperties() { Properties properties = null; InputStream in = null; try { properties = new Properties(); //获取文件 in = Object.class.getResourceAsStream("/config.properties"); properties

通过反射获取class文件中的构造方法,运行构造方法

/* * 通过反射获取class文件中的构造方法,运行构造方法 * 运行构造方法,创建对象 * 1.获取class文件对象 * 2.从class文件对象中,获取需要的成员 * * Constructor 描述构造方法对象类 */ 1.person类,用于测试获取无参的构造方法 package cn.itcast.demo1; public class Person { public String name; private int age; /*static{ System.out.printl

读取properties文件中的内容

看情况有时候某些属性值在很多地方要用,而且可能会有改动,就可以把它们存在properties文件中. 当然也可以准备一个静态类来放. //调用方法的静态代码块 private static String getStr = ""; static { try { Properties properties = PropertiesUtil.readProperties("sys.properties"); //Str是文件中配置的名字部分 //例:文件中存了几个属性,其