峰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 ...
随机推荐
- Redis (一) 概念安装
一.阿里云安装Redis 1.安装Redis yum -y install redis 2.启动Redis service redis start 或者(推荐使用) systemctl start ...
- ehlib ado 删除选中记录 的方法
procedure TForm1.Button1Click(Sender: TObject); var I: Integer; begin do begin DBGridEh1.DataSource. ...
- python笔记02:列表与元素
本章将引入一个新的概念:数据结构.数据结构是通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合.这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在python中,最基本的数据结构是序 ...
- 记python3 UnicodeEncodeError: 'latin-1' codec... 报错
python3用cx_Oracle查询oracle数据库并打印输出,在windows上执行没问题,打算放suse上跑的时候就遇到了打印中文UnicodeEncodeError: 'latin-1' c ...
- 怎样更新PE内的工具
准备工作:1. UltraISO - 下载:http://yunpan.cn/Q5XuHwG4ydv85 (访问密码:6263) 2. 7-zip - 下载:http://yunpan.c ...
- Linux C:access()时间条件竞争漏洞
access()函数用来检查调用进程是否可以对指定的文件执行某种操作. ================================================================ ...
- ThinkPad 复刻计划 ThinkPad Time Machine
在快节奏的高科技市场中,针对性的进化 ThinkPad 的设计几乎是闻所未闻的.在汽车行业,保时捷无疑干的不错,但我不认为有任何其他的电脑公司可以顶住压力,坚持自己的初心这么久.没有任何一个竞争对手可 ...
- 深入理解uwsgi和gunicorn网络模型
前言: 去年10月份建了一个python技术群,到现在为止人数已经涨到700人了.最一开始我经常在群里回应大家的问题,不管是简单还是困难的,我都会根据自己的经验来交流. 让人新奇的是一些初学者关注最多 ...
- 在本地设置 http-proxy 代理 (前后端分离)
1. 利用package.json 安装nodejs,创建package.json文件:内容如下 "dependencies": { "http-proxy": ...
- vue2.0分页组件,
pagination.vue <!-- 表格分页组件 --> <template> <nav class="boot-nav"> <ul ...