Spring入门(5)-自动装配Bean属性
Spring入门(5)-自动装配Bean属性
本文介绍如何装配Bean属性。
0. 目录
- ByName
- ByType
- constructor
- 默认自动装配
- 混合使用自动装配和显示装配
1. ByName
把与Bean的属性具有相同名字(或ID)的其他Bean自动装配到Bean对应的属性中。如果没有跟属性名称相匹配的Bean,则该属性不进行装配。
package com.chzhao.springtest;
public class PersonBll implements IPersonBll {
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public void show() {
System.out.println(this.person.getName());
}
}
<?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">
<bean name="person" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="byName">
</bean>
</beans>
2. ByType
把与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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="byType">
</bean>
</beans>
如上,如果设置为byType,Bean的名字随便设置了,不需要为person
3. constructor
把与Bean的构造器入参具有相同类型的其他Bean自动装配到Bean构造器的对应入参中。
package com.chzhao.springtest;
public class PersonBll implements IPersonBll {
public PersonBll(Person person) {
this.person = person;
}
private Person person;
public void show() {
System.out.println(this.person.getName());
}
}
<?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">
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value ="老王"></property>
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="constructor">
</bean>
</beans>
4. 默认自动装配
Spring可以通过设置default-autowire使所有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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="constructor">
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value ="老王"></property>
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll">
</bean>
</beans>
5. 混合使用自动装配和显示装配
即使对某个Bean的属性采用了自动装配,仍然可以通过显示装配配置某个属性。
package com.chzhao.springtest;
public class PersonBll implements IPersonBll {
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
private Person person;
public void show() {
System.out.println(this.person.getName());
}
}
<?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="byType"
>
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="laoli" class="com.chzhao.springtest.Person">
<property name="name" value="老李" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" >
<property name="person" ref="laoli"></property>
</bean>
</beans>
Spring入门(5)-自动装配Bean属性的更多相关文章
- Spring学习笔记--自动装配Bean属性
Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ...
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...
- Spring的自动装配Bean
spring的自动装配功能的定义:无须在Spring配置文件中描述javaBean之间的依赖关系(如配置<property>.<constructor-arg>).IOC容器会 ...
- Spring自动装配Bean的五种方式
在Spring中,支持 5 自动装配模式. no – 缺省情况下,自动配置是通过“ref”属性手动设定,在项目中最常用byName – 根据属性名称自动装配.如果一个bean的名称和其他bean属性的 ...
- 3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/
1.自动装配/手动装配 xml配置文件里的bean自动装配 Spring IOC 容器里可以自动的装配Bean,需要做的仅仅是在<bean>的autowire属性里面指定自动装配模式 -& ...
- IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置
1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...
- spring的自动装配Bean与自动检测Bean
spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring自动装配Bean
除了使用 XML 和 Annotation 的方式装配 Bean 以外,还有一种常用的装配方式——自动装配.自动装配就是指 Spring 容器可以自动装配(autowire)相互协作的 Bean 之间 ...
随机推荐
- BZOJ 2323 细胞(矩阵)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2323 题意: 题意过于复杂,我直接简化下.给出一个长度为n的数字串,只包含1到9,将数字 ...
- hdu 2372 El Dorado (dp)
题目链接 题意:给n个数字, 求有k个数字的上升子序列有多少种. 思路:d[i][j]表示 以第i个元素为 子序列的最后一个元素,长度为j的子序列 有多少种. 比赛的时候 光想着用组合数做了..... ...
- The dialect was not set. Set the property hibernate.dialect
The dialect was not set. Set the property hibernate.dialect load hibernate.cfg.xml 出 ...
- 启动PL/SQL Developer 报字符编码不一致错误 Database character set (AL32UTF8) and Client character set (ZHS16GBK) are different. Character set conversion may cause unexpected results. Note: you can set the client
今天写hibernate时候遇到一些异常 代码: 出现异常情况: 出现以上原因是Session关闭 如果不是使用的SessionFactory.getSession()来获得Session. 而是使用 ...
- 函数buf_page_init_for_read
/********************************************************************//** Function which inits a pag ...
- bzoj2245: [SDOI2011]工作安排
费用流. 这道题的模型比较明显,拆点也是很容易看出来的. #include<cstdio> #include<algorithm> #include<cstring> ...
- Firefox和Chrome浏览器导出书签
Chrome浏览器: 或者直接在地址栏中输入:“chrome://bookmarks/#1”也可以 Firefox浏览器:
- 浅析Java中HashMap的实现
概述 HashMap是一个散列表,是基于拉链法实现的.这个类继承了Map接口,Map接口提供了所有的哈希操作,比如set().put().remove()等,并且允许操作的键值对为null.HashM ...
- RPi 2B Raspbian system install
/***************************************************************************** * RPi 2B Raspbian系统安装 ...
- angular js 指令的数据传递 及作用域数据绑定
<div my-directive my-url="http://google.com" my-link-text="Click me to go to Googl ...