峰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 ...
随机推荐
- Python 数据类型--集合(set)
一.集合(set) 集合也是一种数据类型,一个类似列表的,无序的,不重复的.它主要有两大作用 1.把一个列表变为集合,就自动去重了,不需要写额外的代码 2.关系测试,测试两组数据之间的交际.差集.并集 ...
- strtus2 文件上传
struts2和spring mvc上传都是用 common-fileupload来实现 1.struts2上的方式需要在对应的Action,加上如下的属性以及get/set方法 private Fi ...
- CreateFile DeviceIoControl dwIoControlCode——应用程序与驱动程序通信
在“进程内存管理器中”的一个Ring0,Ring3层通信问题,之前也见过这样的代码,这次拆分出来详细总结一下. 先通过CreateFile函数得到设备句柄,CreateFile函数原型: HANDLE ...
- 如何控制jquery ui弹窗下方按钮水平居中
1.问题背景 一般情况下,jquery ui弹窗下方的按钮是居右的,但是有时系统为了达到美观统一,需要将按钮放在中间 2.问题原因 <!DOCTYPE html> <html> ...
- Kaggle新手入门之路(完结)
学完了Coursera上Andrew Ng的Machine Learning后,迫不及待地想去参加一场Kaggle的比赛,却发现从理论到实践的转变实在是太困难了,在此记录学习过程. 一:安装Anaco ...
- Linux命令行操作进程
参见 12个进程管理命令介绍 Linux如何查看进程.杀死进程.启动进程等常用命令
- 一步步入门log4cpp
前言 项目实现过程中,需要检查.查找或者调试程序bug等,此时程序日志则较为清晰地展现代码的运行过程.目前接触到的方法有打印消息到控制台,将重要信息输出到某个文件比如txt文件,或者直接使用日志库. ...
- TJU Problem 1065 Factorial
注意数据范围,十位数以上就可以考虑long long 了,断点调试也十分重要. 原题: 1065. Factorial Time Limit: 1.0 Seconds Memory Limit ...
- CodeForces - 325E:The Red Button (哈密尔顿 转 欧拉回路)
Piegirl found the red button. You have one last chance to change the inevitable end. The circuit und ...
- BZOJ5091: [Lydsy1711月赛]摘苹果【期望DP】
Description 小Q的工作是采摘花园里的苹果.在花园中有n棵苹果树以及m条双向道路,苹果树编号依次为1到n,每条道路的两 端连接着两棵不同的苹果树.假设第i棵苹果树连接着d_i条道路.小Q将会 ...