溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring中@Value注入復(fù)雜類型怎么用

發(fā)布時(shí)間:2021-12-16 11:13:55 來源:億速云 閱讀:214 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“Spring中@Value注入復(fù)雜類型怎么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Spring中@Value注入復(fù)雜類型怎么用”這篇文章吧。

為什么用,分割的字符串可以注入數(shù)組?于是我就去一步一步的斷點(diǎn)去走了一遍@value注入屬性的過程,才發(fā)現(xiàn)了根本原因。

@Value不支持復(fù)雜類型封裝(數(shù)組、Map、對(duì)象等)這個(gè)說法確實(shí)是有問題的,不夠嚴(yán)謹(jǐn),因?yàn)樵谔厥馇闆r下,是可以注入復(fù)雜類型的。

先來梳理一下@Value對(duì)屬性的注入流程

先交代一下我們的代碼:

一個(gè)yml文件a.yml

test: a,b,c,d

一個(gè)Bean A.java

@Component
@PropertySource(value = {"classpath:a.yml"},ignoreResourceNotFound = true, encoding = "utf-8")
public class A {
	@Value("${test}")
	private String[] test;
	public void test(){
		System.out.println("test:"+Arrays.toString(test));
		System.out.println("長度:"+test.length);
	}
}

main方法:

@Configuration
@ComponentScan("com.kinyang")
public class HelloApp {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(HelloApp.class);
		A bean = ac.getBean(A.class);
		bean.test();
	}
}

ok!下面開始分析

1、從AutowiredAnnotationBeanPostProcessor后置處理說起

過多的Spring初始化Bean的流程就不說了,我們直接定位到Bean的屬性注入的后置處理器AutowiredAnnotationBeanPostProcessor。

此類中的processInjection()方法中完成了Bean 中@Autowired、@Inject、 @Value 注解的解析并注入的功能。

	 此方法中完成了Bean 中@Autowired、@Inject、 @Value 注解的解析并注入的功能
	public void processInjection(Object bean) throws BeanCreationException {
		Class<?> clazz = bean.getClass();
		///  找到 類上所有的需要自動(dòng)注入的元素
		// (把@Autowired、@Inject、 @Value注解的字段和方法包裝成InjectionMetadata類的對(duì)象返回)
		InjectionMetadata metadata = findAutowiringMetadata(clazz.getName(), clazz, null);
		try {
			metadata.inject(bean, null, null);
		}
		catch (BeanCreationException ex) {
			throw ex;
		}
		catch (Throwable ex) {
			throw new BeanCreationException(
					"Injection of autowired dependencies failed for class [" + clazz + "]", ex);
		}
	}

2、接著進(jìn)入InjectionMetadata的inject()方法

inject()方法就是一個(gè)循環(huán)上面一步解析出來的注解信息,注解的方法或者字段包裝后的對(duì)象是InjectedElement類型的類,InjectedElement是一個(gè)抽象類,他的實(shí)現(xiàn)主要有兩個(gè):對(duì)注解字段生成的是AutowiredFieldElement類,對(duì)注解方法生成的是AutowiredMethodElement類。

我們這里只分析@Value注解字段的注入流程,所以下一步會(huì)進(jìn)到AutowiredFieldElement類的inject()方法.

此方法就兩大步驟:

  • 獲取要注入的value

  • 通過反射,把值去set字段上

其中獲取要注入的value過程比較復(fù)雜,第二步set值就兩行代碼搞定

具體邏輯看下面代碼上我寫的注釋

		protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
			Field field = (Field) this.member;
			Object value;
			if (this.cached) {
			/// 優(yōu)先從緩存中獲取
				value = resolvedCachedArgument(beanName, this.cachedFieldValue);
			}
			else {
			///緩存中沒有的話,走下面的邏輯處理
				DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
				desc.setContainingClass(bean.getClass());
				Set<String> autowiredBeanNames = new LinkedHashSet<>(1);
				Assert.state(beanFactory != null, "No BeanFactory available");
				  這個(gè)對(duì)我們今天討論的問題很關(guān)鍵
				  獲取一個(gè) 類型轉(zhuǎn)換器
				TypeConverter typeConverter = beanFactory.getTypeConverter();
				try {
					///   獲取值(重點(diǎn),這里把一個(gè)TypeConverter傳進(jìn)去了)
					value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
					///  經(jīng)過上面的方法返回來的 value 就是要注入的值了
					///  通過斷點(diǎn)調(diào)試,我們可以發(fā)現(xiàn)我們在配置文件yml中配置的 “a,b,c,d”字符串已經(jīng)變成了一個(gè)String[]數(shù)組
				}
				catch (BeansException ex) {
					throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
				}
				synchronized (this) {
					..... 
					這里不是我們本次討論的重點(diǎn)所以就去掉了
				}
			}
			if (value != null) {
			 這里就是第二步,賦值
				ReflectionUtils.makeAccessible(field);
				field.set(bean, value);
			}
		}
	}

從上面代碼來看,所有重點(diǎn)就都落到了這行代碼

value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);

推斷下來resolveDependency方法里應(yīng)該是讀取配置文件字符串,然后將字符串用,分割轉(zhuǎn)換了數(shù)組。

那么具體怎么轉(zhuǎn)換的呢?我們繼續(xù)跟進(jìn)!

進(jìn)入resolveDependency()方法,里面邏輯很簡單做了一些判斷,真正實(shí)現(xiàn)其實(shí)是doResolveDependency()方法,進(jìn)行跟進(jìn)。

根據(jù)@Value注解,從配置文件a.yml中解析出配置的內(nèi)容:“a,b,c,d”

Spring中@Value注入復(fù)雜類型怎么用

Spring中@Value注入復(fù)雜類型怎么用

到這里我們得到值還是配置文件配置的字符串,并沒有變成我們想要的String[]字符串?dāng)?shù)組類型。

我們繼續(xù)往下走,下面是獲取一個(gè)TypeConverter類型轉(zhuǎn)換器,這里的類型轉(zhuǎn)換器是上面?zhèn)鬟M(jìn)來的,具體類型SimpleTypeConverter類。

然后通過這個(gè)類型轉(zhuǎn)換器的convertIfNecessary方法把,我們的字符串"a,b,c,d"轉(zhuǎn)換成了String[]數(shù)組。

Spring中@Value注入復(fù)雜類型怎么用

所以我們現(xiàn)在知道了,我們從配置文件獲取到的值,通過了Spring轉(zhuǎn)換器,調(diào)用了convertIfNecessary方法后,進(jìn)行了類型自動(dòng)轉(zhuǎn)換。那么這轉(zhuǎn)換器到底是怎么進(jìn)行工作的呢?

繼續(xù)研究~~

那接下來要研究的就是Spring的TypeConverter的工作原理問題了

首先我們這里知道了外面?zhèn)鬟M(jìn)來的那個(gè)轉(zhuǎn)換器是一個(gè)叫SimpleTypeConverter 的轉(zhuǎn)換器。

這轉(zhuǎn)換器是org.springframework.beans.factory.support.AbstractBeanFactory#getTypeConverter方法得到的

	@Override
	public TypeConverter getTypeConverter() {
		TypeConverter customConverter = getCustomTypeConverter();
		if (customConverter != null) {
			return customConverter;
		}
		else {
		/// 如果沒有 用戶自定的TypeConverter 那就用 默認(rèn)的SimpleTypeConverter吧
			// Build default TypeConverter, registering custom editors.
			SimpleTypeConverter typeConverter = new SimpleTypeConverter();
			 注冊一些默認(rèn)的ConversionService
			typeConverter.setConversionService(getConversionService());
			  再注冊一些默認(rèn)的CustomEditors
			registerCustomEditors(typeConverter);
			return typeConverter;
		}
	}

默認(rèn)的SimpleTypeConverter里面注冊了一些轉(zhuǎn)換器,從debug過程我們可以看到默認(rèn)是注入了12個(gè)PropertyEditor

Spring中@Value注入復(fù)雜類型怎么用

這12個(gè)PropertyEditor是在哪注入的呢?大家可以看registerCustomEditors(typeConverter)方法,這里就不展開了,我直接說了,是通過ResourceEditorRegistrar類注入進(jìn)去的。

@Override
	public void registerCustomEditors(PropertyEditorRegistry registry) {
		ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
		doRegisterEditor(registry, Resource.class, baseEditor);
		doRegisterEditor(registry, ContextResource.class, baseEditor);
		doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
		doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
		doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
		doRegisterEditor(registry, Path.class, new PathEditor(baseEditor));
		doRegisterEditor(registry, Reader.class, new ReaderEditor(baseEditor));
		doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));

		ClassLoader classLoader = this.resourceLoader.getClassLoader();
		doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
		doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
		doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));

		if (this.resourceLoader instanceof ResourcePatternResolver) {
			doRegisterEditor(registry, Resource[].class,
					new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
		}
	}

現(xiàn)在我們回到 SimpleTypeConverter 的convertIfNecessary方法里去,這個(gè)方法其實(shí)是SimpleTypeConverter的父類TypeConverterSupport的方法,而這個(gè)父類方法里調(diào)用的又是TypeConverterDelegate類的convertIfNecessary方法(一個(gè)比一個(gè)懶,哈哈哈就是自己不干活)

最后我們重點(diǎn)來分析TypeConverterDelegate的convertIfNecessary方法。

這個(gè)方法內(nèi)容比較多,但是整體思路就是 根據(jù)最后想轉(zhuǎn)換的類型,選擇出對(duì)應(yīng)的PropertyEditor或者ConversionService,然后進(jìn)行類型轉(zhuǎn)換。

從上面的看的注入的12個(gè)PropertyEditor中,我們就可以看出來了,我們匹配到的是

這行代碼doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));注入的ClassArrayEditor。

所以我ClassArrayEditor這個(gè)類就可以了,這個(gè)類就很簡單了,主要看setAsText方法

public void setAsText(String text) throws IllegalArgumentException {
		if (StringUtils.hasText(text)) {
			///  這里通過StringUtils 把字符串,轉(zhuǎn)換成 String數(shù)組
			String[] classNames = StringUtils.commaDelimitedListToStringArray(text);
			Class<?>[] classes = new Class<?>[classNames.length];
			for (int i = 0; i < classNames.length; i++) {
				String className = classNames[i].trim();
				classes[i] = ClassUtils.resolveClassName(className, this.classLoader);
			}
			setValue(classes);
		}
		else {
			setValue(null);
		}
	}

這個(gè)方法里通過

Spring的字符串工具類StringUtils的commaDelimitedListToStringArray(text)方法把字符串轉(zhuǎn)換成了數(shù)組,方法里就是通過 “,” 進(jìn)行分割的。

到此為止,我們知道了@Value為什么可以把“,”分割的字符串注冊到數(shù)組中了吧。

其實(shí)@Value可以注入U(xiǎn)RI、Class、File、Resource等等類型,@Value可以注入什么類型完全取決于能不能找到處理 String 到 注入類型的轉(zhuǎn)換器。

上面列出來的12個(gè)其實(shí)不是全部默認(rèn)的,系統(tǒng)還有47個(gè)其他的轉(zhuǎn)換器,只不過是上面的12個(gè)優(yōu)先級(jí)比較高而已,其實(shí)還有下面的40多個(gè)轉(zhuǎn)換器,所以你看@Value可以注入的類型還會(huì)很多的。

private void createDefaultEditors() {
		this.defaultEditors = new HashMap<>(64);

		// Simple editors, without parameterization capabilities.
		// The JDK does not contain a default editor for any of these target types.
		this.defaultEditors.put(Charset.class, new CharsetEditor());
		this.defaultEditors.put(Class.class, new ClassEditor());
		this.defaultEditors.put(Class[].class, new ClassArrayEditor());
		this.defaultEditors.put(Currency.class, new CurrencyEditor());
		this.defaultEditors.put(File.class, new FileEditor());
		this.defaultEditors.put(InputStream.class, new InputStreamEditor());
		this.defaultEditors.put(InputSource.class, new InputSourceEditor());
		this.defaultEditors.put(Locale.class, new LocaleEditor());
		this.defaultEditors.put(Path.class, new PathEditor());
		this.defaultEditors.put(Pattern.class, new PatternEditor());
		this.defaultEditors.put(Properties.class, new PropertiesEditor());
		this.defaultEditors.put(Reader.class, new ReaderEditor());
		this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());
		this.defaultEditors.put(TimeZone.class, new TimeZoneEditor());
		this.defaultEditors.put(URI.class, new URIEditor());
		this.defaultEditors.put(URL.class, new URLEditor());
		this.defaultEditors.put(UUID.class, new UUIDEditor());
		this.defaultEditors.put(ZoneId.class, new ZoneIdEditor());

		// Default instances of collection editors.
		// Can be overridden by registering custom instances of those as custom editors.
		this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));
		this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));
		this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));
		this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));
		this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));

		// Default editors for primitive arrays.
		this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());
		this.defaultEditors.put(char[].class, new CharArrayPropertyEditor());

		// The JDK does not contain a default editor for char!
		this.defaultEditors.put(char.class, new CharacterEditor(false));
		this.defaultEditors.put(Character.class, new CharacterEditor(true));

		// Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.
		this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false));
		this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));

		// The JDK does not contain default editors for number wrapper types!
		// Override JDK primitive number editors with our own CustomNumberEditor.
		this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false));
		this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true));
		this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false));
		this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true));
		this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false));
		this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true));
		this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false));
		this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true));
		this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false));
		this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true));
		this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false));
		this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true));
		this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));
		this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));

		// Only register config value editors if explicitly requested.
		if (this.configValueEditorsActive) {
			StringArrayPropertyEditor sae = new StringArrayPropertyEditor();
			this.defaultEditors.put(String[].class, sae);
			this.defaultEditors.put(short[].class, sae);
			this.defaultEditors.put(int[].class, sae);
			this.defaultEditors.put(long[].class, sae);
		}
	}

重點(diǎn)來了,分析了這么久了,那么,如果我們想注冊一個(gè)我們自定義的類該如何操作呢???

好了,既然知道了@Value的注入的原理和中間類型轉(zhuǎn)換的過程,那我們就知道該從哪里下手了,那就是寫一個(gè)我們自己的PropertyEditor,然后注冊到Spring的類型轉(zhuǎn)換器中。

先明確一下我們的需求,就是在yml配置文件中,配置字符串,然后通過@Value注入為一個(gè)自定義的對(duì)象。

我們的自定義對(duì)象 Car.java

public class Car {
	private String color;
	private String name;
	// 省略 get set方法
}

yml配置文件,配置car: 紅色|法拉利,我們這里用|分割

test: a,b,c,d
car: 紅色|法拉利

用于測試的Bean A.java

@Component
@PropertySource(value = {"classpath:a.yml"},ignoreResourceNotFound = true, encoding = "utf-8")
public class A {
	@Value("${test}")
	private String[] test;
	@Value("${car}")
	private Car car;
	public void test(){
		System.out.println("test:"+Arrays.toString(test));
		System.out.println("長度:"+test.length);

		System.out.println("自定的Car 居然通過@Value注冊成功了");
		System.out.println(car.toString());
	}
}

下面就是寫我們的PropertyEditor然后注冊到Spring的Spring的類型轉(zhuǎn)換器中。

  • 自定義 一個(gè) propertyEditor類:CarPropertyEditor,

  • 這里不要直接去實(shí)現(xiàn)PropertyEditor接口,那樣太麻煩了,因?yàn)橛泻芏嘟涌谝獙?shí)現(xiàn)

  • 我們這里通過繼承PropertyEditorSupport類,通過覆蓋關(guān)鍵方法來做

  • 主要是兩個(gè)方法 setAsText 和 getAsText 方法


/**
 * @author KinYang.Lau
 * @date 2020/12/18 11:00 上午
 *
 * 自定義 一個(gè) propertyEditor,
 * 這里不要直接去實(shí)現(xiàn)PropertyEditor接口,那樣太麻煩了,因?yàn)橛泻芏嘟涌谝獙?shí)現(xiàn)
 * 我們這里通過繼承PropertyEditorSupport類,通過覆蓋關(guān)鍵方法來做
 * 主要是兩個(gè)方法 setAsText 和 getAsText 方法
 */
public class CarPropertyEditor extends PropertyEditorSupport {
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		///  這實(shí)現(xiàn)我們的 字符串 轉(zhuǎn) 自定義對(duì)象的 邏輯
		if (StringUtils.hasText(text)) {
			String[] split = text.split("\\|");

			Car car = new Car();
			car.setColor(split[0]);
			car.setName(split[1]);
			setValue(car);
		}
		else {
			setValue(null);
		}
	}

	@Override
	public String getAsText() {
		Car value = (Car) getValue();
		return (value != null ? value.toString() : "");
	}
}

那么如何注冊到Spring的Spring的類型轉(zhuǎn)換器中呢?

這個(gè)也簡單,ConfigurableBeanFactory 接口有一個(gè)

void registerCustomEditor(Class<?> requiredType, Class<? extends PropertyEditor> propertyEditorClass);方法就是用于注冊CustomEditor的。

所以我們寫一個(gè)BeanFactory的后置處理器就可以了。

/**
 * @author KinYang.Lau
 * @date 2020/12/18 10:54 上午
 */
@Component
public class MyCustomEditorConfigurer implements BeanFactoryPostProcessor, Ordered {
	private int order = Ordered.LOWEST_PRECEDENCE;  // default: same as non-Ordered
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	///  把我們自定義的 轉(zhuǎn)換器器注冊進(jìn)去
		beanFactory.registerCustomEditor(Car.class, CarPropertyEditor.class);
	}

	@Override
	public int getOrder() {
		return this.order;
	}
}

下面我運(yùn)行一下程序,看看結(jié)果吧:

@Configuration
@ComponentScan("com.kinyang")
public class HelloApp {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(HelloApp.class);
		A bean = ac.getBean(A.class);
		bean.test();
	}
}

Spring中@Value注入復(fù)雜類型怎么用

搞定?。。?/p>

以上是“Spring中@Value注入復(fù)雜類型怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI