欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中使用@Value注解注入屬性文件中的值詳解

 更新時間:2023年12月14日 09:20:56   作者:Terisadeng  
這篇文章主要介紹了Spring中使用@Value注解注入屬性文件中的值詳解,通過Spring的@Value注解可以將xml中關聯的屬性文件中的值注入變量中,這樣就不需要通過創(chuàng)建Properties然后根據屬性文件讀取屬性值了,需要的朋友可以參考下

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

通過Spring的@Value注解可以將xml中關聯的屬性文件中的值注入變量中,這樣就不需要通過創(chuàng)建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注解注入屬性文件中的值內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 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的優(yōu)缺點說明

    關于Mybatis與JPA的優(yōu)缺點說明

    這篇文章主要介紹了關于Mybatis與JPA的優(yōu)缺點說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Hibernate的Annotation版Hello world實例

    Hibernate的Annotation版Hello world實例

    這篇文章主要介紹了Hibernate的Annotation版Hello world實現方法,詳細分析了Annotation的具體使用步驟與Hello world實現方法,需要的朋友可以參考下
    2016-03-03

最新評論