引用其他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. TCD产品技术参考资料

    1.Willis环 https://en.wikipedia.org/wiki/Circle_of_Willis 2.TCD仿真软件 http://www.transcranial.com/index ...

  2. JVM内存管理机制和垃圾回收机制

    JVM内存管理机制和垃圾回收机制 JVM结构 图片描述: java源码编译成class文件 class文件通过类加载器加载到内存 其中方法区存放的是运行时的常量.静态变量.类信息等,被所有线程共享 堆 ...

  3. Delphi中Templates代码模板添加注意事项

    今天用Delphi中的代码模板添加一段代码,结果就是有问题,多次测试后,发现是编码需要注意. <?xml version="1.0" encoding="GB231 ...

  4. A problem occurred evaluating project ':'. > ASCII

    项目编译出错: 错误信息如下: FAILURE: Build failed with an exception. * Where: Build file 'F:\git\i***\build.grad ...

  5. uwsgi配置文件

    [uwsgi] http = :9000 #the local unix socket file than commnuincate to Nginx #socket端口这个用作nginx与其通讯 s ...

  6. 复制MySQL数据库A到另外一个MySQL数据库B(仅仅针对innodb数据库引擎)

    方案一:(不用太大的变化my.ini文件) copy 原数据库A中的   数据库(database)  ib_logfile1  ib_logfile0   ibdata1: 关闭目的数据库B: 备份 ...

  7. TFTP & commons-net-3.3.jar

    项目需求:上传文件到服务器,TFTP 了解TFTP http://wenku.baidu.com/link?url=MhRVgIySotFMkm5ar6B71zROPMoqC7cd5cSbKJo2kx ...

  8. 第十篇 Python的字符串格式化

    字符串格式化:就是按照你的意愿做一个拼接的过程. 1. 字符串格式化的第一种方式:百分号方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. %[ ...

  9. gitbook.explore更新升级了, 不能再搜索了

    www.gitbook.com/explore 不再是一个索引页面 Can I browse existing projects on GitBook ? The new version of Git ...

  10. centos tomcat开机自启

    在 /etc/rc.local 下 输入tomcat bin目录下的startup.sh  /usr/tomcat8/bin/startup.sh 即可