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 ...
随机推荐
- JMeter学习-011-JMeter 后置处理器实例之 - 正则表达式提取器(三)多参数获取进阶引用篇
前两篇文章分表讲述了 后置处理器 - 正则表达式提取器概述及简单实例.多参数获取,相应博文敬请参阅 简单实例.多参数获取. 此文主要讲述如何引用正则表达式提取器获取的数据信息.其实,正则表达式提取器获 ...
- Java学习-018-EXCEL 文件写入实例源代码
众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...
- 将option添加到select框
var select=document.createElement("select"); select.setAttribute("class","f ...
- XLST
xlst转换 // 读入源请求和mapping配置 StreamSource xmlSource = new StreamSource(new InputStreamReader(new ByteAr ...
- 移动端a链接点击时取出背景色及边框
a{blr:expression(this.onFocus=this.blur())} :focus{outline:0;} /*去掉a标签的虚线框,避免出现奇怪的选中区域*/*{-webkit-ta ...
- Base64解码中文部分中文乱码的原因
参考这篇博客 http://blog.sina.com.cn/s/blog_4eb5ae750101cq16.html 需要做的就是 filename=filename.replace(" ...
- 微信公众平台开发(68)苹果IMEI查询
微信公众平台开发 苹果IMEI查询 苹果序列号查询 iPhone/iPad/iPod/Mac 作者:方倍工作室 地址:http://www.cnblogs.com/txw1958/p/weixin69 ...
- 05-雷海林-mysql备份原理与在TDSQL中的实践
05-雷海林-mysql备份原理与在TDSQL中的实践 下载地址: http://files.cnblogs.com/files/MYSQLZOUQI/05-%E9%9B%B7%E6%B5%B7%E6 ...
- Android系统自带样式(@android:style/)
在AndroidManifest.xml文件的activity中配置 1.android:theme="@android:style/Theme" 默认状态,即如果theme这里不 ...
- python模块(os)
os模块 os模块提供了许多与操作系统交互的接口 os.getcwd() -> str # 返回当前路径, 相当于pwd os.chdir("dirname") -> ...