引用其他Bean

  1. 组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能 , 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用。
  2. 在 Bean 的配置文件中 , 可以通过 <ref> 元素或 ref 属性为 Bean 的属性或者构造器参数指定对 Bean 的引用。
  3. 也可以在属性或者构造器里包含 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 id="person" class="com.itdjx.spring.dependency.injection.Person">
<property name="name" value="张三"/>
<property name="age" value="23"/>
<property name="sex" value="男"/>
<property name="car" ref="car3"/>
</bean> <bean id="car3" class="com.itdjx.spring.dependency.injection.Car">
<constructor-arg value="BaoMa" index="0"/>
<constructor-arg value="HuaChen" index="1"/>
<constructor-arg value="230000" index="2" type="double"/>
</bean> <bean id="car4" class="com.itdjx.spring.dependency.injection.Car">
<constructor-arg value="BaoMa" index="0"/>
<constructor-arg value="HuaChen" index="1"/>
<constructor-arg value="230" type="int"/>
</bean> </beans>
 package com.itdjx.spring.dependency.injection;

 /**
* 属性注入
*
* @author Wáng Chéng Dá
* @create 2017-02-28 15:14
*/
public class Person { private String name; private String sex; private int age; private Car car; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", car=" + car +
'}';
}
}
 package com.itdjx.spring.dependency.injection;

 /**
* 构造器注入
*
* @author Wáng Chéng Dá
* @create 2017-02-28 15:32
*/
public class Car { private String brand; private String address; private double price; private int maxSpeed; public Car(String brand, String address, double price) {
this.brand = brand;
this.address = address;
this.price = price;
} public Car(String brand, String address, int maxSpeed) {
this.brand = brand;
this.address = address;
this.maxSpeed = maxSpeed;
} public Car(String brand, double price, int maxSpeed) {
this.brand = brand;
this.price = price;
this.maxSpeed = maxSpeed;
} public Car() {
} @Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", address='" + address + '\'' +
", price=" + price +
", maxSpeed=" + maxSpeed +
'}';
}
}
 package com.itdjx.spring.dependency.injection;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* 依赖注入main
*
* @author Wáng Chéng Dá
* @create 2017-02-28 15:16
*/
public class MainIOC { public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml");
Person person = (Person) app.getBean("person");
System.out.println(person); }
}

控制台输出:

Person{name='张三', sex='男', age=23, car=Car{brand='BaoMa', address='HuaChen', price=230000.0, maxSpeed=0}}

内部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 id="person2" class="com.itdjx.spring.dependency.injection.Person">
<property name="name" value="李玉"/>
<property name="age" value="23"/>
<property name="sex" value="女"/>
<property name="car" >
<bean class="com.itdjx.spring.dependency.injection.Car">
<constructor-arg value="Ferrari" index="0"/>
<constructor-arg value="Italy" index="1"/>
<constructor-arg value="22500000" type="double"/>
</bean>
</property>
</bean> </beans>
 package com.itdjx.spring.dependency.injection;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* 依赖注入main
*
* @author Wáng Chéng Dá
* @create 2017-02-28 15:16
*/
public class MainIOC { public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml"); Person person = (Person) app.getBean("person2");
System.out.println(person); }
}

控制台输出:

Person{name='李玉', sex='女', age=23, car=Car{brand='Ferrari', address='Italy', price=2.25E7, maxSpeed=0}}

Spring学习--引用其他Bean , 内部Bean的更多相关文章

  1. spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入

    三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...

  2. Spring学习记录(二)---容器和bean属性配置

    下载spring包,在eclipse搭建spring环境. 这步我在eclipse中无法导入包,看网上的: http://sishuok.(和谐)com/forum/blogPost/list/242 ...

  3. Spring学习之旅(三)--装配Bean

    装配 Bean 的方式 在 XML 中进行显式配置 在 Java 中进行显式配置 隐式的 Bean 发现机制和自动装配 Spring 提供了以上三种方式进行 Bean 的配置,可以根据自己的需求选择一 ...

  4. Spring 学习指南 第三章 bean的配置 (未完结)

    第三章 bean 的配置 ​ 在本章中,我们将介绍以下内容: bean 定义的继承: 如何解决 bean 类的构造函数的参数: 如何配置原始类型 (如 int .float 等) .集合类型(如 ja ...

  5. Spring学习笔记(7)——Bean的基本配置

            先从IOC说起,这个概念其实是从我们平常new一个对象的对立面来说的,我们平常使用对象的时候,一般都是直接使用关键字类new一个对象,那这样有什么坏处呢?其实很显然的,使用new那么就 ...

  6. Spring学习笔记(二)之装配Bean

    一,介绍Bean的装配机制 在Spring中,容器负责对象的创建并通过DI来协调对象之间的关系.但是我们要告诉Spring创建哪些Bean并且如何将其装配在一起.,装配wiring就是DI依赖注入的本 ...

  7. Spring学习笔记(三)之装配Bean

    除了组件扫描与自动装配之外还有基于Java代码的装配与基于XML的装配. 有一些场景是我们不能用自动装配的,比如我们要给第三方库中的组件装配到我们的应用中,这时自动装配无效,因为自动装配只能扫描本应用 ...

  8. Spring 学习笔记(五)—— Bean之间的关系、作用域、自动装配

    继承 Spring提供了配置信息的继承机制,可以通过为<bean>元素指定parent值重用已有的<bean>元素的配置信息. <?xml version="1 ...

  9. Spring学习系列(二) 自动化装配Bean

    一.Spring装配-自动化装配 @Component和@ComponentScan 通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组 ...

随机推荐

  1. Spring 的好处?

    1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很容易实现如权限拦截,运行期监 ...

  2. Java 中编码与摘要算法

    URL 编码与解码 String s = "你好,世界!"; // URL 编码 String urlEncodedString = URLEncoder.encode(s, &q ...

  3. 20145202马超《JAVA》预备作业3

    虚拟机的安装[http://www.cnblogs.com/tuolemi/p/5861062.html] Linux命令[http://www.cnblogs.com/tuolemi/p/58781 ...

  4. stm32--FatFs移植(SPIFlash)

    前言 硬件: 单片机:stm32f072CB,sram大小16k.(其他单片机只要sram>8k即可通用) SPIFlash:W25Q128FV,16Mbyte,单次擦除最小4k. 程序使用Ke ...

  5. Sphinx与coreseek

    Sphinx : 高性能SQL全文检索引擎 分类 编程技术 Sphinx是一款基于SQL的高性能全文检索引擎,Sphinx的性能在众多全文检索引擎中也是数一数二的,利用Sphinx,我们可以完成比数据 ...

  6. CSS层叠样式表的解释

    css:    在标签上设置style属性css注释:     /*z注释内容*/css样式的编写位置:    1.在标签的的style属性里    2.在head里面,style标签中写样式     ...

  7. 第5讲——cin处理字符输入

    本来这一讲应该是while.for.if之类的,但是,我们可是学过C的男人,再浪费时间搞这个??? 还不如学点C++中的新知识. cin对象支持3种不同模式的单字符输入,其用户接口各不相同. 下面我们 ...

  8. 二分图的最大匹配——Hopcroft-Karp算法

    http://blog.csdn.net/wall_f/article/details/8248373

  9. c#和html方法互调

    具体见连接:https://www.cnblogs.com/zeroLove/p/3912460.html

  10. C++结构体排序

    在C++中,对结构体的排序方式比C语言丰富的多.在C语言中,我们主要是通过qsort进行排序操作(抛开手写排序算法不说). 在C++<algorithm>中,有一个十分强大的排序函数sor ...