Spring学习笔记之bean配置
1、命名bean
每个bean都有一个或者多个的的标识符。这些标识符必须在加载他们的容器里边唯一。一个bean经常有且只有一个标识符,但是如果需要超过一个的名字,可以考虑额外的别名。
基于xml的配置源文件,你可以使用id或者name属性去指定bean的标识符,这里有个问题,如果是名字和ID的不一致,内部又是如何处理的?
如果你不想再另外再给他们定义一个别名,你可以指定name属性,利用逗号(,),分号(;)或者空格。spring3.1以前id属性是xsd:ID类型的,这个是唯一的,3.1以后,xml文件是xsd:string这个xml文件不校验,但是spring需要它唯一。
如果一个bean没有明确指定ID或者name,容器将为bean生成一个唯一的名字。但是,如果你想用ref来引用,就必须定义一个名字。
Motivations for not supplying a name are related to using inner beans and autowiring collaborators.
这句话怎么理解??!!!!!!!!
Bean 命名约定,和java一样,用一个小写字符,驼峰式
在一个bean的定义里边,你可以给bean起多于一个的名字,一个ID,多个名字,名字name和别名alias是等价的
2、
Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata:
A package-qualified class name: typically the actual implementation class of the bean being defined. 
Bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth). 
References to other beans that are needed for the bean to do its work; these references are also called collaborators or dependencies.
合作和依赖有什么区别?
Other configuration settings to set in the newly created object, for example, the number of connections to use in a bean that manages a connection pool, or the size limit of the pool. 
This metadata translates to a set of properties that make up each bean definition.
Table 5.1. The bean definition
Property
class Instantiating beans
name Naming beans
scope Bean scopes
constructor arguments Dependency injection
properties Dependency injection
autowiring mode Autowiring collaborators
lazy-initialization mode Lazy-initialized beans
initialization method Initialization callbacks
destruction method Destruction callbacks
In addition to bean definitions that contain information on how to create a specific bean, the ApplicationContext implementations also permit the registration of existing objects that are created outside the container, by users. This is done by accessing the ApplicationContext’s BeanFactory via the method getBeanFactory() which returns the BeanFactory implementation DefaultListableBeanFactory. DefaultListableBeanFactory supports this registration through the methods registerSingleton(..) and registerBeanDefinition(..). However, typical applications work solely with beans defined through metadata bean definitions.
3、
bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
</bean>
<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg name="years" value="7500000"/>
    <constructor-arg name="ultimateAnswer" value="42"/>
</bean>
// Fields omitted @ConstructorProperties({"years", "ultimateAnswer"}) public ExampleBean(int years, String ultimateAnswer) { this.years = years; this.ultimateAnswer = ultimateAnswer; }
Spring学习笔记之bean配置的更多相关文章
- Spring学习笔记(2)——Bean的配置
		
要使应用程序中的Spring容器成功启动,需要以下三个方面的条件都具备: 1.Spring框架的类包都已经放到应用程序的类路径下 2.应用程序为Spring提供完备的Bean配置信息 3.Bean的类 ...
 - Spring学习笔记(3)——Bean的注入方式
		
依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...
 - Spring学习笔记—装配Bean
		
在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...
 - Spring学习笔记之Bean的实例化
		
一.bean的实例化方法有3种, 1.构造器实例化 2.静态工厂方法实例化 3.实例工厂方法实例化 二.用构造器来实例化 <bean id="ShunDao" class=& ...
 - Spring学习笔记——02 Bean的命名及实例化
		
一.Bean的命名 前一篇讲到IoC是一个管理Bean的容器,Bean多数情况下都是通过XML文件进行配置的,其中Bean的命名有以下几种方式,现在梳理一下. 1. 不指定id,只配置类名 <b ...
 - spring学习笔记之---bean管理
		
bean管理(xml) (一)spring的工厂类 FileSystemXmlApplicationContext 读取磁盘配置文件 (二)bean实例化的三种方式 (1)使用类构造器实例化(默认无参 ...
 - Spring学习笔记--注入Bean属性
		
这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果. package com.moonlit.myspring; import java.util.List; im ...
 - spring学习笔记之---bean管理的注解方式
		
bean管理的注解方式 (一)使用注解定义bean (1)常用注解 (2)实例 1.在pom.xml中进行配置 <dependencies> <dependency> < ...
 - Spring学习笔记之Bean的一些属性设置
		
1.beans 里边配置default-init-method="shunge",有这个方法的会执行,没有也不会报错 2.beans 里边配置default-destroy-met ...
 
随机推荐
- 【ufldl tutorial】Softmax Regression
			
今天太长姿势了,什么叫懂了也写不出代码说的不就是我吗,就那么几行代码居然叽叽歪歪写了一个小时. 首先exercise要实现的是softmax的cost function和gradient,如下图: ( ...
 - 闲谈--心态 (zhuan)
			
http://blog.csdn.net/marksinoberg/article/details/53261034 ***************************************** ...
 - Intent 隐式跳转,向下一个活动传递数据,向上一个活动返回数据。
			
一.每个Intent只能指定一个action,多个Category. 使用隐式跳转,我们不仅可以跳转到自己程序内的活动,还可以启动其他程序的活动.使得Android多个程序之间的功能共享成为可能. 例 ...
 - C# Socket编程(3)编码和解码
			
在网络通信中,很多情况下:比如说QQ聊天,通讯双方直接传递的都是字符信息.但是字符信息并不能够直接通过网络传输,这些字符集必须先转换成一个字节序列后才能够在网络中传输,于是这里就产生了编码和解码的概念 ...
 - PHP开发者常犯的MySQL错误
			
PHP开发者常犯的MySQL错误 数据库是WEB大多数应用开发的基础.如果你是用PHP,那么大多数据库用的是MYSQL也是LAMP架构的重要部分. PHP看起来很简单,一个初学者也可以几个小时内就 ...
 - mysql命令行登录指定编码
			
mysql --default-character-set=utf8 -uroot -p mysql --default-character-set-utf8 -uroot-p
 - 在滚动列表中实现视频的播放(ListView & RecyclerView)
			
英文原文:Implementing video playback in a scrolled list (ListView & RecyclerView) 本文将讲解如何在列表中实现视频播放. ...
 - 和为S的两个数VS和为S的连续正数序列
			
其实这个题目如果没有限制时间复杂度的话,那么就很简单了,一遍一遍地扫描吧.时间复杂度肯定就是 O(n2)啰.但是这题目肯定不会这么简单,否则就是小学生的水平了嘛. 其实我刚到这题的时候想到的是用二叉查 ...
 - alert与console.log
			
1.alert在页面中弹出 console.log是在控制台显示 例子 var aa="Silence"; alert(typeof(aa)); console.log(typeo ...
 - q和blockquote标签的区别
			
q用来分离文本中的引语,定义一个短的引用.该标签会对引用的文本加双引号,一般情况很少用,博客论坛系统会用得多一些: blockquote用于对长文本的引用,用来定义一段引语,标签内的内容会自动有缩进: ...