Spring框架入门笔记——深入了解Spring容器的Bean
2015-03-15 16:58
603 查看
一、Spring容器中的Bean
这部分主要讲解Bean的作用域;普通属性、依赖关系、集合的注入;使用自动装配注入合作者Bean指定bean的别名
<alias name="person" alias="jack" />
如果不想Spring容器预初始化某个singleton Bean时,可以指定lazy-init=”true“ ,在beans标签中属性和bean属性名词一样,只是在前面加上default,如default-lazy-init,表示对虽有Bean都起作用。
<bean id="bean1" class="lee.Test" lazy-init="true" />
当Spring创建Bean实例是,可以为Bean指定特定的作用域。Spring支持如下5中作用域:
singleton 、 prototype 、 request 、session 、 global session
singleton,单例模式,在整个Spring IoC容器中,使用singleton定义的Bean只有一个实例。
prototype,原型模式,每次通过容器的getBean方法都将获得一个新的Bean实例。Bean实例的创建和销毁都需要花费系统的开销,所以除非必要,否则尽量避免将Bean设置为portotype作用域。
request,对于每次Http请求都将返回新的Bean实例,只有在Web应用中使用Spring时,该作用域才真正有效。
session,对于每次Http Session都将产生不同的Bean实例,只有在Web应用中使用Spring时,该作用域才真正有效。
global session,每个全局的HTTP Session对应一个Bean实例,典型情况下,仅在使用portlet context的时候有效。只有在Web应用中使用Spring时,该作用域才真正有效。
Spring创建Bean和注入依赖关系,主要是通过反射机制来实现的,这里不是重点。
Spring通过四种元素为Bean实例注入属性值,分别是 value ref bean list、set 、map及props
Spring的自动装配,可以通过<beans> <bean>两个标签的default-autowire 和 autowire属性来配置
属性接受的值有no、byName、byType、constructor、autodetect
no表示不使用自动装配,默认的就是这种配置,Bean的依赖必须通过显示的ref元素定义。
byName根据属性名自动装配,根据id和属性同名的Bean完成注入,没有找到的话,不进行任何注入。
byType根据属性类型自动注入,如果多个这样的Bean,就会抛异常,没找到的话,什么都不会发生,属性不会被设置,如果想没找到bean装配的话,抛出异常,可以设置dependency-check=”Objects“
constructor和byType类似,都是通过类型注入,区别是通过构造注入。如果不是恰好只有一个同类型的Bean的话,则会抛出异常。
autodetect 根据Bean的内部结构,决定选则是用byType还是使用constructor,如果找到一个默认的构造函数,则会选则使用byType。
大型项目不建议使用自动装配,因为没有显示的配置依赖关系来的清楚明了。
当Bean既使用自动装配,又使用ref显示指定依赖的时候,显示依赖会覆盖自动装配。
<bean>标签的autowire-candidate属性的true或false 可以限制此Bean能否拥有自动装配的能力。也可以在<beans>标签中使用default-autowire-candidate=”*abc“ 来将所有以abc结尾的Bean都排除在自动装配之外。
public class Person { private House house; public void setHouse(House house) { this.house = house; } public void info() { System.out.println(this.house.getAddress()); } }
public class House { <span style="white-space:pre"> </span>private String address; <span style="white-space:pre"> </span>public void setAddress(String address) { <span style="white-space:pre"> </span>this.address = address; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public String getAddress() { <span style="white-space:pre"> </span>return this.address; <span style="white-space:pre"> </span>} }
<span style="white-space:pre"> </span><bean id="house" class="org.crazyit.app.service.House" > <span style="white-space:pre"> </span> <property name="address" value="上海" /> <span style="white-space:pre"> </span> </bean> <span style="white-space:pre"> </span> <bean id="h2" class="org.crazyit.app.service.House" > <span style="white-space:pre"> </span> <property name="address" value="安庆" /> <span style="white-space:pre"> </span> </bean> <span style="white-space:pre"> </span><bean id="ps" class="org.crazyit.app.service.Person" autowire="byName" autowire-candidate="default"> <span style="white-space:pre"> </span></bean>
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); <span style="white-space:pre"> </span>ctx.getBean("ps",Person.class).info();
这里 autowire-candidate="default"或者设置为true 或者false 总是输出上海,说明byName自动装配始终生效,表示autowire-candidate设置的属性一点没生效。不知道为什么,甚至我在<beans>标签中写着default-autowire="byName" <bean id="ps" class="org.crazyit.app.service.Person" autowire-candidate="default" /> 这地方换成true或者false也是同样不生效。不知道为什么。另外还有一个疑问,是”default-autowire-candidate=”*abc“
来将所有以abc结尾的Bean都排除在自动装配之外“,这地方是指id以abc结尾的Bean被排除在自动装配之外,还是说实现类class是以abc结尾的类呢?这地方也没有试出来,default-autowire-candidates="*s"写着也不生效,不知道为什么?有知道的朋友可以告诉下我为什么吗?求大神解惑一下,谢谢。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" default-autowire-candidates="*s"> <bean id="house" class="org.crazyit.app.service.House" > <property name="address" value="上海" /> </bean> <bean id="h2" class="org.crazyit.app.service.House" > <property name="address" value="安庆" /> </bean> <bean id="ps" class="org.crazyit.app.service.Person" autowire="byName"/> <bean id="ps2" class="org.crazyit.app.service.Person" autowire="byName" /> </beans>
ctx.getBean("ps",Person.class).info(); ctx.getBean("ps2",Person.class).info();
上海 上海
Spring注入Bean实例的value 和 ref很简单就不说了,下面代码是spring注入的第三种写法,表示嵌套Bean的注入:
<bean id="ps3" class="org.crazyit.app.service.Person" > <property name="house" > <bean class="org.crazyit.app.service.House" /> </property> </bean>嵌套Bean限制了Bean的访问,提高了内聚性,但是降低了程序的灵活性,只有确定无须通过Spring容器访问某个Bean实例时,才考虑使用嵌套Bean定义。
二、Bean实例的创建方式及依赖配置
三、Bean的生命周期
相关文章推荐
- Spring笔记――5.深入理解容器中的Bean
- spring学习笔记——spring框架bean装配流水线
- 【Spring学习笔记-3.1】让bean获取spring容器上下文(applicationContext.xml)
- Spring实战(第4版) Spring Inaction 笔记(第二章)bean深入
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
- 深入了解Spring中的容器
- spring管理bean容器(笔记)
- Spring学习笔记(二) IoC容器与Bean
- Spring框架使用properties配置数据源Bean【Spring入门】
- [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器
- Spring入门学习——使用Bean感知容器
- Spring-----8、深入理解容器中的bean
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.5 Bean的生命周期
- Spring MVC 教程,快速入门,深入分析――如何取得Spring管理的bean
- 深入spring ——加载外部配置bean到当前容器中
- 【Spring从入门到精通】(四)容器的基础XmlBeanFactory
- Spring学习笔记---2.1-IOC容器解析Bean配置信息
- Spring学习笔记四: 从Spring容器中获取Bean
- Spring学习笔记四: 从Spring容器中获取Bean
- Spring学习笔记——核心容器Bean