@Value


Person.java

package com.dym.bean;import org.springframework.beans.factory.annotation.Value;public class Person {//使用@Value赋值;//1、基本数值//2、可以写SpEL; #{}//3、可以写${};取出配置文件【properties】中的值(在运行环境变量里面的值)@Value("张三")private String name;@Value("#{20-2}")private Integer age;@Value("${person.nickName}")private String nickName;public String getNickName() {return nickName;}public void setNickName(String nickName) {this.nickName = nickName;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Person(String name, Integer age) {super();this.name = name;this.age = age;}public Person() {super();}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + ", nickName=" + nickName + "]";}}

MainConfigOfPropertyValues.java

package com.dym.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;import com.dym.bean.Person;//使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中;加载完外部的配置文件以后使用${}取出配置文件的值
@PropertySource(value={"classpath:/person.properties"})
@Configuration
public class MainConfigOfPropertyValues {@Beanpublic Person person(){return new Person();}}

IOCTest_PropertyValue.java

package com.dym.test;import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;import com.dym.bean.Person;
import com.dym.config.MainConfigOfLifeCycle;
import com.dym.config.MainConfigOfPropertyValues;public class IOCTest_PropertyValue {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);@Testpublic void test01() {printBeans(applicationContext);System.out.println("=============");Person person = (Person) applicationContext.getBean("person");System.out.println(person);ConfigurableEnvironment environment = applicationContext.getEnvironment();String property = environment.getProperty("person.nickName");System.out.println(property);applicationContext.close();}private void printBeans(AnnotationConfigApplicationContext applicationContext) {String[] definitionNames = applicationContext.getBeanDefinitionNames();for (String name : definitionNames) {System.out.println(name);}}}

组件赋值——@Value  ---@PropertySource(value={“classpath:/person.properties“})相关推荐

  1. Spring的组件赋值以及环境属性@PropertySource

    @PropertySource 将指定类路径下的.properties一些配置加载到Spring当中, 有个跟这个差不多的注解@PropertySources @Target(ElementType. ...

  2. 通过@Value + @PropertySource来给组件赋值

    public class Person {//通过普通的方式@Value("司马")private String firstName;//spel方式来赋值@Value(" ...

  3. @value 静态变量_Spring注解驱动开发之八——@Value属性赋值、@PropertySource 加载外部配置文件...

    本文包含以下内容: 建立新的配置类 建立新的测试方法 通过@Value 进行赋值 通过@PropertySource  加载配置文件,并进行注入 拓展@Value  .@PropertySource ...

  4. layui select 赋值_layui给下拉框及日期组件赋值

    一.吐槽,layui感觉真的是比较不好用 二.为啥不好用呢,在现如今的页面动态渲染的情况的,layui这种基于原生js的模式让代码编写变得很臃肿, 可能一个功能的开发我用其vue框架很简便就实现了,但 ...

  5. vue子组件赋值props_vue 父子传值props赋值失效问题

    前天我的同事问了我一个问题,说她遇到一个父子组件之间的props赋值失效的问题,奇怪的是同一个页面另外一个地方也调用了同一个子组件并且props使用赋值正常,而且她和我说其他props能正常赋值,还就 ...

  6. html 日志记录组件,使用HTML自定义格式的Log4j.properties进行日志记录

    我需要帮助编辑Apache Log4j文件的输出. 我正在使用html布局来保存创建的日志.这里是我的log4j.properties代码:使用HTML自定义格式的Log4j.properties进行 ...

  7. vue父子组件赋值操作时报错Avoid mutating a prop directly since the value will be overwritten whenever

    前言: 我写了一个组件,然后子组件外层绑定了一个v-model='moreInquiriesCard', 这个值通过props取的父组件传过来的值,子组件是个modal弹框,对默认右上角有个" ...

  8. 处理vue异步请求数据动态从父组件向子组件赋值时,子组件无法获取到值问题

    1. 组件执行顺序导致:     父组件:created->子组件:created->子组件:mounted->父组件:mounted 2.问题子组件无法获取到值 //父组件 exp ...

  9. java的classpath配置文件_Java Classpath及.properties配置文件

    private static final String CONFIG_FILE = "logmonitor.properties"; ClassLoader cl =ClassLo ...

最新文章

  1. POJ 3347 Kadj Squares(复杂的线段相交问题)
  2. 【Spring框架家族】SpringBoot自动配置基本实现
  3. PX4 - position_estimator_inav
  4. 由歌词引发的模式思考之下篇(模拟Spring的BeanFactory)
  5. ios 内存管理的理解(三)ARC下 对象内存管理
  6. python怎么导出程序_[272]如何把Python脚本导出为exe程序
  7. python 获取excel文本框_简单使用python做excel多文件批量搜索(带图形界面)(已更新)...
  8. 问答| 为什么四轮驱动机器人(SSMR)的质心(COM)没有横向分速度vy呢?
  9. 《Java从入门到放弃》JavaSE入门篇:JDBC(入门版)
  10. RHEL6 下搭建 nginx + uwsgi + cgi 平台
  11. 计算机切换用户屏幕闪,小编教您Win10切换用户后闪屏的具体办法
  12. 阿里云国际版CDN 安全保护指南:管理篡改、攻击和内容
  13. 移动硬盘提示数据错误循环冗余检查要怎么办啊
  14. jnhs中国的省市县区邮编坐标mysql数据表
  15. pureftpd 配置 mysql_Pure-ftp配置文件详解
  16. 大数据实战之Spark-Flume-Kafka-idea-Mysql实时处理数据并存储
  17. 霞浦职业中专学校计算机专业,霞浦职业中专学校
  18. 《SQL必知必会》学习笔记——第十二课 连结表
  19. 在Ubuntu虚拟机使用ffmpeg采集摄像头的yuv视频数据
  20. 最大加权矩形 压缩+前缀和+dp

热门文章

  1. 在IIS上建立WAP网站的图文方法
  2. js获取元素提示信息
  3. [20180818]校内模拟赛
  4. c# WebApi之身份验证:Basic基础认证
  5. Core Java 第三章 Java基本的程序设计结构
  6. 牛X的web报表设计工具Grid++Report
  7. sqlplus登录时遇到的ORA-12560: TNS: 协议适配器错误解决办法
  8. android系统底层驱动多个物理按键上报同一个键值给app层,app层如何区分
  9. Pat乙级 1045 快速排序
  10. Linq to Sql : 动态构造Expression进行动态查询