Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系
XML配置里的Bean自动装配
Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName,constructor

举例:
常规用法:
Member.java
package com.dx.spring.bean.autowire;
public class Member {
private String name;
private int age;
private Address address;
private Work work;
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Work getWork() {
return work;
}
public void setWork(Work work) {
this.work = work;
}
@Override
public String toString() {
return "Member [name=" + name + ", age=" + age + ", address=" + address + ", work=" + work + "]";
}
}
Address.java
package com.dx.spring.bean.autowire;
public class Address {
private String city;
private String area;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
@Override
public String toString() {
return "Address [city=" + city + ", area=" + area + "]";
}
}
Work.java
package com.dx.spring.bean.autowire;
public class Work {
private String company;
private int money;
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "Work [company=" + company + ", money=" + money + "]";
}
}
bean-autowire.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" 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.dx.spring.bean.autowire.Address">
<property name="city" value="beijing"></property>
<property name="area" value="huilongguan"></property>
</bean> <bean id="work" class="com.dx.spring.bean.autowire.Work">
<property name="company" value="baidu"></property>
<property name="money" value="80000"></property>
</bean> <bean id="member" class="com.dx.spring.bean.autowire.Member"
p:name="Rose" p:age="27" p:address-ref="address" p:work-ref="work"></bean> </beans>
Main.java
package com.dx.spring.bean.autowire; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("bean-autowire.xml");
Member member = (Member) cxt.getBean("member");
System.out.println(member);
}
}
测试打印结果:
Member [name=Rose, age=27, address=Address [city=beijing, area=huilongguan], work=Work [company=baidu, money=80000]]
1)byName(根据名称自动装配):
必须将目标Bean的名称和属性名设置的完成相同。
修改常规用法中的bean-autowire.xml:
<bean id="member" class="com.dx.spring.bean.autowire.Member"
p:name="Rose" p:age="27" autowire="byName"></bean>
此时,如果修改work bean的名字为work2
<bean id="work2" class="com.dx.spring.bean.autowire.Work">
<property name="company" value="baidu"></property>
<property name="money" value="80000"></property>
</bean>
则Main.java调用时,无法匹配上work属性:
Member [name=Rose, age=27, address=Address [city=beijing, area=huilongguan], work=null]
2)byType(根据类型自动装配):
若IOC容器中有多个与目标Bean类型一致的Bean,在这种情况下,Spring将无法判定哪个Bean最合适该属性,所以不能执行自动装配。
<bean id="member" class="com.dx.spring.bean.autowire.Member"
p:name="Rose" p:age="27" autowire="byType"></bean>
此时如果在bean-autowire.xml中包含多个Work的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" class="com.dx.spring.bean.autowire.Address">
<property name="city" value="beijing"></property>
<property name="area" value="huilongguan"></property>
</bean> <bean id="work" class="com.dx.spring.bean.autowire.Work">
<property name="company" value="baidu"></property>
<property name="money" value="80000"></property>
</bean>
<bean id="work3" class="com.dx.spring.bean.autowire.Work">
<property name="company" value="jd"></property>
<property name="money" value="90000"></property>
</bean> <bean id="member" class="com.dx.spring.bean.autowire.Member"
p:name="Rose" p:age="27" autowire="byType"></bean> </beans>
此时运行时,将会抛出异常:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'member' defined in class path resource [bean-autowire.xml]:
Unsatisfied dependency expressed through bean property 'work';
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'com.dx.spring.bean.autowire.Work' available: expected single matching bean but found 2: work,work3
3)constructor(通过构造器自动装配):
当Bean中存在多个构造器时,此种自动装配方式将会很复杂,不推荐使用。
XML配置里的Bean自动装配缺点
1)在bean配置文件里设置autowire属性进行自动装配将会装配bean所有属性。然而,若只希望装配个别属性是,autowire属性就不够灵活了。
2)autowire属性要么根据类型自动装配,要么根据名称自动装配,不能两者兼而有之。
3)一般情况下,在实际项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力。
bean之间的关系:继承;依赖。
bean之间的关系:继承
1)Spring允许继承bean的配置,被集成的bean称为父bean。继承这个父bean的bean称为子bean。
2)子bean从父bean中继承配置,包括bean的属性配置
3)父bean可以作为配置模版,也可以作为bean实例。若只想把父bean作为模版,可以设置<bean>的abstract属性为true,这样Spring将不会实例化这个bean。
4)并不是<bean>元素里的所有属性都会被继承。比如:autowire,abstract等。
5)也可以忽略父bean的class属性,让子bean指定自己的类,而共享相同的属性配置,但此时abstract必须设为true。
常规示例:有两个address bean,此时用法如下:
bean-relation-dependence.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" 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.dx.spring.beans.relation_dependence.Address"
p:city="Beijing" p:street="HuiLongGuan">
</bean>
<bean id="address2" class="com.dx.spring.beans.relation_dependence.Address"
p:city="Beijing" p:street="ZhongGuanCun">
</bean>
</beans>
Adress.java
package com.dx.spring.beans.relation_dependence;
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 + "]";
}
}
Client.java
package com.dx.spring.beans.relation_dependence; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("bean-relation-dependence.xml");
Address address = (Address) cxt.getBean("address");
System.out.println(address); Address address2 = (Address) cxt.getBean("address2");
System.out.println(address2);
}
}
a)实际上address2是可以通过继承address bean的,修改bean-relation-dependence.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" 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.dx.spring.beans.relation_dependence.Address"
p:city="Beijing" p:street="HuiLongGuan">
</bean> <bean id="address2" p:street="ZhongGuanCun" parent="address">
</bean> </beans>
b)可以把父类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="address" p:city="Beijing" p:street="HuiLongGuan" class="com.dx.spring.beans.relation_dependence.Address" abstract="true">
</bean> <bean id="address2" p:street="ZhongGuanCun"
class="com.dx.spring.beans.relation_dependence.Address" parent="address">
</bean> </beans>
此时address这个bean是不能够被实例化的,否则会抛出异常。
package com.dx.spring.beans.relation_dependence; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("bean-relation-dependence.xml");
// 当把address bean的属性设置abstract="true"时,该bean不能被实例化,否则会抛出异常。
// Address address = (Address) cxt.getBean("address");
// System.out.println(address); Address address2 = (Address) cxt.getBean("address2");
System.out.println(address2);
}
}
c)也可以把父类bean配置为一个无class属性的模版(抽象)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="Beijing" p:street="HuiLongGuan" abstract="true">
</bean>
<bean id="address2" p:street="ZhongGuanCun"
class="com.dx.spring.beans.relation_dependence.Address" parent="address">
</bean>
</beans>
备注:如果一个bean的class属性没有被指定,则该bean必须为一个抽象bean。
bean之间的关系:依赖
1)spring允许用户通过depends-on属性设定bean前置依赖的bean,前置依赖的bean会在本bean实例化之前创建好。
2)如果前置依赖于多个bean,则可以通过逗号、空格的方式配置bean的名称
bean-relation-dependence.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" 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="Beijing" p:street="HuiLongGuan"
abstract="true">
</bean> <bean id="address2" p:street="ZhongGuanCun"
class="com.dx.spring.beans.relation_dependence.Address" parent="address">
</bean> <bean id="car" class="com.dx.spring.beans.relation_dependence.Car"
p:brand="Audi" p:corp="Shanghai" p:price="400000" p:maxSpeed="260"></bean> <bean id="person" class="com.dx.spring.beans.relation_dependence.Person"
p:name="RoseGun" p:age="35" p:address-ref="address2" depends-on="car"></bean>
</beans>
此时,如果depends-on的car不存在,则会抛出异常。
Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系的更多相关文章
- spring实战二之Bean的自动装配(非注解方式)
Bean的自动装配 自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bea ...
- bean的自动装配,使用注解开发,使用java的方式配置Spring
bean的自动装配 自动装配是Spring满足bean依赖一种方式! Spring会在上下文中自动寻找,并自动给bean装配属性! 在Spring中有三种装配的方式 在xml中显示的配置 在java中 ...
- Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配
一.Spring IOC 容器支持自动装配 Bean,所谓自动装配是指,不需要通过 <property> 或 <constructor-arg> 为 Bean 的属性注入值的过 ...
- spring实战四之Bean的自动装配(注解方式)
使用注解装配: 从spring2.5开始,Spring启用了使用注解自动装配Bean的属性,使用注解方式自动装配与在XML中使用 autowire 属性自动装配并没有太大区别,但是使用注解方式允许更细 ...
- Spring基础09——Bean的自动装配
1.XML配置的Bean自动装配 SpringIOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,而不需要手工去指定要装配的Bean,a ...
- Spring(三):bean的自动装配
Bean的自动装配 自动装配是Spring满足bean依赖的一种方式. Spring会在上下文中自动寻找,并自动给bean装配属性 Spring中三种装配方式 在xml中显式的配置. 在java中显式 ...
- Spring学习03(Bean的自动装配)
6.Bean的自动装配 6.1 自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring中bean的三种装配机 ...
- spring bean autowire自动装配
转自:http://blog.csdn.net/xiao_jun_0820/article/details/7233139 autowire="byName"会自动装配属性与Bea ...
- Spring学习--xml 中 Bean 的自动装配
Spring IOC 容器可以自动装配 Bean. 只要在 <bean> 的 autowire 属性里指定自动装配的模式. byName(根据名称自动装配):必须将目标 Bean 的名称和 ...
随机推荐
- Fiddler 实现手机的抓包
Fiddler是我最喜爱的工具,几乎每天都用, 我已经用了8年了. 至今我也只学会其中大概50%的功能. Fiddler绝对称得上是"神器", 任何一个搞IT的人都得着的. 小弟我 ...
- WMAppManifest.xml
大家在编写Windows phone的程序的时候可能并没有关注WMAppManifest.xml,其实这个档案是记录了应用程式的相关属性描述,以及定义应用程式的功能性的..所以还是相当重要的一个文档, ...
- 利用Fiddler和Wireshark解密SSL加密流量
原文地址:http://kelvinh.github.io/blog/2014/01/12/decrypt-ssl-using-fiddler-and-wireshark/ Fiddler是一个著名的 ...
- android4.0 中关于内外置sd卡的获取及读写权限问题
from://http://blog.chinaunix.net/uid-26727976-id-3146895.html 在2.x的版本中,在manifest中配置的权限android.permis ...
- python测试开发django-24.表单提交之get请求
前言 通常我们需要在html页面上输入框里面输入数据,比如登录的时候,输入账号和密码,点提交按钮. 从html把数据提交到服务端,服务端接收数据后判断提交的数据,然后做出对应的响应,这么一整个流程就是 ...
- NoSQL现状
经过了至少4年的激烈争论,现在是对NoSQL的现状做一个阶段性结论的时候了.围绕着NoSQL发生了如此之多的事情,以至于很难对其作出一个简单概括,也很难判断它达到了什么目标以及在什么方面没有达到预期. ...
- 用wifi来调试应用程序
我们一般调试程序都是用的adb,这个adb其实是可以连接到某个端口的,只要我们的手机和电脑处于同一wifi环境下(你可以用电脑分出来的wifi),手机也接入同一端口就可以实现程序的无线调试了,终于可以 ...
- 详细解读Android中的搜索框(一)—— 简单小例子
这次开的是一个讲解SearchView的栏目,第一篇主要是给一个小例子,让大家对这个搜索视图有一个了解,之后再分布细化来说. 目标: 我们先来定个目标,我们通过搜索框来输入要搜索的联系人名字,输入的时 ...
- [Android Pro] 开发一流Android SDK
cp from : https://blog.csdn.net/dd864140130/article/details/53558011 本篇文章已授权微信公众号 guolin_blog (郭霖)独家 ...
- Linux文本行倒序排列6种方法
Linux文本行倒序排列6种方法 1. 命令方法: nl filename | sort -nr | cut -f2 这个方法很unix风格,使用多命令组合完成某种功能是典型的unix特点 单命令: ...