Spring学习--Bean 之间的关系
Bean 之间的关系:继承、依赖。
Bean 继承:
- Spring 允许继承 bean 的配置 , 被继承的 bean 称为父 bean , 继承这个父 bean 的 bean 称为子 bean。
- 子 bean 从父 bean 中继承配置 , 包括 bean 的属性配置。
- 子 bean 也可以覆盖从父 bean 继承过来的配置。
- 父 bean 可以作为配置模板 , 也可以作为 bean 实例。若只想把父 bean 作为模板 , 可以设置 <bean> 的 abstract 属性为 true , 这样 IOC容器将不会实例这个 bean。
- 并不是 <bean> 元素里的所有属性都会被继承。比如: autowire , abstract 等。
- 父 bean 的 class 属性可以忽略 , 让子 bean 指定自己的类 , 而共享相同的属性配置。但此时 abstract 必须设为 true。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.itdoc.spring.extendsinfo.Address" p:city="DaLian" p:street="ShaHeKou"/> <bean id="address1" class="com.itdoc.spring.extendsinfo.Address" p:street="GanJingZi" parent="address"/> </beans>
package com.itdoc.spring.extendsinfo; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-01 20:48
*/
public class Address { private String city; private String street; public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} public String getStreet() {
return street;
} public void setStreet(String street) {
this.street = street;
} @Override
public String toString() {
return "Address{" +
"city='" + city + '\'' +
", street='" + street + '\'' +
'}';
}
}
package com.itdoc.spring.extendsinfo; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-01 20:55
*/
public class Main { public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("beans_extends.xml");
Address address = (Address) app.getBean("address");
System.out.println(address); System.out.println("-------------------");
address = (Address) app.getBean("address1");
System.out.println(address); }
}
控制台输出:
|
Address{city='DaLian', street='ShaHeKou'} |
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.itdoc.spring.extendsinfo.Address" p:city="DaLian" p:street="ShaHeKou" abstract="true"/> <bean id="address1" class="com.itdoc.spring.extendsinfo.Address" p:street="GanJingZi" parent="address"/> </beans>
控制台输出:
|
Exception in thread "main" org.springframework.beans.factory.BeanIsAbstractException: Error creating bean with name 'address': Bean definition is abstract |
原因:address 成为了模板 bean , 不可以实例化。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" p:city="DaLian" p:street="ShaHeKou"/> <bean id="address1" class="com.itdoc.spring.extendsinfo.Address" p:street="GanJingZi" parent="address"/> </beans>
package com.itdoc.spring.extendsinfo; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-01 20:55
*/
public class Main { public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("beans_extends.xml");
Address address = (Address) app.getBean("address1");
System.out.println(address); }
}
控制台输出:
|
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'address' defined in class path resource [beans_extends.xml]: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition |
原因:父 bean 中的 class 属性忽略 , 该 bean 必须是 abstract="true" 的。
Bean 依赖:
- Spring 允许用户通过 depends-on 属性设定 bean 前置依赖的 bean , 前置依赖的 bean 会在本 bean 实例化前创建好。
- 如果前置依赖于多个 bean , 则可以通过逗号或空格的方式配置 bean 的名称。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.itdoc.spring.extendsinfo.Person" p:name="华崽儿" p:car-ref="car" p:address-ref="address"/>
<bean id="address" class="com.itdoc.spring.extendsinfo.Address" p:city="DaLian" p:street="ShaHeKou"/>
<bean id="car" class="com.itdoc.spring.beans.Car" p:brand="8手奥拓" p:price="3000" p:maxSpeed="120"/> </beans>
package com.itdoc.spring.extendsinfo; import com.itdoc.spring.beans.Car; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-01 20:47
*/
public class Person { private String name; private Address address; private Car car; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Address getAddress() {
return address;
} public void setAddress(Address address) {
this.address = address;
} public Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
} public Person() {
System.out.println("I am Person Constructor...");
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", address=" + address +
", car=" + car +
'}';
}
}
package com.itdoc.spring.extendsinfo; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-01 20:48
*/
public class Address { private String city; private String street; public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} public String getStreet() {
return street;
} public void setStreet(String street) {
this.street = street;
} public Address() {
System.out.println("I am Address Constructor...");
} @Override
public String toString() {
return "Address{" +
"city='" + city + '\'' +
", street='" + street + '\'' +
'}';
}
}
package com.itdoc.spring.beans; /**
* 集合中的对象
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-02-28 19:56
*/
public class Car { private String brand; private double price; private int maxSpeed; public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public int getMaxSpeed() {
return maxSpeed;
} public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
} public Car() {
System.out.println("I am Car Constructor...");
} @Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
", maxSpeed=" + maxSpeed +
'}';
}
}
package com.itdoc.spring.extendsinfo; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-01 20:55
*/
public class Main { public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("beans_extends.xml");
Person person = (Person) app.getBean("person");
System.out.println(person); }
}
控制台输出:
|
I am Person Constructor... |
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.itdoc.spring.extendsinfo.Person" p:name="华崽儿" p:car-ref="car" p:address-ref="address" depends-on="car, address"/> <bean id="address" class="com.itdoc.spring.extendsinfo.Address" p:city="DaLian" p:street="ShaHeKou"/> <bean id="car" class="com.itdoc.spring.beans.Car" p:brand="8手奥拓" p:price="3000" p:maxSpeed="120"/> </beans>
控制台输出:
|
I am Car Constructor... |
根据控制台输出我们不难看出 bean 的初始化顺序发生了改变 , 前置依赖的 bean 会在本 bean 实例化前创建好。
Spring学习--Bean 之间的关系的更多相关文章
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring 学习笔记(五)—— Bean之间的关系、作用域、自动装配
继承 Spring提供了配置信息的继承机制,可以通过为<bean>元素指定parent值重用已有的<bean>元素的配置信息. <?xml version="1 ...
- 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, ...
- 峰Spring4学习(5)bean之间的关系和bean的作用范围
一.bean之间的关系: 1)继承: People.java实体类: package com.cy.entity; public class People { private int id; priv ...
- 3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/
1.自动装配/手动装配 xml配置文件里的bean自动装配 Spring IOC 容器里可以自动的装配Bean,需要做的仅仅是在<bean>的autowire属性里面指定自动装配模式 -& ...
- Spring初学之bean之间的关系和bean的作用域
一.bean之间的关系 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...
- Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...
- Spring_自动装配 & bean之间的关系 & bean的作用域
1.自动装配 beans-autowire.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
随机推荐
- $.ajax()各方法详解(转)
jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...
- 隐藏WPF ToolBar 左侧的移动虚线和右侧的小箭头
原文:隐藏WPF ToolBar 左侧的移动虚线和右侧的小箭头 上面的图是两个工具栏的链接处. 去除蓝色部分的方法是 设置工具栏的ToolBarTray.IsLocked附加选项为True ...
- No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser").警告解决方法
在使用BeautifulSoup库时出现该警告,虽然不影响正常运行,但强迫症不能忍啊!! 详细警告信息如下: UserWarning: No parser was explicitly specifi ...
- Word中使用宏调整表格
Dim i As Integer For i = 1 To Selection.Tables.Count Selection.Tables(i).Columns(9).Delete Selecti ...
- 手动监控Windows端口
转载自http://blog.51cto.com/ywzhou/1579917 1.监控端口的几个主要Keys: net.tcp.listen[port] Checks if this port ...
- python基础篇 07set集合 深浅拷贝
本节主要内容:1. 基础数据类型补充2. set集合3. 深浅拷⻉ " ".join方法 循环删除列表中的内容: 错误的 原因:在for循环中,循环到第一个,然后删除,删除之 ...
- Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】
源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...
- 前端开发神器Sublime Text2/3之安装使用(windows7/Mac)
一,到官方网站下载神器 地址:http://www.sublimetext.com/3 Sublime Text 3 配置解释(默认){// 设置主题文件“color_scheme”: “Packag ...
- Python标准模块logging
http://blog.csdn.net/fxjtoday/article/details/6307285 开发Python, 一直以来都是使用自己编写的logging模块. 比较土...... 今天 ...
- Solr的搭建和部署
1.概述 简介 Solr,全称Search On Lucene Replication.一个开源的搜索服务器,对外提供类似于WebService的API接口. 用户可以通过http请求,向搜索引擎服务 ...