在 Spring 的 IOC 容器里配置 Bean

<bean id="helloWorld" class="com.xiya.spring.beans.HelloWorld">
<property name="name" value="lgh"/>
</bean>

在 xml 文件中通过 bean 节点来配置 bean

id:Bean 的名称。

在 IOC 容器中必须是唯一的

若 id 没有指定,Spring 自动将权限定性类名作为 Bean 的名字

id 可以指定多个名字,名字之间可用逗号、分号、或空格分隔

  • 在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.
  • Spring 提供了两种类型的 IOC 容器实现. 
    • BeanFactory: IOC 容器的基本实现.
    • ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口.BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身;ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
  • 无论使用何种方式, 配置文件时相同的
ApplicationContext 的主要实现类:

ClassPathXmlApplicationContext:从 类路径下加载配置文件

FileSystemXmlApplicationContext: 从文件系统中加载配置文件

ConfigurableApplicationContext 扩展于 ApplicationContext,新增加两个主要方法:refresh() 和 close(), 让 ApplicationContext 具有启动、刷新和关闭上下文的能力

ApplicationContext 在初始化上下文时就实例化所有单例的 Bean。

WebApplicationContext 是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作


依赖注入的方式:
1、属性注入
属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象

属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值 

属性注入是实际应用中最常用的注入方式
<bean id="helloWorld" class="com.xiya.spring.beans.HelloWorld">
<property name="name" value="lgh"/>
</bean>

2、构造器注入
通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。

构造器注入在 <constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性
<bean id="car" class="com.xiya.spring.beans.Car">
<constructor-arg value="BMW"/>
<constructor-arg value="china"/>
<constructor-arg value="100000"/>
</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
class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求bean中必须有无参数的构造器
id:标识容器中的bean,且唯一
-->
<bean id="helloWorld" class="com.xiya.spring.beans.HelloWorld">
<property name="name" value="lgh"/>
</bean> <!--
通过构造方法来配置bean的属性
-->
<bean id="car1" class="com.xiya.spring.beans.Car">
<constructor-arg value="BMW" index="0"/>
<constructor-arg value="beijing" index="1"/>
<constructor-arg value="100000" index="2" type="double"/>
</bean>
<!--
使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器
-->
<bean id="car2" class="com.xiya.spring.beans.Car">
<constructor-arg value="Audi" index="0" type="java.lang.String"/>
<constructor-arg value="shanghai" index="1" type="java.lang.String"/>
<constructor-arg value="240" index="2" type="int"/>
</bean>
</beans>

package com.xiya.spring.beans;

/**
* Created by N3verL4nd on 2017/3/22.
*/
public class Car {
private String brand;
private String corp;
private double price;
private int maxSpeed; public Car(String brand, String corp, double price) {
this.brand = brand;
this.corp = corp;
this.price = price;
} public Car(String brand, String corp, int maxSpeed) {
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
} @Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", corp='" + corp + '\'' +
", price=" + price +
", maxSpeed=" + maxSpeed +
'}';
}
}


package com.xiya.spring.beans;

/**
* Created by N3verL4nd on 2017/3/22.
*/
public class HelloWorld {
private String name; public HelloWorld() {
System.out.println("HelloWorld's constructor...");
} public void setName(String name) {
this.name = name;
} public void hello() {
System.out.println("hello " + name);
}
}

package com.xiya.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by N3verL4nd on 2017/3/22.
*/
public class Main {
public static void main(String[] args) {
/*
//IOC容器为我们执行创建对象与赋值操作
HelloWorld helloWorld = new HelloWorld();
//为name属性赋值
helloWorld.setName("lgh");*/ //1、创建spring的IOC容器对象
//ApplicationContext代表IOC容器
//ClassPathXmlApplicationContext:是ApplicationContext接口的实现类,该实现类从类路径下(src目录下)加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2、从IOC容器中获取bean实例
//利用id返回到IOC容器中的bean
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
//利用类型返回IOC容器中的bean,但要求IOC容器中必须只能有一个该类型的bean
//HelloWorld helloWorld = context.getBean(HelloWorld.class);
//3、调用hello()方法
helloWorld.hello(); Car car = (Car) context.getBean("car1");
System.out.println(car); car = (Car) context.getBean("car2");
System.out.println(car);
}
}

字面值:可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入。

基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式

若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来,或者使用XML里的转译字符。

<value><![CDATA[<Audi>]]]></value>

XML中的转译字符

&(逻辑与)  &amp;        

<(小于)    &lt;        

>(大于)    &gt;        

"(双引号)  &quot;      

'(单引号)  &apos;
<bean id="car1" class="com.xiya.spring.beans.Car">
<constructor-arg value="beijing" index="1"/>
<constructor-arg value="&lt;BMW&gt;" index="0"/>
<constructor-arg value="100000" index="2" type="double"/>
</bean>
<!--
使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器
-->
<bean id="car2" class="com.xiya.spring.beans.Car">
<constructor-arg index="0" type="java.lang.String">
<value><![CDATA[<Audi>]]]></value>
</constructor-arg>
<constructor-arg value="shanghai" index="1" type="java.lang.String"/>
<constructor-arg index="2" type="int" value="240"/>
</bean>

组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能. 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用

在 Bean 的配置文件中, 可以通过 <ref> 元素或 ref  属性为 Bean 的属性或构造器参数指定对 Bean 的引用. 

<bean id="person1" class="com.xiya.spring.beans.Person">
<!--属性注入-->
<!--<property name="name" value="lgh"/>
<property name="age" value="24"/>
<property name="car" ref="car2"/>-->
<!--构造器注入-->
<constructor-arg value="lgh"/>
<constructor-arg value="24"/>
<!--可以使用property的ref属性建立bean之间的引用关系-->
<constructor-arg index="2" ref="car1"/>
</bean>

也可以在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean

<bean id="person2" class="com.xiya.spring.beans.Person">
<property name="name" value="lgh"/>
<property name="age" value="24"/>
<!--内部bean,不能够被外部引用-->
<property name="car">
<bean class="com.xiya.spring.beans.Car">
<constructor-arg value="BMW"/>
<constructor-arg value="shanghai"/>
<constructor-arg value="100000.0"/>
<constructor-arg value="240"/>
</bean>
</property>
</bean>

当 Bean 实例仅仅给一个特定的属性使用时, 可以将其声明为内部 Bean. 内部 Bean 声明直接包含在 <property> 或 <constructor-arg> 元素里, 不需要设置任何 id 或 name 属性

内部 Bean 不能使用在任何其他地方

可以使用专用的 <null/> 元素标签为 Bean 的字符串或其它对象类型的属性注入 null 值

和 Struts、Hiberante 等框架一样,Spring 支持级联属性的配置。
<property name="car">
<bean class="com.xiya.spring.beans.Car">
<constructor-arg value="BMW"/>
<constructor-arg>
<null/>
</constructor-arg>
<constructor-arg value="100000.0"/>
<constructor-arg value="240"/>
</bean>
</property>

<constructor-arg index="2" ref="car1"/>
<property name="car.brand" value="Audi"/>

在 Spring中可以通过一组内置的 xml 标签(例如: <list>, <set> 或 <map>) 来配置集合属性.

配置 java.util.List 类型的属性, 需要指定 <list>  标签, 在标签里包含一些元素. 这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用. 通过<bean> 指定内置 Bean 定义. 通过 <null/> 指定空元素. 甚至可以内嵌其他集合.

数组的定义和 List 一样, 都使用 <list>

配置 java.util.Set 需要使用 <set> 标签, 定义元素的方法与 List 一样.

Java.util.Map 通过 <map> 标签定义, <map> 标签里可以使用多个 <entry> 作为子标签. 每个条目包含一个键和一个值. 

必须在 <key> 标签里定义键

因为键和值的类型没有限制, 所以可以自由地为它们指定 <value>, <ref>, <bean> 或 <null> 元素. 

可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义

使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 作为子标签. 每个 <prop> 标签必须定义 key 属性. 
<property name="cars">
<!--使用list节点为list类型的属性赋值-->
<list>
<ref bean="car1"/>
<ref bean="car2"/>
<bean class="com.xiya.spring.beans.collections.Car">
<constructor-arg value="Ford"/>
<constructor-arg value="changan"/>
<constructor-arg value="200000.0"/>
<constructor-arg value="200"/>
</bean>
</list>
</property>
<!--配置Properties属性值-->
<bean id="dataSource" class="com.xiya.spring.beans.collections.DataSource">
<property name="properties">
<!--使用props和prop子节点来为Properties属性赋值-->
<props>
<prop key="user">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
<bean id="newPerson" class="com.xiya.spring.beans.collections.NewPerson">
<property name="name" value="xiya"/>
<property name="age" value="24"/>
<property name="cars">
<map>
<entry key="BMW" value-ref="car1"/>
<entry key="Audi" value-ref="car2"/>
</map>
</property>
</bean>

使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合, 所以无法在不同 Bean 之间共享集合.

可以使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 <beans> 根元素里添加 util schema 定义

<!--配置独立的集合bean,以供多个bean进行引用-->
<util:list id="cars">
<ref bean="car1"/>
<ref bean="car2"/>
</util:list> <bean id="person4" class="com.xiya.spring.beans.collections.Person">
<property name="name" value="lgh"/>
<property name="age" value="24"/>
<property name="cars" ref="cars"/>
</bean>
•为了简化 XML 文件的配置,越来越多的XML文件采用属性而非子元素配置信息。
•Spring 从 2.5版本开始引入了一个新的p命名空间,可以通过<bean>元素属性的方式配置Bean的属性。
•使用 p 命名空间后,基于XML的配置方式将进一步简化

Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式

byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配.

byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同.

constructor(通过构造器自动装配): 当 Bean 中存在多个构造器时, 此种自动装配方式将会很复杂. 不推荐使用
在 Bean 配置文件里设置 autowire 属性进行自动装配将会装配 Bean 的所有属性. 然而, 若只希望装配个别属性时, autowire 属性就不够灵活了. 

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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.xiya.spring.beans.autowire.Address" p:city="beijing" p:street="dawanglu"/>
<bean id="car" class="com.xiya.spring.beans.autowire.Car" p:brand="Audi" p:price="400000"/>
<bean id="person" class="com.xiya.spring.beans.autowire.Person" p:name="lgh" p:address-ref="address" p:car-ref="car"/>
<!--
可以使用autowire属性指定自动装配的方式
byName根据bean的名字和当前bean的setter风格的属性名进行自动装配,若没有匹配的,则不进行装配。
byType根据bean的类型和当前bean的属性的类型进行自动装配。若IOC容器由1个以上的配型匹配,则抛异常。
-->
<!--<bean id="person" class="com.xiya.spring.beans.autowire.Person" p:name="autowire" autowire="byType"/>-->
</beans>

Spring 允许继承 bean 的配置, 被继承的 bean 称为父 bean. 继承这个父 Bean 的 Bean 称为子 Bean

子 Bean 从父 Bean 中继承配置, 包括 Bean 的属性配置

子 Bean 也可以覆盖从父 Bean 继承过来的配置

父 Bean 可以作为配置模板, 也可以作为 Bean 实例. 若只想把父 Bean 作为模板, 可以设置 <bean> 的abstract 属性为 true, 这样 Spring 将不会实例化这个 Bean

并不是 <bean> 元素里的所有属性都会被继承. 比如: autowire, abstract 等.

也可以忽略父 Bean 的 class 属性, 让子 Bean 指定自己的类, 而共享相同的属性配置. 但此时 abstract 必须设为 true

<!--
抽象bean:bean的abstract属性为true的bean,它不能被IOC容器实例化,只能被继承
若某一个bean的class属性没有指定,则该bean必须是一个抽象bean
-->
<bean id="address" class="com.xiya.spring.beans.autowire.Address"
p:city="beijing" p:street="dawanglu" abstract="false"/> <!--bean配置的继承:使用bean的parent属性指定继承哪个bean的配置-->
<bean id="address2" parent="address" p:street="wudaokou "/>

Spring 允许用户通过 depends-on 属性设定 Bean 前置依赖的Bean,前置依赖的 Bean 会在本 Bean 实例化之前创建好

如果前置依赖于多个 Bean,则可以通过逗号,空格或的方式配置 Bean 的名称

<!--要求在配置bean时,必须有一个关联的car,或者说person这个bean依赖于car这个bean-->
<bean id="person" class="com.xiya.spring.beans.autowire.Person"
p:name="lgh" p:address-ref="address2" depends-on="car"/>

Spring配置 bean的更多相关文章

  1. spring 配置bean

    Main(测试方法) public class Main { public static void main(String[] args) { //1.创建Spring 的IOC容器对象: //spr ...

  2. Spring配置bean的详细知识

    在Spring中配置bean的一些细节.具体信息请参考下面的代码及注释 applicationContext.xml文件 <?xml version="1.0" encodi ...

  3. Spring -- 配置bean的三种方法

    配置通过静态工厂方法创建的bean public class StaticBookFactory { //静态工厂方法: public static Book getBook(String bookN ...

  4. Spring配置Bean,为属性赋值

    SayHello的实体类: package com.langchao; /** * @ClassName: SayHello * @description: * @author: ZhangYawei ...

  5. Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件

    Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...

  6. Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系

    XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...

  7. Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节

    在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...

  8. spring 配置bean的方法及依赖注入发方式

    Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).FactoryBean 这里依据全类名配置bean <bean id="helloWo ...

  9. Spring入门第二课:Spring配置Bean的细节

    1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...

  10. spring 配置bean-自己主动装配

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/28260477 概要:(蓝色为本节所讲) ...

随机推荐

  1. Atlas 读写分离

    1.前置条件 需要配置好mysql 主从 主库:192.168.28.137:16205 从库:192.168.28.135:16205 Atlas:192.168.28.139 2.Atlas 部署 ...

  2. Mybatis是如何实现SQL防注入的

    Mybatis这个框架在日常开发中用的很多,比如面试中经常有一个问题:$和#的区别,它们的区别是使用#可以防止SQL注入,今天就来看一下它是如何实现SQL注入的. 什么是SQL注入 在讨论怎么实现之前 ...

  3. .gitignore 文件配置

    git 使用过程中,有许多文件或者文件夹是不希望更新到远程仓库了,因为他们比较占地方,这个时候我们可以利用 .gitignore 文件忽略文件. 按项目进行忽略 .gitignore 文件用于忽略文件 ...

  4. linux下大文件查询具体段内容

    有时候我们的文件比较大,比如几十G,甚至上百G.这么大的文件怎么查询呢? 有很多种方法都可以实现,这儿选择用 cat 这个命令实现. 先来看看 cat 的介绍 cat 有个对应的命令 tac,cat反 ...

  5. 进一步学习 nox 教程,轻松掌握命令行用法

    英文 |Command-line usage 出处 | nox 官方文档 译者 | 豌豆花下猫@Python猫 Github地址:https://github.com/chinesehuazhou/n ...

  6. 【ARM】---关于ARM内核与架构的解释

    本文摘自某论坛某位大神的一段回复,经典至极,copy来己用! 只要你玩过ARM内核的芯片,那么关于内核和架构,我想应该或多或少的困惑过你,看了下面的介绍,你应该会清楚很多! 好比你盖房子,刚开始因为水 ...

  7. NTT - 牛客

    链接:https://www.nowcoder.com/acm/contest/133/D来源:牛客网 题目描述 Applese打开了m个QQ群,向群友们发出了组队的邀请.作为网红选手,Applese ...

  8. C# 调用word进程操作文档关闭进程

    C# 调用word进程操作文档关闭进程 作者:Jesai 时间:2018-02-12 20:36:23 前言: office办公软件作为现在主流的一款办公软件,在我们的日常生活和日常工作里面几乎每天都 ...

  9. Qt Installer Framework翻译(3-3)

    移除组件 下图说明了删除所有或某些已安装组件的默认工作流程: 本节使用在macOS上运行的Qt 5维护工具为例,来演示用户如何删除所有或部分选定组件. 移除所有组件 用户启动维护工具时,将打开&quo ...

  10. CSS中设置元素的圆角矩形

    圆角矩形介绍 在CSS中通过border-radius属性可以实现元素的圆角矩形. border-radius属性值一共有4个,左上.右上.左下.右下. border-radius属性值规则如下:第一 ...