继承

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="baseuser" class="Demo05.BaseUser">
  6. <property name="userid" value="1"/>
  7. <property name="username" value="rekent"/>
  8. <property name="password" value="123"/>
  9. </bean>
  10.  
  11. <bean id="user1" parent="baseuser">
  12. <property name="userid" value="2"/>
  13. </bean>
  14. </beans>

  例如:上述我创建了一个BaseUser,其后的只要继承了BaseUser的Bean都会有BaseUser的配置信息,此时只需要重写不同的部分即可。

  注意:这里的继承是指配置信息的重用,与面向对象的继承毫无关系。另外,Spring并没有要求配置信息存在继承关系的两个Bean是统一类型的,只要具有相关属性即可。


依赖

  Spring 通过Bean之间的引用ref建立了所有Bean之间的完整依赖关系,当实例化一个Bean时,IoC容器能保证该Bean所依赖的其他Bean已经初始化完毕。

  但是有时,可能要求Bean A的初始化必须在Bean B的初始化之后,而B不是A的属性,因此无法通过向A注入B来保证首先完成B的创建。

  此时便提供了depends-on属性来指定前置依赖的Bean。例如:

  1. <bean id="father" class=""></bean>
  2. <bean id="son" class="" depends-on="father"></bean>

作用域

  作用域通过<bean>元素的scope属性指定,Spring支持5种作用域。

作用域 描述
singleton 一个Bean定义对应唯一一个对象实例,Bean以单实例的方式存在(默认)
prototype 一个Bean定义对应多个对象实例,每次调用getBean()时,就创建一个新实例(开销大,不推荐)
request (仅在基于Web的SpringApplicationContext情形下有效)在一次Http请求中,一个Bean定义对应一个实例,即每个请求都会有各自的Bean。
session (仅在基于Web的SpringApplicationContext情形下有效)在一个HtppSession中,一个Bean定义对应一个实例。
global session (仅在基于Web的SpringApplicationContext情形下有效)在一个全局的Http Session中,一个Bean定义对应一个实例。

自动装配(源于他人,原文链接:http://www.cnblogs.com/sysman/p/4485199.html

可以使用bean元素的autowire属性指定自动装配的类型,spring支持如下类型:

自动装配的类型 描述
no/default autowire="no"指定spring不使用自动装配,需要手动装配
byName 按照bean属性的名字从spring容器中找同名的bean进行注入,适用于setter注入
byType 按照bean属性的类型从spring容器中找相同类型的bean进行注入,适用于setter注入
constructor 按照类型装配,跟byType类似.适用于构造器参数注入

下面我们将分别讲解着四种装配类型

不使用自动装配-no

我们之前讲解的所有的例子都属于这种类型.在这种情况下所有bean的装配都是手动进行的.我们再用一个例子复习下

1.新建包com.tutorialspoint.autowire,并在包中新建Cat.java、Dog.java、Duck.java.后面所有例子都会用到这三个类:

  1. //Cat.java
  2. package com.tutorialspoint.autowire;
  3.  
  4. public class Cat {
  5.  
  6. public void sayHi(){
  7. System.out.println("miao miao ... ");
  8. }
  9. }
  10.  
  11. //Dog.java
  12. package com.tutorialspoint.autowire;
  13.  
  14. public class Dog {
  15.  
  16. public void sayHi(){
  17. System.out.println("wang wang ... ");
  18. }
  19. }
  20.  
  21. //Duck.java
  22. package com.tutorialspoint.autowire;
  23.  
  24. public class Duck {
  25.  
  26. public void sayHi(){
  27. System.out.println("ga ga ... ");
  28. }
  29. }

2.新建包com.tutorialspoint.autowire.no,并在包中新建Zoo.java,内容如下:

  1. package com.tutorialspoint.autowire.no;
  2.  
  3. import com.tutorialspoint.autowire.*;
  4.  
  5. public class Zoo {
  6.  
  7. private Cat cat;
  8. private Dog dog;
  9. private Duck duck;
  10.  
  11. public void setCat(Cat cat) {
  12. this.cat = cat;
  13. }
  14. public void setDog(Dog dog) {
  15. this.dog = dog;
  16. }
  17. public void setDuck(Duck duck) {
  18. this.duck = duck;
  19. }
  20.  
  21. public void print(){
  22.  
  23. if(cat==null){
  24. System.out.println("cat is null");
  25. }else{
  26. cat.sayHi();
  27. }
  28.  
  29. if(dog==null){
  30. System.out.println("dog is null");
  31. }else{
  32. dog.sayHi();
  33. }
  34.  
  35. if(duck==null){
  36. System.out.println("duck is null");
  37. }else{
  38. duck.sayHi();
  39. }
  40. }
  41.  
  42. }

3.在src目录下新建autowire_no.xml配置文件,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7.  
  8. <bean id="cat" class="com.tutorialspoint.autowire.Cat"></bean>
  9. <bean id="dog" class="com.tutorialspoint.autowire.Dog"></bean>
  10. <bean id="duck" class="com.tutorialspoint.autowire.Duck"></bean>
  11.  
  12. <bean id="zoo" class="com.tutorialspoint.autowire.no.Zoo">
  13. <property name="cat" ref="cat"></property>
  14. <property name="dog" ref="dog"></property>
  15. </bean>
  16.  
  17. </beans>

4.在com.tutorialspoint.autowire.no包中新建MainApp.java.内容如下:

  1. package com.tutorialspoint.autowire.no;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class MainApp {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. ApplicationContext context = new ClassPathXmlApplicationContext("autowire_no.xml");
  11.  
  12. Zoo zoo = (Zoo) context.getBean("zoo");
  13.  
  14. zoo.print();
  15. }
  16. }

5.运行程序,检查结果:

通过上面的程序我们可以得出如下结论:

手动装配bean,bean的所有依赖项都要在bean元素中明确指定.如果不进行指定spring容器就不会注入该属性.

按照名字进行自动装配-byName

在byName装配方式下,spring首先会反射autowire="byName"的bean,得到bean中的所有属性名(根据setter推算),然后从容

器中寻找同名的bean,最后把找到的bean注入到当前bean中.我们还是用代码说话:

1.新建包com.tutorialspoint.autowire.byname,并在包中新建Zoo.java类.内容如下:

  1. package com.tutorialspoint.autowire.byname;
  2.  
  3. import com.tutorialspoint.autowire.Cat;
  4. import com.tutorialspoint.autowire.Dog;
  5. import com.tutorialspoint.autowire.Duck;
  6.  
  7. public class Zoo {
  8.  
  9. private Cat cat;
  10. private Dog dog;
  11. private Duck duck;
  12. // 自动装配并不适用于原始类型.这时候我们可以对该属性进行手动装配
  13. private String zooName;
  14.  
  15. public void setZooName(String zooName) {
  16. this.zooName = zooName;
  17. }
  18.  
  19. public void setCat(Cat cat) {
  20. this.cat = cat;
  21. }
  22.  
  23. public void setDog(Dog dog) {
  24. this.dog = dog;
  25. }
  26.  
  27. public void setDuck(Duck duck) {
  28. this.duck = duck;
  29. }
  30.  
  31. public void print() {
  32.  
  33. if (cat == null) {
  34. System.out.println("cat is null");
  35. } else {
  36. cat.sayHi();
  37. }
  38.  
  39. if (dog == null) {
  40. System.out.println("dog is null");
  41. } else {
  42. dog.sayHi();
  43. }
  44.  
  45. if (duck == null) {
  46. System.out.println("duck is null");
  47. } else {
  48. duck.sayHi();
  49. }
  50.  
  51. System.out.println(zooName);
  52. }
  53.  
  54. }

2.在src目录下新建autowire_byName.xml配置文件.内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7.  
  8. <!-- 名为zoo的bean在按照byName进行装配的时候,可以适配名字为cat、dog
  9. 的bean,不能适配名字为duck1的bean。所以最终zoo中会注入进cat和dog
  10. 不会注入duck. -->
  11. <bean id="cat" class="com.tutorialspoint.autowire.Cat"></bean>
  12. <bean id="dog" class="com.tutorialspoint.autowire.Dog"></bean>
  13. <bean id="duck1" class="com.tutorialspoint.autowire.Duck"></bean>
  14.  
  15. <bean id="zoo" class="com.tutorialspoint.autowire.byname.Zoo" autowire="byName">
  16. <!-- 由于自动装配仅适用于引用类型,普通类型还需要手动进行注入 -->
  17. <property name="zooName" value="international_zoo"></property>
  18. </bean>
  19.  
  20. </beans>

3.在包com.tutorialspoint.autowire.byname中新建MainApp.java.内容如下:

  1. package com.tutorialspoint.autowire.byname;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class MainApp {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. ApplicationContext context = new ClassPathXmlApplicationContext("autowire_byName.xml");
  11.  
  12. Zoo zoo = (Zoo)context.getBean("zoo");
  13.  
  14. zoo.print();
  15. }
  16. }

4.运行代码,检查结果:

分析结果可以看到cat和dog已经按照属性名字自动装配到了zoo中.duck1由于没有匹配的属性名所以没有进行装配.zooName是

我们手动进行注入的。

按照类型进行自动装配-byType

在byType装配方式下,spring首先会反射autowire="byType"的bean,得到bean属性的返回类型,然后去spring容器中按照类

型去匹配,最后把匹配到的bean注入到当前bean中.看个例子就明白了:

1.新建包com.tutorialspoint.autowire.bytype,并在包中新建Zoo.java类,内容如下:

  1. package com.tutorialspoint.autowire.bytype;
  2.  
  3. import com.tutorialspoint.autowire.Cat;
  4. import com.tutorialspoint.autowire.Dog;
  5. import com.tutorialspoint.autowire.Duck;
  6.  
  7. public class Zoo {
  8.  
  9. private Cat cat;
  10. private Dog dog;
  11. private Duck duck;
  12. // 自动装配并不适用于原始类型.这时候我们可以对该属性进行手动装配
  13. private String zooName;
  14.  
  15. public void setZooName(String zooName) {
  16. this.zooName = zooName;
  17. }
  18.  
  19. public void setCat(Cat cat) {
  20. this.cat = cat;
  21. }
  22.  
  23. public void setDog(Dog dog) {
  24. this.dog = dog;
  25. }
  26.  
  27. public void setDuck(Duck duck) {
  28. this.duck = duck;
  29. }
  30.  
  31. public void print() {
  32.  
  33. if (cat == null) {
  34. System.out.println("cat is null");
  35. } else {
  36. cat.sayHi();
  37. }
  38.  
  39. if (dog == null) {
  40. System.out.println("dog is null");
  41. } else {
  42. dog.sayHi();
  43. }
  44.  
  45. if (duck == null) {
  46. System.out.println("duck is null");
  47. } else {
  48. duck.sayHi();
  49. }
  50.  
  51. System.out.println(zooName);
  52. }
  53.  
  54. }

2.在src目录下新建配置文件autowire_byType.xml。内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7.  
  8. <!-- 名为zoo的bean在按照byType进行装配的时候,可以适配名字为cat1、dog1、duck1
  9. 的bean的类型。所以最终cat1、dog1、duck1会被分别注入进zoo的cat、dog、duck属性。
  10. 在按照类型进行装配的时候,如过有两个bean的类型符合的话,spring就不知道最终该使用哪个,这时候我们
  11. 可以使用primary="true"告诉spring优先使用本bean
  12. -->
  13. <bean id="cat1" class="com.tutorialspoint.autowire.Cat" primary="true"></bean>
  14. <bean id="cat2" class="com.tutorialspoint.autowire.Cat"></bean>
  15. <bean id="dog1" class="com.tutorialspoint.autowire.Dog"></bean>
  16. <bean id="duck1" class="com.tutorialspoint.autowire.Duck"></bean>
  17.  
  18. <bean id="zoo" class="com.tutorialspoint.autowire.bytype.Zoo" autowire="byType">
  19. <!-- 由于自动装配仅适用于引用类型,普通类型还需要手动进行注入 -->
  20. <property name="zooName" value="international_zoo"></property>
  21. </bean>
  22.  
  23. </beans>

3.在包com.tutorialspoint.autowire.bytype中新建MainApp.java。内容如下:

  1. package com.tutorialspoint.autowire.bytype;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class MainApp {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. ApplicationContext context = new ClassPathXmlApplicationContext("autowire_byType.xml");
  11.  
  12. Zoo zoo = (Zoo)context.getBean("zoo");
  13.  
  14. zoo.print();
  15. }
  16. }

4.运行程序,检查结果:

构造器参数类型自动装配-constructor

在constructor自动装配模式下,spring首先会反射bean的构造函数,得出构造函数的参数的类型,然后起spring容器中匹配合适的

类型的bean,最后使用构造器参数注入的方法把符合的bean注入到当前bean中。看代码:

1.新建包com.tutorialspoint.autowire.constructor,并在包中新建Zoo.java。内容如下:

  1. package com.tutorialspoint.autowire.constructor;
  2.  
  3. import com.tutorialspoint.autowire.*;
  4.  
  5. public class Zoo {
  6.  
  7. private Cat cat;
  8. private Dog dog;
  9. private Duck duck;
  10. private String zooName;
  11.  
  12. public Zoo(Cat cat, Dog dog, Duck duck, String zooName) {
  13. this.cat = cat;
  14. this.dog = dog;
  15. this.duck = duck;
  16. this.zooName = zooName;
  17. }
  18.  
  19. public void print() {
  20.  
  21. if (cat == null) {
  22. System.out.println("cat is null");
  23. } else {
  24. cat.sayHi();
  25. }
  26.  
  27. if (dog == null) {
  28. System.out.println("dog is null");
  29. } else {
  30. dog.sayHi();
  31. }
  32.  
  33. if (duck == null) {
  34. System.out.println("duck is null");
  35. } else {
  36. duck.sayHi();
  37. }
  38.  
  39. System.out.println(zooName);
  40. }
  41.  
  42. }

2.在src目录下新建autowire_constructor.xml,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7.  
  8. <!-- constructor跟byType十分相似.
  9. 名为zoo的bean在按照constructor进行装配的时候,可以适配名字为cat1、dog1、duck1
  10. 的bean的类型。所以最终cat1、dog1、duck1会被分别注入进zoo的cat、dog、duck属性(使用
  11. 构造器参数进行注入)。
  12. 在按照constructor进行装配的时候,如过有两个bean的类型符合的话,spring就不知道最终该使用哪个,这时候我们
  13. 可以使用primary="true"告诉spring优先使用本bean
  14. -->
  15. <bean id="cat1" class="com.tutorialspoint.autowire.Cat" primary="true"></bean>
  16. <bean id="cat2" class="com.tutorialspoint.autowire.Cat"></bean>
  17. <bean id="dog" class="com.tutorialspoint.autowire.Dog"></bean>
  18. <bean id="duck1" class="com.tutorialspoint.autowire.Duck"></bean>
  19.  
  20. <bean id="zoo" class="com.tutorialspoint.autowire.constructor.Zoo" autowire="constructor">
  21. <!-- 由于自动装配仅适用于引用类型,普通类型还需要手动进行注入 -->
  22. <constructor-arg name="zooName" value="international_zoo"></constructor-arg>
  23. </bean>
  24.  
  25. </beans>

3.在com.tutorialspoint.autowire.constructor包中新建MainApp.java。内容如下:

  1. package com.tutorialspoint.autowire.constructor;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class MainApp {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. ApplicationContext context = new ClassPathXmlApplicationContext("autowire_constructor.xml");
  11.  
  12. Zoo zoo = (Zoo)context.getBean("zoo");
  13.  
  14. zoo.print();
  15. }
  16. }

4.运行程序,检查结果:

如果使用sping的自动装配,本人不推荐使用xml的配置方式.最好使用注解的配置方式。原因如下:

1.基于xml的自动装配粒度态度。默认会装配所有符合条件的bean.不能指定哪个属性不进行自动装配

2.不能指定哪些属性必须进行装配,否则抛出异常.

以上两点使用spring的注解配置元数据都是可以做到的。我们下节就讲解spring注解配置元数据。

Spring 学习笔记(五)—— Bean之间的关系、作用域、自动装配的更多相关文章

  1. Spring学习记录(四)---bean之间的关系:继承、依赖

         继承 这里说的继承和java的继承是不一样的,不是父类子类.但思想很相似,是父bean和子bean 1.父bean是一个实例时.它本身是一个完整的bean 2.父bean是模板,抽象bean ...

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

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

  3. Spring学习笔记(2)——Bean的配置

    要使应用程序中的Spring容器成功启动,需要以下三个方面的条件都具备: 1.Spring框架的类包都已经放到应用程序的类路径下 2.应用程序为Spring提供完备的Bean配置信息 3.Bean的类 ...

  4. Spring学习笔记(3)——Bean的注入方式

    依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...

  5. Spring学习笔记--注入Bean属性

    这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果. package com.moonlit.myspring; import java.util.List; im ...

  6. Spring4.0学习笔记(3) —— Spring_Bean之间的关系

    1.继承关系 bean-relation.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  7. Spring学习笔记——02 Bean的命名及实例化

    一.Bean的命名 前一篇讲到IoC是一个管理Bean的容器,Bean多数情况下都是通过XML文件进行配置的,其中Bean的命名有以下几种方式,现在梳理一下. 1. 不指定id,只配置类名 <b ...

  8. Spring学习记录(五)---bean的作用域scope

    作用域:singleton:单例,整个应用中只创建一个实例(默认) prototype:原型,每次注入时都新建一个实例 session:会话,每个会话创建一个实例 request:请求,每个请求创建一 ...

  9. Spring学习笔记之bean配置

    1.命名bean 每个bean都有一个或者多个的的标识符.这些标识符必须在加载他们的容器里边唯一.一个bean经常有且只有一个标识符,但是如果需要超过一个的名字,可以考虑额外的别名. 基于xml的配置 ...

  10. Spring学习笔记之Bean的实例化

    一.bean的实例化方法有3种, 1.构造器实例化 2.静态工厂方法实例化 3.实例工厂方法实例化 二.用构造器来实例化 <bean id="ShunDao" class=& ...

随机推荐

  1. Ubuntu 14.04 VPS安装配置***的方法

    #安装*** $ sudo apt-get update $ sudo apt-get install python-gevent python-pip $ sudo pip install shad ...

  2. lambda函数,内置map()函数及filter()函数

    8.1 lambda函数 作用及意义:  1.没必要专门定义函数,给函数起名,起到精简的效果  2.简化代码的可读性 def ds(x): return 2 * x + 1 ds(5) ---11 g ...

  3. 4.vue引入axios同源跨域

    前言: 跨域方案有很多种,既然我们用到了Vue,那么就使用vue提供的跨域方案. 解决方案: 1.修改HttpRequestUtil.js import axios from 'axios' expo ...

  4. JS - 给数组的原型添加去掉重复元素的distinct方法

    /* 调用完该方法,原数组只留下非重复的数据 返回一个数组,里面是依次出现的重复元素 */Array.prototype.distinct = function () {    var removeA ...

  5. 基于Xtrabackup恢复单个innodb表

      Preface       We all know that Xtrabackup is a backup tool of percona for innodb or Xtradb.It's us ...

  6. linux基础目录

    第1章 linux目录结构 1.1 linux目录结构的特点 一切皆文件 1)倒挂的树状结构   一切从根开始 2)linux每个目录可以挂载在不同的设备(磁盘)上.windows不容易做到. /da ...

  7. 如何设置 html 中 select 标签不可编辑、只读

    转载自: https://blog.csdn.net/hjm4702192/article/details/33729767 1. <select style="width:195px ...

  8. 记一次微信小程序在安卓的白屏问题

    在做小程序的时候,做到了一个限时商品售卖,用到了倒计时,因为这个原因导致了安卓手机上使用小程序时,将小程序放入后台运行一段时间后,再次进入小程序后出现了页面白屏或者点击事件失效的情况,这里记录下 1. ...

  9. 【Ecshop】商品数据采集扩展

    一个自用的Ecshop商品数据采集程序 ->到此下载

  10. iPhone 横屏时默认会放大文字的问题

    有人说用 html { text-size-adjust: 100%; }我发现这个并不能解决问题.下面代码可以完美解决. 添加标签:<meta name="viewport" ...