在上一篇文章文本太长介绍了一下Spring自动装配(autowire)的三个属性,还有三个属性这里介绍。
4:constructor的解析
与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常
案例:
//spring中 app.xml的设置 <!-- 定义一个empServiceImpl的javabean实例 --> <bean id="empServiceImpl" class="com.csdn.service.EmpServiceImpl"> <property name="name"> <value>Nan</value></property> </bean> <!-- 定义一个hourEmpServiceImpl的javabean实例,实现empServiceImpl的业务层 --> <bean id="hourEmpServiceImpl" class="com.csdn.service.HourEmpServiceImpl" autowire="constructor" > <!— 如果autowire="constructor"在HourEmpServiceImpl 类中没有通过构造器赋值,那么empServiceImpl= null。 但是如果有构造器赋值,在xml中没有声明那么就会出现bug: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hourEmpServiceImpl' defined in class path resource [app.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.csdn.service.EmpServiceImpl]: : No unique bean of type [com.csdn.service.EmpServiceImpl] is defined: Unsatisfied dependency of type [class com.csdn.service.EmpServiceImpl]: expected at least 1 matching bean; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.csdn.service.EmpServiceImpl] is defined: Unsatisfied dependency of type [class com.csdn.service.EmpServiceImpl]: expected at least 1 matching bean at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:591) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:193) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:925) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:835) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) at java.security.AccessController.doPrivileged(Native Method) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at com.csdn.junit.EmpTest.test(EmpTest.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41) at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.junit.runners.ParentRunner.run(ParentRunner.java:220) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.csdn.service.EmpServiceImpl] is defined: Unsatisfied dependency of type [class com.csdn.service.EmpServiceImpl]: expected at least 1 matching bean at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:622) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:584) ... 42 more --> </bean> //empServiceImpl的代码 package com.csdn.service; public class EmpServiceImpl { /**定义一个变量实例*/ private String name; /**通过set方法进行赋值*/ public void setName(String name) { this.name = name; } } // hourEmpServiceImpl的代码 package com.csdn.service; public class HourEmpServiceImpl { /**定义一个EmpServiceImpl的操作对象*/ private EmpServiceImpl empServiceImpl; /**通过构造器实现赋值*/ public HourEmpServiceImpl(EmpServiceImpl empServiceImpl) { this.empServiceImpl = empServiceImpl; } } //junit测试类代码 @Test public void test(){ ApplicationContext ac = new ClassPathXmlApplicationContext("app.xml"); HourEmpServiceImpl hsi=(HourEmpServiceImpl) ac.getBean("hourEmpServiceImpl"); }
5:autodetect的解析
首先尝试 constructor来自动装配,然后使用byType方式。不确定性的处理与constructor和byType 方式一样
案例:
//spring中 app.xml的设置 <!-- 定义一个empServiceImpl的javabean实例 --> <bean id="empServiceImpl" class="com.csdn.service.EmpServiceImpl"> <property name="name"> <value>Nan</value></property> </bean> <!-- 定义一个hourEmpServiceImpl的javabean实例,实现empServiceImpl的业务层 --> <bean id="hourEmpServiceImpl" class="com.csdn.service.HourEmpServiceImpl" autowire=" autodetect" > <!—autowire的定义为autodetect首先尝试 constructor来自动装配,然后使用byType方式--> </bean> //empServiceImpl的代码 package com.csdn.service; public class EmpServiceImpl { /**定义一个变量实例*/ private String name; /**通过set方法进行赋值*/ public void setName(String name) { this.name = name; } } // hourEmpServiceImpl的代码 package com.csdn.service; public class HourEmpServiceImpl { /**定义一个EmpServiceImpl的操作对象*/ private EmpServiceImpl empServiceImpl; /**构造器赋值*/ public HourEmpServiceImpl(EmpServiceImpl empServiceImpl) { System.out.println("构造器赋值"); this.empServiceImpl = empServiceImpl; } /**生成相应的set方法 通过set方法注入的*/ public void setEmpServiceImpl(EmpServiceImpl empServiceImpl) { System.out.println("set方法赋值"); this.empServiceImpl = empServiceImpl; } } //junit测试类代码 @Test public void test(){ ApplicationContext ac = new ClassPathXmlApplicationContext("app.xml"); HourEmpServiceImpl hsi=(HourEmpServiceImpl) ac.getBean("hourEmpServiceImpl"); }
6: default的解析
由上级标签<beans>的default-autowire属性确定。
案例:
<?xml version="1.0" encoding="UTF-8"?> <beans 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-2.5.xsd" default-autowire="byName"> <!-- 定义一个empServiceImpl的javabean实例 --> <bean id="empServiceImpl" class="com.csdn.service.EmpServiceImpl"> <property name="name"> <value>楠楠</value></property> </bean> <!-- 定义一个hourEmpServiceImpl的javabean实例,实现empServiceImpl的业务层 --> <bean id="hourEmpServiceImpl" class="com.csdn.service.HourEmpServiceImpl" autowire=" default "> <!—这个bean的autowire属性的值为上一级跟标签的值--> </bean> </beans> //empServiceImpl的代码 package com.csdn.service; public class EmpServiceImpl { /**定义一个变量实例*/ private String name; /**通过set方法进行赋值*/ public void setName(String name) { this.name = name; } } // hourEmpServiceImpl的代码 package com.csdn.service; public class HourEmpServiceImpl { /**定义一个EmpServiceImpl的操作对象*/ private EmpServiceImpl empServiceImpl; /**生成相应的set方法 通过set方法注入的*/ public void setEmpServiceImpl(EmpServiceImpl empServiceImpl) { this.empServiceImpl = empServiceImpl; } } //junit测试类代码 @Test public void test(){ ApplicationContext ac = new ClassPathXmlApplicationContext("app.xml"); HourEmpServiceImpl hsi=(HourEmpServiceImpl) ac.getBean("hourEmpServiceImpl"); }
以上只用于spring2.5.6版本
相关推荐
Spring自动装配的方式和举例、以及@Qualifier、@Autowire、@Resource的使用。
08_编码剖析Spring装配基本属性的原理 09_Spring如何装配各种集合类型的属性 10_使用构造器装配属性 11_用@Resource注解完成属性装配 12_编码剖析@Resource注解的实现原理 13.@Autowire注解与自动装配 14.让...
#学习springboot自动装配 ##一,手动装配 ### 1,模式注解装配 @Component注解,或者@Component注解的扩展,@ Controller,@ Service,存储库,@ Configruation等, ### 2. @ Configuration启动容器+ @ Bean注册...
使用autowire属性指定自动装配的方式byName根据bean的名字和当前bean的setter风格属性名进行自动装配若有匹配,则自动转配若无匹配,则不装配
八、 自动装配autowire 13 (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:...
八、 自动装配autowire 13 (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:...
3.3.6. 自动装配(autowire)协作者 3.3.6.1. 设置Bean使自动装配失效 3.3.7. 依赖检查 3.3.8. 方法注入 3.3.8.1. Lookup方法注入 3.3.8.2. 自定义方法的替代方案 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. ...
3.3.6. 自动装配(autowire)协作者 3.3.7. 依赖检查 3.3.8. 方法注入 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. 其他作用域 3.4.4. 自定义作用域 3.5. 定制bean特性 3.5.1...
3.3.5. 自动装配(autowire)协作者 3.3.6. 依赖检查 3.3.7. 方法注入 3.4. Bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. Singleton beans和prototype-bean的依赖 3.4.4. 其他作用域 ...
3.3.5. 自动装配(autowire)协作者 3.3.6. 依赖检查 3.3.7. 方法注入 3.4. Bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. Singleton beans和prototype-bean的依赖 3.4.4. 其他作用域 ...
3.3.6. 自动装配(autowire)协作者 3.3.7. 依赖检查 3.3.8. 方法注入 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. 其他作用域 3.4.4. 自定义作用域 3.5. 定制bean特性 3.5.1...
spring基础,主要讲解了 spring的autowire:自动装配 collections:属性配置的细节 properties:外部属性文件的使用 relati:bean之间的关系 scope:bean的作用域 spel:spel的使用
可以在一个应用上下文中定义多个配置文件,每个配置文件设置自己的默认自动装配策略(default-autowire) 如果使用constructor自动装配策略,就不能混合使用constructor-arg 注解方式可以实现更细粒度的自动装配,...
讲述的内容:aop, autowire(自动装配),动态代理、静态代理、注解、hibernate集成、struts集成等,欢迎关注我的其他资源!
概述Mockitobeans是一个 ,通过使用Spring自己的@Autowire语法,您可以无缝地对所有bean进行自动装配。历史mockitobeans时,我一直在寻找一种方式来开始使用嘲笑说已经自动装配依赖其中有额外的自动装配Autowired的...
指定Spring框架的自动装配模式, 该属性的默认值是name, 即默认根据Bean的name属性自动装配. struts.objectFactory.spring.useClassCache 该属性指定整合Spring框架时,是否缓存Bean实例,该属性只允许使用true和false...