Spring中使用@Value注解注入属性文件中的值详解

 更新时间:2023年12月14日 09:20:56   作者:Terisadeng  
这篇文章主要介绍了Spring中使用@Value注解注入属性文件中的值详解,通过Spring的@Value注解可以将xml中关联的属性文件中的值注入变量中,这样就不需要通过创建Properties然后根据属性文件读取属性值了,需要的朋友可以参考下

@Value注解注入属性文件中的值

通过Spring的@Value注解可以将xml中关联的属性文件中的值注入变量中,这样就不需要通过创建Properties然后根据属性文件读取属性值了。

1、定义一个属性文件

首先定义一个属性文件,其中存储代码中需要引入的属性值

2、添加一个配置文件

spring-config-properties.xml,通过spring容器读取,内容如下:

<?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:jaxws="http://cxf.apache.org/jaxws" xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
       http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
       http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
   <util:properties id="configProperties" location="classpath:conf/setting.properties" />
</beans>

3、测试类

编写测试类测试注入的效果:

package com.teriste.other;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:conf/spring/spring-config-properties.xml"})
public class PropertiesTest {
    @Value("#{configProperties['secretAccessKey']}")
    private String secretAccessKey;
    public String getSecretAccessKey() {
        return secretAccessKey;
    }
    public void setSecretAccessKey(String secretAccessKey) {
        this.secretAccessKey = secretAccessKey;
    }
    @Test
    public void testGetProperties(){
        System.out.println("通过@Value注解注入属性文件中的值:"+secretAccessKey);
    }
}

4、测试结果

到此这篇关于Spring中使用@Value注解注入属性文件中的值详解的文章就介绍到这了,更多相关@Value注解注入属性文件中的值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring中的使用@Async异步调用方法

    Spring中的使用@Async异步调用方法

    这篇文章主要介绍了Spring中的使用@Async异步调用方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Mybatis useGeneratedKeys参数用法及问题小结

    Mybatis useGeneratedKeys参数用法及问题小结

    这篇文章主要介绍了Mybatis useGeneratedKeys参数用法及遇到的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • J2EE项目代码编写规范分享

    J2EE项目代码编写规范分享

    这篇文章主要介绍了J2EE项目代码编写规范分享,需要的朋友可以参考下
    2014-10-10
  • java实现字符串反转

    java实现字符串反转

    这篇文章主要为大家详细介绍了java实现字符串反转,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • jdk自带线程池实例详解

    jdk自带线程池实例详解

    在最近做的一个项目中,需要大量的使用到多线程和线程池,下面就java自带的线程池和大家一起分享
    2018-02-02
  • springboot+nginx+https+linux实现负载均衡加域名访问简单测试

    springboot+nginx+https+linux实现负载均衡加域名访问简单测试

    这篇文章主要介绍了springboot+nginx+https+linux实现负载均衡加域名访问简单测试,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-05-05
  • Springboot2以代码的方式统一配置Jackson教程

    Springboot2以代码的方式统一配置Jackson教程

    这篇文章主要介绍了Springboot2以代码的方式统一配置Jackson教程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 简析Java中的util.concurrent.Future接口

    简析Java中的util.concurrent.Future接口

    这篇文章主要介绍了简析Java中的util.concurrent.Future接口,作者把future归结为在未来得到目标对象的占位符,需要的朋友可以参考下
    2015-07-07
  • Json 自定义使用函数的简单实例

    Json 自定义使用函数的简单实例

    下面小编就为大家带来一篇Json 自定义使用函数的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-10-10
  • 使用jsoup解析html的table中的文本信息实例

    使用jsoup解析html的table中的文本信息实例

    今天小编就为大家分享一篇使用jsoup解析html的table中的文本信息实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05

最新评论