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

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

阅读更多

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

简单介绍一下Spring autowire(自动装配)那首先问问你,你觉得springautowire(自动装配)好吗?

如果使用它可能会降低可读性和可维护性,但是spring中类与类之间的依赖都用<ref>标签来连接,这样太费事了。那你如何选择呢?

 

下面介绍Spring为我们提供了autowire(自动装配)的属性。

 

  spring配置文件中autowire属性值如下:

1no解析

不使用自动装配,是autowire默认的值。必须通过ref元素指定依赖,这是默认设置。

案例:

//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="no"  >
<!—autowire的定义为no代表实现其它类时需要用ref -->
	<property name="empServiceImpl" ref="empServiceImpl"></property>
	</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;
/**生成相应的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"); 
}

 

2byName解析

通过属性名的方式查找spring容器,检测javabean的名字与属性完全一致的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="byName"  >
<!—autowire的定义为byName代表实现其它类,自动检测javabean的名字与属性完全一样的注意:如果实现的javabean没有在Spring的xml中实例,那么也不会报错只是为空值-->
</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;
/**生成相应的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"); 
}


 

3byType的解析

如果容器中存在一个与指定属性类型相同的bean,如果没有找到相符的bean,该属性就没有被装配上。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。

案例:

//spring中 app.xml的设置
<!-- 定义一个empServiceImpl的javabean实例 -->
	<bean id="empServiceImpl" class="com.csdn.service.EmpServiceImpl">
	<property name="name">
	<value>Nan</value></property>
	</bean>
<!-- 定义一个HourAddressServiceImpl的javabean实例实现了EmpServiceImpl的javabean那么会出现bug:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hourEmpServiceImpl' defined in class path resource [app.xml]: Unsatisfied dependency expressed through bean property 'empServiceImpl': : No unique bean of type [com.csdn.service.EmpServiceImpl] is defined: expected single matching bean but found 2: [empServiceImpl, com.csdn.service.HourAddressServiceImpl#0]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.csdn.service.EmpServiceImpl] is defined: expected single matching bean but found 2: [empServiceImpl, com.csdn.service.HourAddressServiceImpl#0]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1091)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:982)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
	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: expected single matching bean but found 2: [empServiceImpl, com.csdn.service.HourAddressServiceImpl#0]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:621)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1076)
	... 40 more

	<bean class="com.csdn.service.HourAddressServiceImpl"></bean>
-->
<!-- 定义一个hourEmpServiceImpl的javabean实例,实现empServiceImpl的业务层  -->
<bean id="hourEmpServiceImpl" class="com.csdn.service.HourEmpServiceImpl" autowire="byType"  >
<!—autowire的定义为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;
/**生成相应的set方法  通过set方法注入的*/
	public void setEmpServiceImpl(EmpServiceImpl empServiceImpl) {
		this.empServiceImpl = empServiceImpl;
	}
}
// HourAddressServiceImpl的代码
package com.csdn.service;
//这里只是继承了EmpServiceImpl的实例javabean
public class HourAddressServiceImpl extends EmpServiceImpl {
}
//junit测试类代码
 @Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("app.xml");
HourEmpServiceImpl  hsi=(HourEmpServiceImpl) ac.getBean("hourEmpServiceImpl"); 
}
注意:父类与子类匹配的方式是:子类自动装载成父类的对象。
	  接口与实现类:实现类的对象可以自动装载到接口的对象。否则会出现空指针异常。

 

1
7
分享到:
评论

相关推荐

    Spring自动装配解析

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

    Spring实现自动装配

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

    spring自动装配

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

    Spring自动装配的方式

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

    Spring自动装配模式表

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

    spring-autowire-demo.zip

    在Spring框架中,自动装配(Autowiring)是一种强大的特性,它允许我们无需显式配置bean之间的依赖关系,Spring会自动地将bean的依赖注入到它们的实例中。本示例"spring-autowire-demo.zip"是基于Spring 5.3.6版本,...

    spring-autowire.zip

    其中,Spring的自动装配特性(Autowired)是其核心特性之一,极大地简化了依赖注入的过程。本篇文章将通过在IDEA 2020环境下构建的Spring 5小实例,深入解析Spring的@Autowired注解,帮助读者理解和掌握这一关键知识...

    Spring的自动装配源代码

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

    自动装配 AutoWire

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

    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版本+源码+解析)

    @Autowired是Spring框架中的一个核心注解,用于实现自动装配bean的需求。当我们想要在类中注入某个依赖时,不再需要手动通过setter方法或构造函数进行设置,而是通过@Autowired来自动完成。这种特性极大地提高了代码...

    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入门教程之bean的继承与自动装配详解

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

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

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

Global site tag (gtag.js) - Google Analytics