`
kuaile863
  • 浏览: 115445 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring自动装配(autowire)麻烦吗?(二)

阅读更多

    在上一篇文章文本太长介绍了一下Spring自动装配(autowire)的三个属性,还有三个属性这里介绍。

4constructor的解析

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"); 
}



 

5autodetect的解析

首先尝试 constructor来自动装配,然后使用byType方式。不确定性的处理与constructorbyType 方式一样

 

案例:

//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版本

2
6
分享到:
评论

相关推荐

    Spring实现自动装配

    Spring框架是Java开发中不可或缺的一部分,它以其强大的依赖注入(Dependency Injection,简称DI)特性而闻名,其中自动装配(Auto-Wiring)是DI的一种实现方式。自动装配允许开发者减少手动配置bean之间的依赖关系...

    Spring自动装配解析

    在Spring的XML配置文件中,可以通过`&lt;beans&gt;`标签的`autowire`属性来全局设置自动装配策略,或者在单个`&lt;bean&gt;`标签中通过`autowire`属性指定某一个bean的自动装配方式。 4. 使用注解进行自动装配 Spring 2.5引入了...

    spring自动装配

    在压缩包文件名称“spring_0700_IOC_Autowire”中,“IOC”代表“Inversion of Control”,即控制反转,它是Spring的核心概念之一,自动装配是IOC的一种实现方式。这个文件可能包含了关于Spring自动装配的教程材料,...

    Spring自动装配的方式

    Spring自动装配的方式和举例、以及@Qualifier、@Autowire、@Resource的使用。

    Spring自动装配模式表

    ### Spring自动装配模式详解 #### 一、引言 在Spring框架中,自动装配是一种非常实用的功能,可以简化Bean的依赖注入过程。Spring提供了多种自动装配模式,每种模式都有其适用场景。本文将详细介绍Spring框架中的...

    spring-autowire-demo.zip

    本示例"spring-autowire-demo.zip"是基于Spring 5.3.6版本,通过多个示例展示了自动装配的不同方式:default、byName、byType和constructor,帮助我们更深入理解这个功能。 首先,让我们了解什么是自动装配。自动...

    spring-autowire.zip

    《Spring自动装配详解——基于IDEA 2020的小实例》 在Java开发领域,Spring框架以其强大的功能和灵活性,成为了企业级应用开发的首选。其中,Spring的自动装配特性(Autowired)是其核心特性之一,极大地简化了依赖...

    自动装配 AutoWire

    **自动装配(Autowired)是Spring框架中的一个重要特性,它允许我们自动将依赖注入到bean中,而无需显式地在配置文件中指定依赖关系。在本文中,我们将深入探讨Autowired的工作原理、使用方式以及它如何简化Java应用的...

    Spring的自动装配源代码

    在Spring框架中,自动装配(Auto-Wiring)是一种简化配置的方式,它允许Spring容器自动为Bean提供依赖。这种特性使得开发者无需显式地在XML配置文件中声明Bean之间的依赖关系,从而减少了配置工作量,提高了代码的可...

    Spring中自动装配的4种方式

    byName 自动装配是指通过设置 property 标签的 autowire 属性为 byName,Spring 将自动寻找一个与该属性名称相同或 id 相同的 Bean,注入进来。例如: ```xml &lt;property name="dataSource" autowire="by...

    Spring自动装配Bean实现过程详解

    默认情况下,配置文件中需要通过ref装配Bean,但设置了autowire="byName",Spring会在配置文件中自动寻找与属性名字personDao相同的bean,找到后,通过调用setPersonDao(PersonDao personDao)方法将id为personDao的...

    autowire自动导入句柄

    在Spring框架中,`autowire`是一种自动装配bean依赖的方式,它极大地简化了bean之间的依赖注入过程。本文将深入探讨`autowire`自动导入句柄及其在实际开发中的应用。 ### 1. `autowire`简介 `autowire`是Spring...

    3Spring使用annotation方式autowire

    本篇将详细探讨"3Spring使用annotation方式autowire"这一主题,包括注解驱动的自动装配(Autowiring)以及相关的源码分析。 ### 一、注解驱动的自动装配 自动装配是Spring框架的一个核心特性,它允许框架自动管理...

    Spring的Autowired自动装配(XML版本+Annotation版本+源码+解析)

    本篇将重点讲解Spring的@Autowired自动装配机制,包括XML配置版和注解版,并深入到源码层面进行解析。 @Autowired是Spring框架中的一个核心注解,用于实现自动装配bean的需求。当我们想要在类中注入某个依赖时,...

    Spring自动装配与扫描注解代码详解

    autowire 是 Spring 中的一种自动装配机制,通过在 Bean 的配置文件中使用 autowire 属性,可以将 Bean 之间的依赖关系自动装配起来。autowire 属性可以取三个值:no、byName、byType。 * no:不进行自动装配,这是...

    pring自动装配方式介绍共2页.pdf.zip

    在Spring框架中,自动装配(Auto-Wiring)是一种简化依赖注入(Dependency Injection,简称DI)的方式,它允许Spring容器自动管理Bean之间的依赖关系,而无需显式地在配置文件中指定。Spring提供了多种自动装配模式...

    彻底搞明白Spring中的自动装配和Autowired注解的使用

    在 Spring 中,自动装配是通过 AutowireCapableBeanFactory 接口来实现的,该接口定义了四种自动装配策略:byName、byType、constructor 和 AUTOWIRE_AUTODETECT(已被弃用)。 1. byName 自动装配 byName 自动...

    spring入门教程之bean的继承与自动装配详解

    这里的`autowire="byName"`指示Spring尝试找到一个名为`name`的Bean来自动装配到`Person`类的相应属性上。 Bean的继承是通过`parent`属性实现的,允许一个Bean继承另一个Bean的配置。这样可以重用和覆盖父Bean的...

Global site tag (gtag.js) - Google Analytics