Spring容器中的Bean
一,配置合作者的Bean
Bean设置的属性值是容器中的另一个Bean实力,使用<ref.../>元素,可制定一个bean属性,该属性用于指定容器中其他Bean实例的id属性
<bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<property name="axe">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<ref bean="steelAxe"/>
</property>
</bean>
也可以简写成
<bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<property name="axe" ref="steelAxe"/>
</bean>
二,使用自动装配注入合作者的Bean
1,自动装配可以通<beans.../>的default-autowire指定,该元素对所有的<bean.../>起作用
2,也可以通过<bean>的autowire指定
default-autowire,autowire可接受如下属性值
no:默认此
byName
<!-- 指定使用byName,根据setter的方法名与Bean的id进行匹配 -->
<bean id="chinese" class="org.com.service.impl.Chinese" autowire="byName">
</bean>
<bean id="gunDog" class="org.com.service.impl.GunDog">
<property name="name" value="wanggang"></property>
</bean>
setter方法名为setGunDog(),spring容器会找id为gunDog的Bean,若找到了,该Bean就会调用setGunDog方法的参数
public void setGunDog(Dog dog){
this.dog=dog;
}
byType:根据参数类型,进行匹配,不多做例子,估计用不到
三,注入集合值
使用集合元素标签<list../><set../><map../><props../>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 资源国际化 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 驱动spring调用messageSource Bean的setBasenames()的方法,改方法需要一个参数组 -->
<property name="basenames">
<list>
<value>message</value>
<!-- 如果有多个资源文件,全部列在这儿 -->
</list>
</property>
</bean>
<bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<property name="axe">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<ref bean="steelAxe"/>
</property>
</bean> <bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<property name="axe" ref="steelAxe"/>
</bean> <!-- 指定使用byName,根据setter的方法名与Bean的id进行匹配 -->
<bean id="chinese" class="org.com.service.impl.Chinese" autowire="byName">
</bean>
<bean id="gunDog" class="org.com.service.impl.GunDog">
<property name="name" value="wanggang"></property>
</bean> <bean id="chinese" class="org.com.service.impl.Chinese">
<!-- 为调用setSchools()方法配置List集合作为参数值 和数组一样 -->
<property name="schools">
<list>
<!-- 每个value,ref,bean..都配置一个List元素 -->
<value>小学</value>
<value>中学</value>
<value>大学</value>
</list>
</property>
<property name="scores">
<!-- 为调用setScores()方法配置Map集合作为参数值 -->
<map>
<entry key="语文" value="80"/>
<entry key="数学" value="80"/>
<entry key="外语" value="80"/>
</map>
</property>
</bean>
</beans>
四,组合属性
除了最后一个属性可以为NULL,其他都不行
<bean id="" class="">
<property name="person.name" value="zhangliang"/>相当于执行getPerson().setName(String name);
</bean>
Spring容器中的Bean的更多相关文章
- 从Spring容器中获取Bean。ApplicationContextAware
引言:我们从几个方面有逻辑的讲述如何从Spring容器中获取Bean.(新手勿喷) 1.我们的目的是什么? 2.方法是什么(可变的细节)? 3.方法的原理是什么(不变的本质)? 1.我们的目的是什么? ...
- 在listener或者工具中使用spring容器中的bean实例
在项目中经常遇见需要在Listener中或者工具中使用Spring容器中的bean实例,由于bean不能在stataic的类中使用. 介绍一种方式: public class SpringTool { ...
- 7 -- Spring的基本用法 -- 5... Spring容器中的Bean;容器中Bean的作用域;配置依赖;
7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- SpringBoot 之 普通类获取Spring容器中的bean
[十]SpringBoot 之 普通类获取Spring容器中的bean 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...
- 获取Spring容器中的Bean协助调试
在使用Spring进行开发时,有时调bug真的是很伤脑筋的一件事,我们可以通过自定义一个监听器来获取Spring容器中的Bean实例来协助我们调试. 第一步:编写自定义监听器 /** * 监听serv ...
- 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!
写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...
- 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?
写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...
- 获取Spring容器中的Bean
摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...
随机推荐
- zepto源码--$.map,$.each,$.grep--学习笔记
从相对比较简单的说起: 1.$.grep 获取一个新数组,新数组只包含回调函数中返回 true 的数组项. 调用javascript中数组原生函数filter,对elements进行过滤,保留回调函 ...
- 学习GitHub
学习GitHub: GitHub教程:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b00 ...
- 在CentOS6.5上安装Tomcat6
Tomcat安装一向方便,linux的比win的更是这样,基本就是拷贝,类似于win中备受青睐的绿色软件,下面只是记录一下过程. 1.从 http://mirrors.cnnic.cn/apache/ ...
- css中的盒子模型
css中的盒子模型 css中的盒子模型,有两种,一种是“标准 W3C 盒子模型”,另外一种是IE盒子模型. 1.w3c盒子模型 从图中可以看出:w3c盒子模型的范围包括了:margin,borde ...
- [代码片段]YEAH!连通域标记和计数
//标记的连通域存储在buff[]里 //返回值为连通域个数 int LinkBlob(unsigned char **imagedata,unsigned char buff[], int heig ...
- empty()函数经典详解
<?php /** * 当var不存在,返回TRUE; * 当var存在,并且是一个非空非零的值(真值)时返回 FALSE 否则返回 TRUE . * 以下的东西被认为是空的: * * 1.&q ...
- 使用openvswitch实现跨主机docker容器互联
安装openvswitch的步骤请参考上一篇文章:http://www.cnblogs.com/xkops/p/5568167.html 环境:192.168.3.201 node1192.168.3 ...
- 详解MySQL---DDL语句、DML语句与DCL语句
背景:近几年,开源数据库逐渐流行起来.由于具有免费使用.配置简单.稳定性好.性能优良等优点,开源数据库在中低端应用上占据了很大的市场份额,而 MySQL 正是开源数据库中的杰出代表.MySQL 数据库 ...
- 3D Modeling using GDI+
https://code.msdn.microsoft.com/3D-Modeling-using-GDI-b93937b9 Introduction Most of us use OpenGL/ D ...
- 文本属性Attributes
1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFontOfSize:_fontS ...