Spring @value和@PropertySource注解使用方法解析

这篇文章主要介绍了Spring @value和@PropertySource注解使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

  • @Value注解:可以使用注入基本字符串 EL表达式,从配置文件读取数据
  • @PropertySource用于引入单个配置文件
  • @PropertySources用于引入多个配置文件
  • @PropertySource或者@PropertySources引入的数据都是存在环境变量ConfigurableEnvironment中的
  • 在resouces文件夹下新建一个cat.properties文件,写入内容parent=tiger
public class Cat {
  @Value("猫") //直接注入字符串
  private String name;
  @Value("#{12+2}") //支持EL表达式
  private int age;
  @Value("${parent}") //支持配置文件读取
  private String parent;
  public Cat() {
    System.out.println("猫被初始化");
  }

  @Override
  public String toString() {
    return "Cat{" +
        "name='" + name + '\'' +
        ", age=" + age +
        ", parent='" + parent + '\'' +
        '}';
  }
}
@Import({Cat.class})
@PropertySources({@PropertySource(value ="cat.properties")})
public class Appconfig {
}
public class Demo {
  public static void main(String[] args) {

    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Appconfig.class);
    Cat bean = context.getBean(Cat.class);
    System.out.println(bean); //Cat{name='猫', age=14, parent='tiger'}
    ConfigurableEnvironment environment = context.getEnvironment();
    System.out.println(environment.getProperty("parent")); //tiger

  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 详解Spring通过@Value注解注入属性的几种方式

    场景 假如有以下属性文件dev.properties, 需要注入下面的tag tag=123 通过PropertyPlaceholderConfigurer <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="dev.properties" /&

  • spring中@value注解需要注意的问题

    首先,@value需要参数,这里参数可以是两种形式:@Value("#{configProperties['t1.msgname']}")或者@Value("${t1.msgname}"): 其次,下面我们来看看如何使用这两形式,在配置上有什么区别: 1. @Value("#{configProperties['t1.msgname']}")这种形式的配置中有"configProperties",其实它指定的是配置文件的加载对

  • Spring4如何自定义@Value功能详解

    前言 本文主要给大家介绍了关于Spring4自定义@Value功能的相关内容,使用的Spring版本4.3.10.RELEASE,下面话不多说了,来一起看看详细的介绍吧. @Value在Spring中,功能非常强大,可以注入一个配置项,可以引用容器中的Bean(调用其方法),也可以做一些简单的运算 如下的一个简单demo,演示@Value的用法 import org.springframework.stereotype.Service; /** * 测试Bean */ @Service("use

  • 详解Spring Boot 自定义PropertySourceLoader

    SpringBoot 的配置文件内置支持 properties.xml.yml.yaml 几种格式,其中 properties和xml 对应的Loader类为 PropertiesPropertySourceLoader ,yml和yaml 对应的Loader类为 YamlPropertySourceLoader. 观察这2个类可以发现,都实现自接口 PropertySourceLoader .所以我们要新增支持别的格式的配置文件,就可以通过实现接口 PropertySourceLoader 来

  • Spring boot中PropertySource注解的使用方法详解

    前言 本文将重点讲解一下Spring中@PropertySource注解的使用,如何通过PropertySource注解加载指定的配置文件.以及PropertySource注解与@ConfigurationProperties两个注解的配合使用.下面话不多说了,来随着小编来一起学习学习吧. 1.1. PropertySource注解加载指定的属性文件 Spring框架提供了PropertySource注解,目的是加载指定的属性文件,接下来我们看一下如何使用该注解.首先我们定义一个配置类,并在类中

  • Spring Boot自定义配置属性源(PropertySource)

    配置覆盖优于profile 在生产实践中,配置覆盖是解决不同环境不同配置的常用方法.比如用生产服务器上的配置文件覆盖包内的文件,或者使用中心化的配置服务来覆盖默认的业务配置. 相比于profile机制(比如maven的profile.spring boot的profile-specific properties),即不同环境使用不同的配置文件,覆盖的方式更有优势.程序员在开发时不需要关心生产环境数据库的地址.账号等信息,一次构建即可在不同环境中运行,而profile机制需要将生产环境的配置写到项

  • Spring中利用配置文件和@value注入属性值代码详解

    1 简单属性值注入 package com.xy.test1; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service // 需要被注入属性值的类需要被Spring管理 public class PropertiesService1 { // 利用@Value注解,即使没有该属性或者属性文件也不会报错 // @Value输入

  • Spring @value和@PropertySource注解使用方法解析

    这篇文章主要介绍了Spring @value和@PropertySource注解使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 @Value注解:可以使用注入基本字符串 EL表达式,从配置文件读取数据 @PropertySource用于引入单个配置文件 @PropertySources用于引入多个配置文件 @PropertySource或者@PropertySources引入的数据都是存在环境变量ConfigurableEnviro

  • 详解Spring中的@PropertySource注解使用

    @PropertySource注解是Spring用于加载配置文件,默认支持.properties与.xml两种配置文件.@PropertySource属性如下: name:默认为空,不指定Spring自动生成 value:配置文件 ignoreResourceNotFound:没有找到配置文件是否忽略,默认false,4.0版本加入 encoding:配置文件编码格式,默认UTF-8 4.3版本才加入 factory:配置文件解析工厂,默认:PropertySourceFactory.class

  • Spring声明式事务注解之@EnableTransactionManagement解析

    Spring声明式事务注解之@EnableTransactionManagement 1. 说明 @EnableTransactionManagement声明在主配置类上,表示开启声明式事务,其原理是通过@Import导入TransactionManagementConfigurationSelector组件,然后又通过TransactionManagementConfigurationSelector导入组件AutoProxyRegistrar和ProxyTransactionManageme

  • 关于spring中aop的注解实现方法实例详解

    前言 在之前的一篇文章中我们讲到spring的xml实现,这里我们讲讲使用注解如何实现aop呢.前面已经讲过aop的简单理解了,这里就不在赘述了.话不多说,来一起看看详细的介绍: 注解方式实现aop我们主要分为如下几个步骤: 1.在切面类(为切点服务的类)前用@Aspect注释修饰,声明为一个切面类. 2.用@Pointcut注释声明一个切点,目的是为了告诉切面,谁是它的服务对象.(此注释修饰的方法的方法体为空,不需要写功能比如 public void say(){};就可以了,方法名可以被候命

  • 快速理解spring中的各种注解

    Spring中的注解大概可以分为两大类: 1)spring的bean容器相关的注解,或者说bean工厂相关的注解: 2)springmvc相关的注解. spring的bean容器相关的注解,先后有:@Required, @Autowired, @PostConstruct, @PreDestory,还有Spring3.0开始支持的JSR-330标准javax.inject.*中的注解(@Inject, @Named, @Qualifier, @Provider, @Scope, @Singlet

  • Spring @RestController注解组合实现方法解析

    Spring中存在很多注解组合的情况,例如@RestController @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Controller @ResponseBody public @interface RestController { /** * The value may indicate a suggestion for a logical component name, * to b

  • Spring@Value属性注入使用方法解析

    这篇文章主要介绍了Spring@Value属性注入使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在使用Spring框架的项目中,@Value是使用比较频繁的注解之一,它的作用是将配置文件中key对应的值赋值给它标注的属性.在日常使用中我们常用的功能都比较简单,本篇文章系统的带大家来了解一下@Value的使用方法. @Value注入支持形式 @Value属性注入功能根据注入的内容来源可分为两类:通过配置文件的属性注入和通过非配置文件

  • Spring与Mybatis基于注解整合Redis的方法

    基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载后直接启动redis-server就行了,见下图: 一,先上jar包 二,创建实体类 package com.sl.user.vo; import java.io.Serializable; import com.fasterxml.jackson.databind.PropertyNamingSt

  • 在spring中使用自定义注解注册监听器的方法

    接口回调 监听器本质上就是利用回调机制,在某个动作发生前或后,执行我们自己的一些代码.在Java语言中,可以使用接口来实现. 实现一个监听器案例 为了方便,直接在spring环境中定义:以工作(work)为例,定义工作开始时(或结束时)的监听器. 1. 定义回调的接口 package com.yawn.demo.listener; /** * @author Created by yawn on 2018-01-21 13:53 */ public interface WorkListener

随机推荐