峰Spring4学习(5)bean之间的关系和bean的作用范围
一、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的作用范围的更多相关文章
- Spring初学之bean之间的关系和bean的作用域
一.bean之间的关系 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring学习--Bean 之间的关系
Bean 之间的关系:继承.依赖. Bean 继承: Spring 允许继承 bean 的配置 , 被继承的 bean 称为父 bean , 继承这个父 bean 的 bean 称为子 bean. 子 ...
- Spring Bean之间的关系
bean之间的关系:继承和依赖继承bean的配置 Spring允许继承bean的配置,被继承的bean称为父bean,继承这个父bean的bean称为子bean 子bean从父bean中继承配置,包括 ...
- Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系
XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...
- 3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/
1.自动装配/手动装配 xml配置文件里的bean自动装配 Spring IOC 容器里可以自动的装配Bean,需要做的仅仅是在<bean>的autowire属性里面指定自动装配模式 -& ...
- XML配置里的Bean自动装配与Bean之间的关系
需要在<bean>的autowire属性里指定自动装配的模式 byType(根据类型自动装配) byName(根据名称自动装配) constructor(通过构造器自动装配) 名字须与属性 ...
- Spring_自动装配 & bean之间的关系 & bean的作用域
1.自动装配 beans-autowire.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
- Spring 学习笔记(五)—— Bean之间的关系、作用域、自动装配
继承 Spring提供了配置信息的继承机制,可以通过为<bean>元素指定parent值重用已有的<bean>元素的配置信息. <?xml version="1 ...
随机推荐
- postgresql的copy
https://www.postgresql.org/docs/current/static/sql-copy.html 一.Copy的基本语法 Copy的作用是复制数据在数据表和文件之间. Copy ...
- 使用Setup安装Windows8 RTM方法
1.下载Windows 8 RTM版. 2.解压ISO映像或加载虚拟光驱. 3.为了安装双系统,打开sources文件夹中的setup.exe才可. 4.打开后选择“在线获取安装程序的最新更新(推荐) ...
- 特征选择: 卡方检验、F 检验和互信息
特征选择是特征工程中的重要一环,其主要目的是从所有特征中选出相关特征 (relevant feature),或者说在不引起重要信息丢失的前提下去除掉无关特征 (irrelevant feature) ...
- SharePoint Visio Graphics Service-PowerShell
1. 配置托管服务账户 Set-SPVisioExternalData -VisioServiceApplication "Visio Graphics Service" –Una ...
- 《TensorFlow实战》读书笔记(完结)
1 TensorFlow基础 ---1.1TensorFlow概要 TensorFlow使用数据流图进行计算,一次编写,各处运行. ---1.2 TensorFlow编程模型简介 TensorFlow ...
- SMON进程、PMON进程、LGWR/ARCH
SMON 进程:system monitor instance monitor 系统监控.实例监控进程 说明及作用:在实例关闭时,会清理临时段,整理空闲空间free space; 实例非正常关闭后,启 ...
- Samsung_tiny4412(驱动笔记10)----mdev,bus,device,driver,platform
/*********************************************************************************** * * mdev,bus,de ...
- C#Delegate.Invoke、Delegate.BeginInvoke And Control.Invoke、Control.BeginInvoke
作者:EasonLeung 一.Delegate的Invoke.BeginInvoke 1.Delegate.Invoke (委托同步调用) a.委托的Invoke方法,在当前线程中执行委托. b.委 ...
- [LeetCode&Python] Problem 876. Middle of the Linked List
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...
- CodeForces - 1097D:Makoto and a Blackboard (积性)
Makoto has a big blackboard with a positive integer n written on it. He will perform the following a ...