2015年11月30日 spring初级知识讲解(一)装配Bean
序,Spring的依赖注入是学习spring的基础。IOC为控制反转,意思是需要的时候就由spring生成一个,而不是先生成再使用。
写在前面
Spring提供面向接口编程,面向接口编程与依赖注入协作实现了松散耦合。
Spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context ">
<!-- Beans 声明 -->
</beans>
从Spring获得Bean
package single.spring; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class testSpringXML { public static void main(String[] args) throws PerformanceException {
//装配Spring.xml配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
//从Spring容器获得声明的Bean
Performer performer = (Performer) ctx.getBean("duke");
//执行Bean的方法
performer.perform();
}
}
一、默认构造函数注入
要求此类具有默认构造函数
<bean id="duke" class="single.spring.Juggler"></bean>
二、通过构造器注入
1 含参构造函数,参数为基本数据类型
public Juggler(int beanBags) {
this.beanBags = beanBags;
}
使用传参构造函数实例化Bean
<bean id="duke"
class="single.spring.Juggler">
<!-- 通過含參構造函數實例化Bean -->
<constructor-arg value="15"></constructor-arg>
</bean>
2 含参数构造函数,参数为引用类型
Performer poeticDuke = (Performer)ctx.getBean("poeticDuke");
poeticDuke.perform();
<bean id="poeticDuke" class="single.spring.PoeticJuggler">
<constructor-arg value="15"></constructor-arg>
<!-- 参数类型为引用类型 用ref-->
<constructor-arg ref="sonnet29"></constructor-arg>
</bean> <bean id="sonnet29" class="single.spring.Sonnet29">
</bean>
三、通过工厂方法创建Bean
有时候静态工厂方法是实例化对象的唯一方法,出于线程安全考虑,getInstance()使用了一种被称为“initialization on demand holder”的技术来创建单例类的实例。
Initialization on Demand Holder模式,这种方法使用内部类来做到延迟加载对象,在初始化这个内部类的时候,JLS(Java Language Sepcification)会保证这个类的线程安全(the class initialization phase is guaranteed by the JLS to be serial),这种写法最大的美在于,完全使用了Java虚拟机的机制进行同步保证,没有一个同步的关键字。
package single.spring;
public class Stage {
private Stage() {
}
//延迟加载实例
private static class StageSingletonHolder {
static Stage instance = new Stage();
}
//返时实例
public static Stage getInstance() {
return StageSingletonHolder.instance;
}
}
spring.xml配置,factory-method属性配置静态方法
<bean id="theStage" class="single.spring.Stage" factory-method="getInstance"></bean>
四、Bean的作用域
所有的Spring Bean默认都是单例,但是Spring提供了一个scope属性来每次成不同的实例
singleton:在每个Spring容器中,一个Bean定义只有一个对象实例(默认)
prototype:允许Bean的定义可以被实例化任意次(每次调用都创建一个实例)
request:在一次Http请求中,每个Bean定义对应一个实例,该作用域仅在基于Web的Spring的上下文(例如Spring MVC)中才有效。
session:在一个Http Session中,每个Bean定义对应一个实例。该作用域仅在基于Web的Spring上下文(例如Spring MVC)中才有效。
global-session:在一个全局Http Session中,每个Bean定义对应一个实例。该作用域仅在Portlet上下文中才有效。
五、初始化和销毁Bean
为了满足初始化和销毁Bean的需求,Spring提供了Bean声明周期的钩子方法。
init-method:提供Bean初始化时调用方法
destroy-method:提供Bean销毁时调用方法
<bean id="auditorium" class="single.spring.Auditorium" init-method="turnOnLights" destroy-method="turnOffLights"></bean>
六、注入Bean属性(属性值为单个值)
使用setter方法注入Bean中的成员属性,前提是存在标准的Setter方法,使用property标签实现,name对应Bean中的成员属性名称,如果属性类型为基本数据类型就用value,如果为引用类型则用ref。
<bean id="kenny" class="single.spring.Instrumentalist">
<!--Setter 基本数据类型,用value -->
<property name="song" value="Jingle Bells"></property>
<!--Setter 引用类型,用ref -->
<property name="instrument" ref="saxophone"></property>
</bean>
<bean id="saxophone" class="single.spring.Saxophone"></bean>
也可以使用内部Bean注入的方式实现成员属性赋值。缺点是内部Bean没有属性ID,导致不能被复用。
<bean id="kenny" class="single.spring.Instrumentalist">
<!--Setter 基本数据类型,用value -->
<property name="song" value="Jingle Bells"></property>
<!--Setter 引用类型,用内部Bean -->
<property name="instrument">
<bean class="single.spring.Saxophone"></bean>
</property>
</bean>
七、装配集合(属性值为集合)
<list> 装配list类型的值,允许重复,适用数组或者java.util.Collection
<set> 装配Set类型的值,不允许重复,适用数组或者java.util.Collection
<map> 装配Map类型的值,名称和值可以是任意类型,java.util.Map
<props> 装配properties类型的值,名称和值必须都是String型,java.util.Properties
1 装配List、Set和Array
成员属性为Collection或Array时都可以,利用List或者Set都可以,可以嵌套适用。
<bean id="hank" class="single.spring.OneManBank">
<property name="instruments"><!-- 成员属性 -->
<list><!-- Collection or Array 类型-->
<ref bean="guitar" /> <!-- 引用类型,基本数据类型则为value -->
<ref bean="cymbal" /> <!-- 还可以为list set 等-->
<ref bean="harmonica" />
</list>
</property>
</bean>
2 装配Map集合
<bean id="hank" class="single.spring.OneManBank">
<property name="instruments"><!-- 成员属性 -->
<map><!-- 只能为Map -->
<entry key="GUITAR" value-ref="guitar"></entry><!-- 根据key value类型可以选择 key key-ref value value-ref>
<entry key="CYMBAL" value-ref="cymbal"></entry><!-- 根据key value类型可以选择 key key-ref value value-ref>
<entry key="HARMONICA" value-ref="harmonica"></entry><!-- 根据key value类型可以选择 key key-ref value value-ref>
<entry
</map>
</property>
</bean>
3 装配Properties集合
成员属性为Properties时需要用下面,Properties就是key和value均为String类型的Map
<bean id="hank" class="single.spring.OneManBank">
<property name="instruments"><!-- 成员属性 -->
<props>
<prop key="GUITAR">STRUM STRUM STRUM</prop>
<prop key="CYMBAL">CRASH CRASH CRASH</prop>
<prop key="HARMONICA">HUM HUM HUM</prop>
</props>
</property>
</bean>
八、装配空值
当需要给成员属性赋值为空时,可以使用如下:
<property name="instruments"><null></null></property>
九、使用SpEL表达式装配
2015年11月30日 spring初级知识讲解(一)装配Bean的更多相关文章
- 2015年12月13日 spring初级知识讲解(四)面向切面的Spring
2015年12月13日 具体内容待补充...
- 2015年12月10日 spring初级知识讲解(二)最小化Spring XML配置 注解
序,随着Spring容器管理Bean数量增加,XML文件会越来越大,而且纯手工配置XML很繁琐,Spring和JAVA都提供了一些注解方式用以简化XML配置. 目录 一.自动装配(autowiring ...
- 2015年12月10日 spring初级知识讲解(三)Spring消息之activeMQ消息队列
基础 JMS消息 一.下载ActiveMQ并安装 地址:http://activemq.apache.org/ 最新版本:5.13.0 下载完后解压缩到本地硬盘中,解压目录中activemq-core ...
- 11月30日《奥威Power-BI智能分析报表制作方法》腾讯课堂开课啦
这么快一周就过去了,奥威公开课又要与大家见面咯,上节课老师教的三种报表集成方法你们都掌握了吗?大家都知道,学习的结果在于实际应用,想要熟练掌握新内容的要点就在于去应用它.正是基于这一要点,每一期的课程 ...
- 2016年11月30日 星期三 --出埃及记 Exodus 20:21
2016年11月30日 星期三 --出埃及记 Exodus 20:21 The people remained at a distance, while Moses approached the th ...
- Git学习(二)(2015年11月18日)(2016年1月29日)
2015年11月18日Git学习: .Shell 删除文件夹及其所有文件 rd/s/q 文件目录 ---------------当前为先创建本地Git库后与网上Git服务器关联------------ ...
- 2019-03-18 Python time 将2015年11月20日转换为2015-11-20
#ReportingDate = soup.select('body > div.main > div > div.ctr > div.recruit > ul > ...
- 北京Uber优步司机奖励政策(11月30日~12月4日)
用户组:人民优步(适用于12月1日)奖励政策: 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:htt ...
- 2015年11月26日 Java基础系列(三)ThreadLocal类初级学习
序,ThreadLocal类是为了解决多线程的安全问题.线程安全的意思也就是说每个线程操作自己的变量,不要对其他线程的值造成影响. 在很多情况下,ThreadLocal比直接使用synchronize ...
随机推荐
- BZOJ1178 [Apio2009]CONVENTION会议中心
本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权! Description Siruseri政府建造了 ...
- [NOIP2015] 提高组 洛谷P2679 子串
题目背景 无 题目描述 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在字符串 A 中出现的顺序依次连接起来得到一 个新 ...
- js调用刷新
_self.fireEvent('refresh');
- JAVA线程池的分析和使用
1. 引言 合理利用线程池能够带来三个好处.第一:降低资源消耗.通过重复利用已创建的线程降低线程创建和销毁造成的消耗.第二:提高响应速度.当任务到达时,任务可以不需要等到线程创建就能立即执行.第三:提 ...
- PL/0编译器(java version) – Scanner.java
1: package compiler; 2: 3: import java.io.BufferedReader; 4: import java.io.FileNotFoundException; ...
- Nuxt.js logoVue.js 后端渲染开源库 Nuxt.js
Nuxt.js 是一个通过 Vue 用于服务端渲染的简单框架,灵感来自 Next.js. 目前尚处于开发阶段,1.0 版本即将发布 1 分钟视频演示 Nuxt 基于 ES2015,这使得代码有着更愉快 ...
- linux配置网卡
我爱折腾.在本地虚拟机里装了linux的环境.要配置linux的网卡文件. 如下: vi /etc/sysconfig/network-script/ifcfg-eth0; 刚装完系统,没有vim , ...
- 利用百度开发者中心的api实现地图及周边的搜索
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- select例子
好长时间没有写了,其实一直在坚持学习. #include <sys/types.h> #include <sys/socket.h> #include <stdio.h& ...
- mysql循环获取结果集
do { MYSQL_RES* res = mysql_store_result(con); ) { MYSQL_ROW row; if (row = mysql_fetch_row(res)) { ...