在上一篇文章文本太长介绍了一下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版本