一、bean之间的关系:

1)继承:

People.java实体类:

package com.cy.entity;

public class People {
private int id;
private String name;
private int age;
private String className; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
} @Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age
+ ", className=" + className + "]";
} }

beans.xml配置文件:

<?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="byName"> <bean id="abstractPeople" class="com.cy.entity.People" abstract="true">
<property name="className" value="高三(1)班"></property>
<property name="age" value="18"></property>
</bean> <bean id="zhangsan" parent="abstractPeople">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean> <bean id="lisi" parent="abstractPeople">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
</bean>
</beans>

测试代码:

@Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test() {
People zhangsan = (People) ac.getBean("zhangsan");
System.out.println(zhangsan); People lisi = (People) ac.getBean("lisi");
System.out.println(lisi);
}

2)依赖:

spring中加载bean的方式默认是按照bean配置的顺序加载的;

例如,查看zhangsan之前,必须要有权限,要先获取权限;

People.java:

package com.cy.entity;

public class People {
private int id;
private String name;
private int age;
private String className; public People(){
System.out.println("People初始化..");
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
} @Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age
+ ", className=" + className + "]";
} }

com.cy.service.Authority:

package com.cy.service;

public class Authority {
public Authority(){
System.out.println("获取权限..");
}
}

beans.xml配置:

<?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="byName"> <bean id="abstractPeople" class="com.cy.entity.People" abstract="true">
<property name="className" value="高三(1)班"></property>
<property name="age" value="18"></property>
</bean> <bean id="zhangsan" parent="abstractPeople">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean> <bean id="lisi" parent="abstractPeople" depends-on="authority">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
</bean> <bean id="authority" class="com.cy.service.Authority"></bean>
</beans>

测试代码:

@Test
public void test() {
People zhangsan = (People) ac.getBean("zhangsan");
System.out.println(zhangsan); People lisi = (People) ac.getBean("lisi");
System.out.println(lisi);
}

配置了depend on属性之后,加载lisi这个bean必须要先加载authority这个bean,所以bean的加载顺序变了,打印如下:

3)引用:

就是这样子:

<bean id="lisi" parent="abstractPeople">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
<property name="dog" ref="dog"></property>
</bean>

二、bean作用范围:

默认是单例的:

<bean id="dog" class="com.java1234.entity.Dog" scope="singleton">
  <property name="name" value="jack"></property>
</bean>

多例的配置:

<bean id="dog" class="com.java1234.entity.Dog" scope="prototype">
  <property name="name" value="jack"></property>
</bean>

峰Spring4学习(5)bean之间的关系和bean的作用范围的更多相关文章

  1. Spring初学之bean之间的关系和bean的作用域

    一.bean之间的关系 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  2. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. Spring学习--Bean 之间的关系

    Bean 之间的关系:继承.依赖. Bean 继承: Spring 允许继承 bean 的配置 , 被继承的 bean 称为父 bean , 继承这个父 bean 的 bean 称为子 bean. 子 ...

  4. Spring Bean之间的关系

    bean之间的关系:继承和依赖继承bean的配置 Spring允许继承bean的配置,被继承的bean称为父bean,继承这个父bean的bean称为子bean 子bean从父bean中继承配置,包括 ...

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

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

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

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

  7. XML配置里的Bean自动装配与Bean之间的关系

    需要在<bean>的autowire属性里指定自动装配的模式 byType(根据类型自动装配) byName(根据名称自动装配) constructor(通过构造器自动装配) 名字须与属性 ...

  8. Spring_自动装配 & bean之间的关系 & bean的作用域

    1.自动装配 beans-autowire.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

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

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

随机推荐

  1. psycopg2 (python与postgresql)

    #快速导入数据到postgresql import pandas as pd import psycopg2 from io import StringIO def sql_to_df(): con= ...

  2. slice,Array.prototype.slice,Array.protyotype.slice.call

    slice 特点:基于当前数组中的一或多个项创建一个新数组.[原数组不会被修改] 返回结果:返回一个从开始到结束(不包括结束)选择的数组的一部分浅拷贝到一个新数组对象. 语法: arr.slice() ...

  3. 【数据库】MFC ODBC(一)

    一.动态创建ODBC数据源 ODBC API提供了动态创建数据源的函数SQLConfigDataSource.该函数的原型如下: BOOL SQLConfigDataSource ( HWND hwn ...

  4. spring集成redis——主从配置以及哨兵监控

    Redis主从模式配置: Redis的主从模式配置是非常简单的,首先我们需要有2个可运行的redis环境: master node : 192.168.56.101 8887 slave node: ...

  5. maven加载本地jar

    [问题描述] 由于对接公司外部产品,导致公司内网上的maven库中并不存在对应的SDK jar,因此,需要通过maven加载本地jar的方式来实现工程编译. [方法] 方法很简单, 1.在resour ...

  6. Android实现EditText的富文本编辑

    前言 本文是我之前写的这篇文章<Android图文混排-实现EditText图文混合插入上传>的升级版,除了在EditText实现了图片上传之外,还包含了视频上传.云盘文件上传.录音上传以 ...

  7. OC基础:block.字面量 分类: ios学习 OC 2015-06-22 19:08 155人阅读 评论(0) 收藏

    block 块语法,可以用block去保存一段代码,或者封装一段代码. block 实际是由c语言实现的,执行效率很高. block 实际借鉴了函数指针的语法. block,在多线程.异步任务,集合遍 ...

  8. Texas Instruments matrix-gui-2.0 hacking -- index.php

    <?php /* * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ * * * Redistrib ...

  9. ubunttu-sh: 1: pause: not found

    old code: //in ubuntu OS system("pause"); error discription: : pause: not found right code ...

  10. TF-IDF算法(1)—算法概述

    假设现在有一篇很长的文章,要从中提取出它的关键字,完全不人工干预,那么怎么做到呢?又有如如何判断两篇文章的相似性的这类问题,这是在数据挖掘,信息检索中经常遇到的问题,然而TF-IDF算法就可以解决.这 ...