Spring2.5.6 中Bean的自动装配六种模式其一
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。autowire一共有六种类型。由于autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥![2] 在xml配置文件中,可以在<bean/>元素中使用autowire属性指定:
模式
|
说明
|
Default
|
在每个bean中都一个autowire=default的默认配置它的含义是:
采用beans和跟标签中的default-autowire="属性值"一样的设置。
|
On
|
不使用自动装配,必须通过ref元素指定依赖,默认设置。
|
下来我们就用案例来证明一下:准备3个类:
public class AddressServiceImpl {
/**住址*/
private String address;
public void setAddress(String address){
this.address=address;
}
}
public class HomeAddressServiceImpl extends AddressServiceImpl {
private String address;
public void setAddress(String address){
this.address=address;
}
public HomeAddressServiceImpl() {
super();
}
public HomeAddressServiceImpl(String address){
this.address=address;
}
}
public class EmpServiceImpl {
/**公司地址*/
private AddressServiceImpl companyAddress;
public void setCompanyAddress(AddressServiceImpl companyAddress){
this.companyAddress=companyAddress;
}
}
default和no值default.xml配置文件
<?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.0.xsd"
default-autowire="no">
<bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl"
scope="singleton">
<property name="address">
<value>北京海淀上地软件园</value>
</property>
</bean>
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"
scope="singleton" autowire="default" />
</beans>
测试类:(junit测试)
public class App {
@Test
public void test(){
ApplicationContext ac= new ClassPathXmlApplicationContext("classpath:default.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");
}
}
声明:
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"
scope="singleton" autowire="no" />
不使用自动装配,必须通过ref元素指定依赖,默认设置。
结束语:这几期将陆续的为大家解析Bean的自动装配中autowire中的值!
<!--EndFragment-->
分享到:
相关推荐
ean的自动装配,有4种 (1)no:不做任何操作 (2)byName:根据属性 名 自动装配,设值注入 <bean id="xxx" class="xxx" ></bean> (3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 <bean...
众所周知Spring里面的bean就类似是定义的一个组件,而这个组件的作用就是实现某个功能的,下面这篇文章主要给大家介绍了关于spring入门教程之bean继承与自动装配的相关资料,需要的朋友可以参考借鉴,下面随着小编来...
主要给大家介绍了关于spring装配bean的3种方式,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。 自动装配(autowiring):Spring自动满足bean之间的依赖。 1 使用@Component定义bean 在类声明的前面使用@Component对类进行标注,这...
博客地址:https://blog.csdn.net/u010476739/article/details/76732201 spring装配bean的方式实例
NULL 博文链接:https://364232252.iteye.com/blog/2369853
目的:Spring容器已经成功获取了UserController实例,并通过调用实例中的方法执行了各层中的输出语句。 运行结果为: User [id=1, name=张三, password=123] userDao say hello world! UserService say hello world ...
spring中的自动装配的形式byType、byName、no、construtor、autodetect和default(beans标签中没有,而bean标签中有该属性)共6种装配的方式的举例说明
Spring会在上下文中自动寻找,并自动给bean装配属性! 下面我们来做以下实验 建立如下测试环境:一个人一只猫一只狗 public class Dog { public void getshout(){ System.out.println("狗叫了三声"); } } public...
Spring Boot技术知识点:Bean装配1
本篇文章主要介绍了浅谈Spring装配Bean之组件扫描和自动装配,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了Spring学习之Bean的装配三种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了 Spring的自动装配Bean的三种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了spring在IoC容器中装配Bean详解,具有一定借鉴价值,需要的朋友可以参考下
如何向Spring Bean中注入一个JavautilProperties 请解释Spring Bean的自动装配 请解释自动装配模式的区别 如何开启基于注解的自动装配 请举例解释Required annotation 请举例解释Autowired注解 请举例说明Qualifier...
今天小编就为大家分享一篇关于在Spring中自动装配Bean的属性,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
在此基础上,又提供了spi机制,用spring.factories可以完成一个小组件的自动装配功能。 在一般业务场景,可能你不大关心一个bean是如何被注册进spring容器的。只需要把需要注册进容器的bean声明为@Component即可,...
主要介绍了Spring自动装配Bean实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了spring装配bean方法实例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring 自动装配及其注解 博客:https://blog.csdn.net/u010476739/article/details/76735241