一、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. QueryPerformanceFrequency

    var c1: int64; t1, t2: int64; QueryPerformanceFrequency(c1); QueryPerformanceCounter(t1); // GetSell ...

  2. Flash网页mp3播放器代码(3例)

    第一款:可以添加多首歌 代码如下:可以添加多首歌曲中间用 |  间隔符号隔开 <EMBED  height=20 type=application/x-shockwave-flash plugi ...

  3. Spring学习笔记之Container overview

    The Spring IoC container

  4. python笔记03:使用字符串

    3.1 基本字符串操作: 所有的标准序列操作(索引,分片,乘法,判断成员资格,求长度,取最小值,取最大值)对于字符串同样有效.但是,请记住:字符串都是不可变的 3.2 字符串格式化:精简版 字符串格式 ...

  5. 生成banner的网站

    http://patorjk.com/software/taag 例如: .__ .__ .__ .__ .___ | |__ ____ | | | | ______ _ _____________| ...

  6. Myecilpse web +tomcat 项目: JSP在mysql中创建表

    <%@ page language="java" import="java.util.*" import="com.mysql.jdbc.Dri ...

  7. Vue.js中用webpack合并打包多个组件并实现按需加载

    对于现在前端插件的频繁更新,所以多多少少要对组件化有点了解,下面这篇文章主要给大家介绍了在Vue.js中用webpack合并打包多个组件并实现按需加载的相关资料,需要的朋友可以参考下.   前言 随着 ...

  8. 详解基本TCP套接字函数

    以下讲解基本TCP套接字函数. 1.socket 函数   指定期望的通信协议类型.    #include <sys/types.h> /* See NOTES */ #include  ...

  9. HDU 1853

    http://acm.hdu.edu.cn/showproblem.php?pid=1853 和下题一模一样,求一个图环的并,此题的题干说的非常之裸露 http://www.cnblogs.com/x ...

  10. 从 Python 第三方进度条库 tqdm 谈起 (转载)

    原文地址: https://blog.ernest.me/post/python-progress-bar tqdm 最近一款新的进度条 tqdm 库比较热门,声称比老版的 python-progre ...