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注解注入属性文件中的值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java连接Mysql数据库的工具类

    java连接Mysql数据库的工具类

    这篇文章主要介绍了java连接Mysql数据库的工具类,非常的实用,推荐给大家,需要的朋友可以参考下
    2015-03-03
  • 用JAVA 设计生成二维码详细教程

    用JAVA 设计生成二维码详细教程

    本文主要介绍用JAVA 设计生成二维码,这里一步一步详细介绍用 java 如何设计二维码,并附有代码示例以便参考,有需要的小伙伴可以参考下
    2016-08-08
  • postman 如何实现传递 ArrayList 给后台

    postman 如何实现传递 ArrayList 给后台

    这篇文章主要介绍了postman 如何实现传递 ArrayList给后台,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 基于Redisson实现注解式分布式锁的示例代码

    基于Redisson实现注解式分布式锁的示例代码

    这篇文章主要为大家详细介绍了如何基于Redisson实现注解式分布式锁,文中的示例代码讲解详细,具有一定的参考价值,需要的可以了解一下
    2023-07-07
  • Spring Boot 配置和使用多线程池的实现

    Spring Boot 配置和使用多线程池的实现

    这篇文章主要介绍了Spring Boot 配置和使用多线程池的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • SpringBoot后端上传文件类型检测方式

    SpringBoot后端上传文件类型检测方式

    这篇文章主要介绍了SpringBoot后端上传文件类型检测方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • springMvc全局异常的实现

    springMvc全局异常的实现

    大家好,本篇文章主要讲的是springMvc全局异常的实现,感兴趣的同学赶紧来看一看吧,对你有帮助的话记得收藏一下
    2022-02-02
  • Springboot中useGeneratedKeys用法小结

    Springboot中useGeneratedKeys用法小结

    本文主要介绍了Springboot中useGeneratedKeys用法小结,useGeneratedKeys 是 MyBatis 框架中的一个参数,用于指定是否允许 JDBC 支持自动生成主键,感兴趣的可以了解一下
    2024-09-09
  • 关于Mybatis与JPA的优缺点说明

    关于Mybatis与JPA的优缺点说明

    这篇文章主要介绍了关于Mybatis与JPA的优缺点说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • Hibernate的Annotation版Hello world实例

    Hibernate的Annotation版Hello world实例

    这篇文章主要介绍了Hibernate的Annotation版Hello world实现方法,详细分析了Annotation的具体使用步骤与Hello world实现方法,需要的朋友可以参考下
    2016-03-03

最新评论