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 之间 ...
随机推荐
- CFF前端沙龙总结
一. -OOCSS + Sass ——大漠 1. OOCSS 结构<=>皮肤 分离 容器<=>内容 分离 2. Sass 工具.处理器 SCSS(CSS风格)<=> ...
- ganglia对于tomcat进程的res内存监控扩展
ganglia是采用yum的安装,因此安装相关内容路径可能不同,但是不影响插件的扩展编写: 本次介绍的扩展是采用python脚本进行扩展,因此监控节点上需要安装python的相关插件: sudo yu ...
- Leap Motion 开发笔记
Leap Motion 体系架构 Leap Motion支持所有主流操作系统,leap motion在Windows系统下是作为一个服务运行的,在Mac和Linux是后台守护进程.软件通过USB线连接 ...
- CCScrollView 实现帮助界面、关卡选择
本文出自[无间落叶]:http://blog.leafsoar.com/archives/2013/07-27.html 本文介绍了 CCScrollView 来编写帮助界面和关卡选择界面的方法,在编 ...
- 学习Mongodb(一)
图片摘录自陈彦铭出品2012.5的<10天掌握MongDB> MongoDB的特点--->面向集合存储,易于存储对象类型的数据--->模式自由--->支持动态查询---& ...
- JQuery实现——黑客帝国代码雨效果
效果如你所见就是本页面上方那样的效果 实现方法来自一个印度小伙纸,学习完我也没总结一下,今儿个补上 如何实现,大家右键查看源码复制即可,不过学习的过程还是要总结总结. 下面通过另外两个小例子,一步一步 ...
- 【转】android:layout_gravity和android:gravity的区别
1.首先来看看android:layout_gravity和android:gravity的使用区别. android:gravity: 这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置 ...
- [Papers]NSE, $\p_3u$, Lebesgue space [Cao, DCDSA, 2010]
$$\bex \p_3\bbu\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=2,\quad \frac{27}{16}\leq q\le ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_labels(self, locator)
def get_selected_list_labels(self, locator): """Returns the visible labels of selecte ...
- mysql EF
使用 mysql-installer-community-5.6.26.0.msi visual studio 2013 update 4版 Install-Package EntityFramewo ...