Spring入门(5)-自动装配Bean属性

本文介绍如何装配Bean属性。

0. 目录

  1. ByName
  2. ByType
  3. constructor
  4. 默认自动装配
  5. 混合使用自动装配和显示装配

1. ByName

把与Bean的属性具有相同名字(或ID)的其他Bean自动装配到Bean对应的属性中。如果没有跟属性名称相匹配的Bean,则该属性不进行装配。

package com.chzhao.springtest;

public class PersonBll implements IPersonBll {

	private Person person;

	public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} public void show() {
System.out.println(this.person.getName());
}
}
<?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.xsd">
<bean name="person" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="byName">
</bean>
</beans>

2. ByType

把与Bean的属性具有相同类型的其他Bean自动装配到Bean对应的属性中。如果没有跟属性类型相匹配的Bean,则该属性不进行装配。

<?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.xsd">
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="byType">
</bean>
</beans>

如上,如果设置为byType,Bean的名字随便设置了,不需要为person

3. constructor

把与Bean的构造器入参具有相同类型的其他Bean自动装配到Bean构造器的对应入参中。

package com.chzhao.springtest;

public class PersonBll implements IPersonBll {

	public PersonBll(Person person) {
this.person = person;
} private Person person; public void show() {
System.out.println(this.person.getName());
}
}
<?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.xsd">
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value ="老王"></property>
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="constructor">
</bean>
</beans>

4. 默认自动装配

Spring可以通过设置default-autowire使所有Bean采取相同配置。

<?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.xsd"
default-autowire="constructor">
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value ="老王"></property>
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll">
</bean>
</beans>

5. 混合使用自动装配和显示装配

即使对某个Bean的属性采用了自动装配,仍然可以通过显示装配配置某个属性。

package com.chzhao.springtest;

public class PersonBll implements IPersonBll {

	public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} private Person person; public void show() {
System.out.println(this.person.getName());
}
}
<?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.xsd"
default-autowire="byType"
>
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="laoli" class="com.chzhao.springtest.Person">
<property name="name" value="老李" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" >
<property name="person" ref="laoli"></property>
</bean>
</beans>

Spring入门(5)-自动装配Bean属性的更多相关文章

  1. Spring学习笔记--自动装配Bean属性

    Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ...

  2. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  3. Spring自动装配Bean详解

    1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wirin ...

  4. Spring的自动装配Bean

    spring的自动装配功能的定义:无须在Spring配置文件中描述javaBean之间的依赖关系(如配置<property>.<constructor-arg>).IOC容器会 ...

  5. Spring自动装配Bean的五种方式

    在Spring中,支持 5 自动装配模式. no – 缺省情况下,自动配置是通过“ref”属性手动设定,在项目中最常用byName – 根据属性名称自动装配.如果一个bean的名称和其他bean属性的 ...

  6. 3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/

    1.自动装配/手动装配 xml配置文件里的bean自动装配 Spring IOC 容器里可以自动的装配Bean,需要做的仅仅是在<bean>的autowire属性里面指定自动装配模式 -& ...

  7. IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置

    1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...

  8. spring的自动装配Bean与自动检测Bean

    spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...

  9. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring自动装配Bean

    除了使用 XML 和 Annotation 的方式装配 Bean 以外,还有一种常用的装配方式——自动装配.自动装配就是指 Spring 容器可以自动装配(autowire)相互协作的 Bean 之间 ...

随机推荐

  1. js中的this怎么理解

    本博客供自己学习备忘, js中的this感觉很混乱,目前还有不少地方搞得不是很清楚,看到一篇不错的文章,先摘下来 this是Javascript语言的一个关键字它代表函数运行时,自动生成的一个内部对象 ...

  2. jquery select操作和联动操作

    (function(){ //select操作 $.fn.loadSelect = function(opt){ opt = $.extend({}, { required:false,//为真则添加 ...

  3. C# winform 窗体从右下角向上弹出窗口效果

    参考自 http://blog.csdn.net/yilan8002/article/details/7197981 /// <summary> /// 窗体动画函数 注意:要引用Syst ...

  4. Linux C enum

    /**************************************************************************** * Linux C enum * * 说明: ...

  5. I.MX6 linux Qt 同时支持Touch、mouse

    /***************************************************************************** * I.MX6 linux Qt 同时支持 ...

  6. 当ASP.NET MVC模型验证遇上CKEditor

    项目需要,使用到了CKEditor编辑器.这是个很不错的富文本编辑器,但是当它绑定的字段需要进行模型验证的时候,却会出现验证失效的问题.因此本文旨在记录这个问题和给出解决办法.以下以Validatio ...

  7. poj 1787 Charlie's Change

    // 题意 给定一个数p,要求用四种币值为1,5,10,25的硬币拼成p,并且硬币数要最多,如果无解输出"Charlie cannot buy coffee.",1<=p&l ...

  8. 【转】【玩转cocos2d-x之二十三】多线程和同步03-图片异步加载

    原创作品,转载请标明:http://blog.csdn.net/jackystudio/article/details/15334159 cocos2d-x中和Android,Windows都 一样, ...

  9. TCP/IP详解学习笔记(5)-IP选路,动态选路,和一些细节

    1.静态IP选路 1.1.一个简单的路由表 选路是IP层最重要的一个功能之一.前面的部分已经简单的讲过路由器是通过何种规则来根据IP数据包的IP地址来选择路由.这里就不重复了.首先来看看一个简单的系统 ...

  10. 五分钟solr4.5教程(搭建、运行)

    环境要求 jdk1.6及以上版本 solr发布版本 下载地址 http://lucene.apache.org/solr/mirrors-solr-latest-redir.html? 启动solr ...