构造函数Spring 自动装配模式之构造函数装配方式


这种模式与 byType 非常相似 , 但它应用于构造器参数 。 Spring 容器看作 beans , 在 XML 配置文件中 beans 的 autowire 属性设置为 constructor 。 然后 , 它尝试把它的构造函数的参数与配置文件中 beans 名称中的一个进行匹配和连线 。 如果找到匹配项 , 它会注入这些 bean , 否则 , 它会抛出异常 。
【构造函数Spring 自动装配模式之构造函数装配方式】例如 , 在配置文件中 , 如果一个 bean 定义设置为通过构造函数自动装配 , 而且它有一个带有 SpellChecker 类型的参数之一的构造函数 , 那么 Spring 就会查找定义名为 SpellChecker 的 bean , 并用它来设置构造函数的参数 。
也就是说 , 将构造函数的参数类型同candidate bean的ID进行匹配 。
你仍然可以使用标签连接其余属性 。
TextEditor:
public class TextEditor { private SpellChecker spellChecker private String name public TextEditor( SpellChecker spellChecker, String name ) { this.spellChecker = spellChecker this.name = name } public SpellChecker getSpellChecker() { return spellChecker } public String getName() { return name } public void spellCheck() { spellChecker.checkSpelling() }}
by constructor装配模式下的beans.xml:
&ltbeans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt&ltbean id="textEditor" class="com.sap.TextEditor"autowire="constructor"&gt &ltconstructor-arg value="http://news.hoteastday.com/a/Generic Text Editor"/&gt bean&gt
&ltbean id="SpellChecker" class="com.sap.SpellChecker"&gt bean&gtbeans&gt 注意 , 将com.sap.SpellChecker这个bean的ID改成任意值均可 。
构造函数Spring 自动装配模式之构造函数装配方式
本文插图
但是如果将这个bean注释掉 , 就会报异常:
构造函数Spring 自动装配模式之构造函数装配方式
本文插图
Jul 25, 2020 10:42:12 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefreshINFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ee285c6: startup date [Sat Jul 25 10:42:12 CST 2020] root of context hierarchyJul 25, 2020 10:42:12 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [Beans.xml]Jul 25, 2020 10:42:12 AM org.springframework.context.support.ClassPathXmlApplicationContext refreshWARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name "textEditor" defined in class path resource [Beans.xml]: Unsatisfied dependency expressed through constructor parameter 0 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type "com.sap.SpellChecker" available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name "textEditor" defined in class path resource [Beans.xml]: Unsatisfied dependency expressed through constructor parameter 0 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type "com.sap.SpellChecker" available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:729) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1270) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:85) at com.sap.MainApp.main(MainApp.java:8)Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type "com.sap.SpellChecker" available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1504) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:721) ... 15 more