Spring----有关bean的配置
1.单例类的配置
如果我们想创建一个单例类的bean,只能会通过静态工厂来创建。
下图为一个单例类:
Stage并没有提供公开的构造方法,构造方法都是私有的,必须通过getInstance()方法获得已经实例化好的stage,所以我们每次调用返回的都是相同的实例。
在Spring的配置文件中,我们可以通过<bean> 的factory-method属性,调用一个指定的静态方法来创建实例。
2.bean的作用域
Spring中所有的bean默认都是单例。当容器分配一个Bean时,返回的都是同一个实例,单有时候我们需要每次调用都返回一个唯一的bean实例,可以通过配置bean的scope属性实现。
scope值默认为singleton 即单例。 修改为prototype 每次调用都会创建一个实例。
3.初始化和销毁Bean
当实例化一个bean时,可能需要执行一些初始化的操作,同样,当不在需要Bean,将其从容器中移除时,我们可能还需要按照顺序执行清楚工作。为了满足在初始化和销毁Bean时所需要做的特殊操作,Spring提供了Bean生命周期的钩子方法。
为Bean定义初始化和销毁操作,只需要使用init-method 和destrory-method参数来配置<bean>元素
例如:
在Stage类中添加初始化和销毁时所要执行的方法:
配置xml文件,在bean中设置init-method和destroy-method属性
4.注入Bean属性
注入简单值
将字符串作为参数注入。
<bean id =“XX” class="xxxxxxxxxxxxxxxxxxxxxx">
<property name="song" value="yesterday once more">
<beans>
将数字作为参数注入
<bean id =“XX” class="xxxxxxxxxxxxxxxxxxxxxx">
<property name="age" value="18">
<beans>
引用其他的bean
将A作为参数注入到其他bean中
<bean id =“A” class="xxxxxxxxxxxxxxxxxxxxxx">
<property name="age" value="18">
<beans>
<bean id =“XX” class="xxxxxxxxxxxxxxxxxxxxxx">
<property name="instrument" ref="A">
<beans>
spring装配集合
当配置集合类型的bean属性时,spring提供了4种类型的集合配置元素,如下。
集合元素 | 用途 |
<list> | 装配list类型的值,允许重复 |
<set> | 装配set类型,不允许重复 |
<map> | 装配map类型的值,名称和值可以是任意类型 |
<props> | 装配properties类型的值,名称和值必须都是String类型 |
装配List:
<bean id="roles" class="cn.com.ztz.spring.model.Roles">
<property name="roleName" value="辅导员"/>
<property name="users">
<list>
<ref bean="users1"/>
<ref bean="users2"/>
<ref bean="users2"/> //允许重复
</list>
</property>
</bean>
<bean id="users1" class="cn.com.ztz.spring.model.Users">
<property name="name" value="张三"/>
</bean>
<bean id="users2" class="cn.com.ztz.spring.model.Users">
<property name="name" value="李四"/>
</bean>
装配set
<bean id="roles" class="cn.com.ztz.spring.model.Roles">
<property name="roleName" value="辅导员"/>
<property name="users">
<set>
<ref bean="users1"/>
<ref bean="users2"/>
<ref bean="users2"/> //不允许重复,user2只会注入一个。
</set>
</property>
</bean>
<bean id="users1" class="cn.com.ztz.spring.model.Users">
<property name="name" value="张三"/>
</bean>
<bean id="users2" class="cn.com.ztz.spring.model.Users">
<property name="name" value="李四"/>
</bean>
装配Map
<bean id="roles" class="cn.com.ztz.spring.model.Roles">
<property name="roleName" value="辅导员"/>
<property name="users">
<map>
<entry key="USERS1" value-ref="users1"/>
<entry key="USERS2" value-ref="users2"/>
</map>
</property>
</bean>
<bean id="users1" class="cn.com.ztz.spring.model.Users">
<property name="name" value="张三"/>
</bean>
<bean id="users2" class="cn.com.ztz.spring.model.Users">
<property name="name" value="李四"/>
</bean>
<map>中的<entry>元素由一个键和一个值组成,键和值可以是简单类型,也可以是其他的Bean的引用。这些属性将帮助我们指定<entry>的键和值
装配properties集合(类似map但是key和value必须均为字符串)
<bean id="roles" class="cn.com.ztz.spring.model.Roles">
<property name="roleName" value="辅导员"/>
<property name="users">
<props>
<prop key="USERS">张三</prop>
<prop key="AGE">19</prop>
</props>
</property>
</bean>
装配null
有时候由于特殊原因,需要将bean的某个值设置为空
这是后我们可以如下配置:
<bean id="roles" class="cn.com.ztz.spring.model.Roles">
<property name="roleName" value="辅导员"/>
<property name="users"><null/></property>
</bean>
Spring----有关bean的配置的更多相关文章
- Spring框架bean的配置(2):SpEL:引用 Bean、属性和方法。。。
将这些架包放入在工程目录下建立的lib文件夹里,并解压 commons-logging-1.1.1 spring-aop-4.0.0.RELEASE spring-beans-4.0.0.RELEAS ...
- Spring中的事物管理,基于spring的bean的配置
很多东西与上边的相同,这儿只简介: 导包... 数据库中建立三个表... 建立存放连接数据库的file文件:jdbc.properties: ----------------------------- ...
- Spring中Bean的配置:基于XML文件的方式
Bean的配置一共有两种方式:一种是基于XML文件的方式,另一种是基于注解的方式.本文主要介绍基于XML文件的方式 <bean id="helloWorld" class=& ...
- String框架搭建的基本步骤,及从 IOC & DI 容器中获取 Bean(spring框架bean的配置)--有实现数据库连接池的链接
Spring框架的插件springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite(是一个压缩包)导入步骤: eclipse->help-> ...
- Spring中bean的配置
先从IOC说起,这个概念其实是从我们平常new一个对象的对立面来说的,我们平常使用对象的时候,一般都是直接使用关键字类new一个对象,那这样有什么坏处呢?其实很显然的,使用new那么就表示当前模块已经 ...
- (转) Spring读书笔记-----Spring的Bean之配置依赖
前一篇博客介绍了Spring中的Bean的基本概念和作用域(Spring读书笔记-----Spring的Bean之Bean的基本概念),现在介绍Spring Bean的基本配置. 从开始我们知道Jav ...
- Spring之Bean的配置方式
在博客中为了演示容器Bean实例化时暴露出的几个接口,将UserBean配置在XML中,其实常见的Bean的配置有3种.1.基于xml配置Bean 2.使用注解定义Bean 3.基于java类提供Be ...
- spring中bean的配置详解--定义parent
在工作中碰到了好多的配置文件,具体来说是spring 中bean配置的parent的配置,搞的我一头雾水,仔细看一下spring中有关bean的配置,剖析一下,具体什么含义! 一.Spring IoC ...
- Spring中Bean的配置:基于注解的方式
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: -@Component:基本注解,标识一个受Spring管理的组件 -@Respositor ...
- Spring框架bean的配置(3):基于注解的配置
1.基于注解的配置: @Component: 基本注解, 标识了一个受 Spring 管理的组件 @Respository: 标识持久层组件 @Service: 标识服务层(业务层)组件 @Contr ...
随机推荐
- HTTP Error 502.5 - Process Failure Win10 VS可以正常访问,部署本地IIS报错
最近本core得升级导致各种问题,之前刚解决了server2012的502.5问题 今天本机又出现这个问题. 情况描述:VS可以正常调试查看,部署本地IIS访问 错误502.5 分析:VS可以使用II ...
- UWP开发入门(二)——RelativePanel
RelativePanel也是Win10 UWP新增的控件,和上篇提到的SplitView一样在UWP的UI布局起到非常重要的作用.说句实在话,这货其实就是为了UWP的Adaptive UI而特意增加 ...
- iOS 设置 (plist)
Architectures:
- python当中的装饰器
1.装饰器 首先我们来说一下一个软件的设计原则:开闭原则,又被称为开发封闭原则,你的代码对功能的扩展是开放的,你的程序对修改源代码是封闭的.这样的软件设计思路可以更好的维护和开发. 开放:对功能扩展开 ...
- python中的循环和编码,运算符, 格式化输出
1.while循环 现在让我们来看看python中的while循环 格式为 while 条件 循环体 (break) (continue) 中断循环的关键字有break和continue, brea ...
- C/C++,python,java,C#月经贴问题
在刚开始的时候,一直纠结于语言之争,什么什么有前途,什么什么没前途.对于什么的支持不好啦,个人信仰问题啦.什么都有. 首先最主要的一个个人观点:“语言不是老婆,不是一夫一妻制”.你可以同时拥有许多的女 ...
- ActionBarSherlock(一)在Eclipse中如何引入ActionBarSherlock和它的例子?
ActionBarSherlock,是一个开源的Actionbar项目(http://actionbarsherlock.com/download.html).为什么我们要用它呢?谷歌已经不强制要求厂 ...
- [agc006f] Blackout 神题
Description 给你一个NN行NN列的网格,第ii行第jj列的格子用(i,j)(i,j)表示 一开始的时候有MM个格子被涂成黑色,其他的格子都是白色,具体一点,涂成黑色的格子为(a1,b1 ...
- C语言 从头学起了
1,我用的是VC6.0++编译器 具体安装使用教程 去网上找 2,刚写hello world时候 就遇到一个坑. #网上 hello world 代码 #include<stdio.h> ...
- ElasticSearch基本查询
词条查询 这是一个简单查询.它仅 匹配给定字段中包含该词条的稳定,且是2未经分析的确切的词条. { “query” :{ “term”:{ “title”:”crime” } } } 多词条查询 匹配 ...