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 ...
随机推荐
- Largest Number || LeetCode
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 1000 int cm ...
- LINQ之select方法选择多个字段
单个字段: var list1 = list.Select(field1 => field1.CouponID).ToList(); 多个字段: var list1 = list.Select( ...
- CodeMIrror 简单使用
代码高亮是程序员的刚需,不管是在笔记类,论坛类,博客类web网站中,都对代码高亮提出要求,不高亮的代码阅读体验很差,codeMirror是一个前端代码高亮库,使用方便. codeMirror可以直接在 ...
- android studio 0.8.11 如何导入Library(新手向)
网上下了个ViewPagerIndicator的库,下载下来是个zip包,解压开来,里面有一个library和一个sample文件夹,还有一些其他的配置文件. 其中library就是我们新项目中要引入 ...
- action script 3如何检测播放器域
检测播放器域 用户在上面观看媒体内容的网页的 URL 和域并非始终随时可用.如果托管网站允许,您可使用 ExternalInterface 类获取确切 URL.尽管如此,允许第三方视频播放器的一些 ...
- iOS腾讯百度面试题
- iOS8 推送注册方式改变的问题
不久之后iPhone 6/6 plus就会在国内如雨后春笋般遍地开花了.iOS 8早已现行一步,不过有的开发者也注意到了在iOS 8上推送通知的注册方式有所变化,报错提示为: 1 registerFo ...
- C语言:十进制进制转换为其他进制(思想:查表法)
// // main.c // Hex conversion // // Created by ma c on 15/7/22. // Copyright (c) 2015年 bjsxt. A ...
- 细聊分布式ID生成方法
细聊分布式ID生成方法 https://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=403837240&idx=1&sn=ae9 ...
- ionic 上拉加载更多&瀑布流加载&滚动到底部加载更多 主意事项
首先下拉刷新的代码是这样的,标红的地方为关键代码 <html> <head> <meta charset="utf-8"> <meta n ...