1. 程式人生 > >Spring中利用配置檔案和@value注入屬性值

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輸入屬性值name,預設值xydefault
	@Value("${name:xydefault}")
	private String name;

	// @Value輸入屬性值num,預設值-1
	@Value("${num:-1}")
	private Integer num;

	// @Value輸入屬性值type,預設值-2
	@Value("${type:-2}")
	private Integer type;

	public void getInfo() {
		System.out.println("name:" + name + ",num:" + num + ",type:" + type);
	}
}
#src/main/resource新建檔案info.properties
name=xy1
num=101
type=1
<!-- applicationContext.xml檔案 -->
<!-- 掃描測試屬性包中的類,要注入屬性類需要被Spring管理 -->
<context:component-scan base-package="com.xy.test1" />

<!--方法1-->
<!-- <context:property-placeholder location="classpath*:info/info.properties" /> -->

<!--方法2-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="order" value="1" />
	<property name="locations">
		<list>
			<value>classpath:info/info.properties</value>
		</list>
	</property>
</bean>
2 利用util標籤注入複雜屬性值
package com.xy.test2;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 該類必須被Spring容器管理屬性才可以被注入。利用@Value註解,即使沒有該屬性或者屬性檔案也不會報錯
 */
@Service
public class PropertiesService2 {

	@Value("#{testPro}")
	private Properties pros;

	@Value("#{testList}")
	private List<String> myList;

	@Value("#{testMap}")
	private Map<Integer, String> myMap;


	public Properties getPros() {
		return pros;
	}

	public void setPros(Properties pros) {
		this.pros = pros;
	}

	public List<String> getMyList() {
		return myList;
	}

	public void setMyList(List<String> myList) {
		this.myList = myList;
	}

	public Map<Integer, String> getMyMap() {
		return myMap;
	}

	public void setMyMap(Map<Integer, String> myMap) {
		this.myMap = myMap;
	}
}
#src/main/resource新建檔案info2.properties
name=xy2
num=102
type=2
<!-- applicationContext.xml -->	
<!-- 掃描測試屬性包中的類,要注入屬性類需要被Spring管理 -->
<context:component-scan base-package="com.xy.test2" />

<!-- properties -->
<util:properties id="testPro" location="classpath:info/info2.properties" />

<!-- list -->
<util:list id="testList" list-class="java.util.ArrayList">
	<value>first</value>
	<value>second</value>
	<value>third</value>
</util:list>

<!-- map -->
<util:map id="testMap" map-class="java.util.HashMap">
	<entry key="1" value="first" />
	<entry key="2" value="second" />
	<entry key="3" value="third" />
</util:map>

相關推薦

Spring利用配置檔案@value注入屬性

1 簡單屬性值注入 package com.xy.test1; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service;

[java][spring]取得通過配置檔案註解注入的bean、取得當前資料庫連線、取得ApplicationContext的方法

[spring]取得通過配置檔案和註解注入的bean、取得當前資料庫連線、取得ApplicationContext的方法 1、思路: 應用程式啟動時使用ApplicationContextAware讓Spring容器傳遞自己生成的ApplicationContext給我們,

linuxmysql配置檔案jdk環境變數

mysql配置檔案 [client]default-character-set = utf8mb4 [mysql]socket = /var/lib/mysql/mysql.sockdefault-character-set = utf8mb4 [mysqld]skip-name-resolveport

【轉】spring對控制反轉依賴注入的理解

由於最近沒有太多時間,不能深入的瞭解控制反轉和依賴注入,只好把別人的理解轉載過來,非常痛恨市面上各種教程對所謂的術語張口就來,等自己有了時間一定要寫出新手都能看懂的教程。     首先想說說IoC(Inversion of Control,控制反轉)。這是spring的核心,貫穿始終。所謂IoC

Spring載入配置檔案的方式

3.1.1. 直接構造ApplicationContext 如果我們可以applicationContext.xml放到classpath下,我們可以使用ClasspathXmlApplicationContext。這裡傳入引數的路徑是相對於classpath的配置的,對於web專案就是WE

解決spring不同配置檔案存在name或者id相同的bean可能引起的問題

spring對同一配置檔案中相同id或者name的兩個或以上的bean時,做直接拋異常的處理,而對不同配置檔案中相同id或者名稱的bean,只會在列印日誌級別為info的資訊,資訊內容大概為"Overriding bean definition for bean xxx :

關於Spring的[控制反轉][依賴注入]的深入理解

第一段說明 對於一個Bean 來說,如果你依賴別的Bean , 只需要宣告即可, spring 容器負責把依賴的bean 給“注入進去“, 起初大家稱之為控制反轉(IoC) 後來 Ma

spring通過配置檔案方式實現定時任務

Spring3.0以後自帶有定時任務的實現功能: 一、修改spring配置檔案的內容:在檔案頭新增名稱空間和描述 <?xmlversion="1.0"encoding="UTF-8"? > <beansxmlns="http://www.springf

Spring核心配置檔案載入頭部資訊的兩種方式

1、沒有外掛的情況下:官網拷貝程式碼複製<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

Spring的依賴查詢依賴注入

作者:[Grey](https://www.cnblogs.com/greyzeng/) 原文地址: [語雀](https://www.yuque.com/greyzeng/mnc4mc/oshvxb) [部落格園](https://www.cnblogs.com/greyzeng/p/14459565

在ServletContextListener 的實現類(使用Spring @Value 註解的方式讀取配置檔案、或者注入Spring bean)

在ServletContextListener 的實現類中 使用Spring @Value 註解的方式讀取配置檔案 我想向ServletContextListener中通過Spring @value 的方法讀取 properties 配置檔案資訊,但是我開始的方法不行 public class MyLi

讓Eclipsespring的xml配置檔案出現屬性類提示

在spring配置檔案中可以讓配置bean的時候出現提示,這裡需要做一些設定。設定包括安裝springide外掛,spring-beans-version.xsd檔案引入,增加xml編輯提示的字元,預設只有=>:。最後是讓配置檔案可以通過Spring Config Editor的方式開

springbean配置bean注入

轉自 https://www.cnblogs.com/wuchanming/p/5426746.html bean與spring容器的關係 Bean配置資訊定義了Bean的實現及依賴關係,Spring容器根據各種形式的Bean配置資訊在容器內部建立Bean定義

Spring依賴注入(基於XML配置檔案Annotation的方式完成屬性裝配)

依賴注入的方式(手工裝配): 1.使用bean的構造器注入—基於XML方式 2.使用屬性setter方法注入—基於XML方式 3.使用field注入—基於Annotation方式 注入依賴物件可

spring提供讀取配置檔案屬性註解@Value

有時,當我們需要把專案中的properties檔案的屬性可以在專案中得到,spring就提供了一個註解@Value來讀取(前提是屬性檔案properties需要首先讓spring管理,即spring配置檔案中包含一下) @Value("${屬性名}")          /

springxml配置的初步理解,併成功注入spring jar包版本號一定要同一)

//.java package com.learning.ioc.interfaces; public interface OneInterface { public void say(String arg); } package com.learning.ioc.int

Spring Boot配置檔案常見操作

Spring Boot配置檔案和常見操作 Spring Boot專案釋出成jar包 Spring Boot配置方式 命令列方式 應用程式屬性檔案: application.properties 參考文獻 Sp

java讀取配置檔案的一些方法 getResourceAsStream 直接 FileInputStream 以及 配置System.getProperty("user.dir")所得的工作目錄

配置檔案位於 /src/ 下的情況已經由上述博主列出,需要的可以移步檢視,即以下幾個情況 1.路徑:src/aa.xml 2.位於src下同一個包下 3.位於src下不同包 不過本博主的專案是web專案,而配置檔案放在src檔案下容易因為快取導致更新不及時,

Spring的ioc介紹,配置檔案基本使用

1. 什麼是spring,它能夠做什麼?    Spring是一個開源框架,它由Rod Johnson建立。它是為了解決企業應用開發的複雜性而建立的。    Spring使用基本的JavaBean來完成以前只可能由EJB完成的事情。   &nbs

3springboot:springboot配置檔案配置檔案、YAML、屬性檔案注入<@Value、@ConfigurationProperties、@PropertySource,@ImportResource、@Bean>)

1.配置檔案: springboot預設使用一個全域性配置檔案 配置檔名是固定的    配置檔案有兩種(開頭均是application,主要是檔案的字尾): ->application. properties ->application