Spring装配Bean---使用xml配置
声明Bean
Spring配置文件的根元素是<beans>.
在<beans>元素内,你可以放所有的Spring配置信息,包括<bean>元素的声明.
除了Beans命名空间,Spring的核心框架总共自带了10个命名空间配置:
命名空间 | 用途 | |||
aop | 为声明切面以及将@AspectJ注解的类代理为Spring切面提供了配置元素 | |||
beans | 支持声明Bean和装配Bean,是Spring最核心也是最原始的命名空间 | |||
context | 为配置Spring应用上下文提供了配置元素,包括自动检测和装配Bean,注入非Spring直接管理的对象 | |||
jee | 提供了与Java EE API的集成,例如JNDI和EJB | |||
jms | 为声明消息驱动的POJO提供了配置元素 | |||
lang | 支持配置由Groovy、JRuby、BeanShell等脚本实现的Bean | |||
mvc | 启用SpringMVC的能力,例如面向注解的控制器、视图控制器和拦截器 | |||
oxm | 支持Spring的对象到xml配置的映射 | |||
tx | 提供声明式事物配置 | |||
util | 提供各种各样的工具类元素,包括把集合配置为Bean,支持属性占位符元素 |
xml结构如下:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="" class="">......</bean>
<bean id="" class="">......</bean>
</beans>
基于构造函数注入
使用<constructor-arg>元素。如果不配置<constructor-arg>元素,那么Spring将使用默认的构造函数。
<!-- 诗 -->
<bean id="poem" class="com.wjx.betalot.impl.Sonnet">
<constructor-arg value="Sonnet poem"></constructor-arg>
</bean>
<!-- 诗人 -->
<bean id="poet" class="com.wjx.betalot.impl.Joe">
<constructor-arg ref="poem"></constructor-arg>
</bean>
通过工厂方法创建Bean
<bean>元素有一个factory-method属性,允许我们调用一个指定的静态方法,从而代替构造函数来创建一个类的实例
<bean id="stage" class="com.wjx.betalot.impl.Stage" factory-method="getInstance" />
配置Bean的作用域
<bean>元素有一个scope属性,允许我们指定Bean的作用域,Bean的作用域主要有一下几种,默作用域为单例singleton
作用域 | 定义 |
singleton | 在每一个Spring容器中,一个Bean定义只有一个对象实例(默认) |
prototype | 允许Bean的定义可以被实例化任意次(每次调用都创建一个实例) |
request | 在一次HTTP请求中,每个Bean定义对应一个实例。该作用域仅在基于Web的Spring上下文(例如SpringMVC)中才有效 |
session | 在一个HTTP Session中,每个Bean定义对应一个实例。该作用域仅在基于Web的Spring上下文(例如SpringMVC)中才有效 |
global-session | 在一个全局HTTP Session中,每个Bean定义对应一个实例。该作用域仅在Portlet上下文中才有效 |
<bean id="poem" class="com.wjx.betalot.impl.Sonnet" scope="prototype">
配置Bean的初始化和销毁方法
Spring提供了Bean生命周期的钩子方法。
为Bean定义初始化和销毁操作,只需要使用init-method和destroy-method参数来配置<bean>元素。init-method属性指定了在初始化Bean时要调用的方法;destroy-method属性指定了Bean从容器移除之前要调用的方法。
<!-- 观众看表演,表演开始前鼓掌欢迎,表演结束鼓掌 -->
<bean id="auditorium" class="com.wjx.betalot.impl.Auditorium" init-method="applause" destroy-method="applause"/>
使用<beans>元素的default-init-method和default-destroy-method属性配置所有<bean>共同默认的初始化方法和销毁方法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-init-method="applause"
default-destroy-method="applause"> ...
</beans>
注入Bean的属性
使用<property>元素。value填充基础类型值,ref填充<bean>引用
<!-- 诗 -->
<bean id="poem" class="com.wjx.betalot.impl.Sonnet">
<property name="lines" value="Sonnet poem"></property>
</bean>
<!-- 诗人 -->
<bean id="poet" class="com.wjx.betalot.impl.Joe">
<property name="poem" ref="poem"></property >
</bean>
装配集合属性,Spring提供了4种类型的集合配置属性 <list> <set> <map> <props>
<bean id="poeticJuggler" class="com.wjx.betalot.performer.impl.PoeticJuggler">
<property name="poemsMap">
<map>
<entry key="POEM1" value-ref="poem1"/>
<entry key="POEM2" value-ref="poem2"/>
<entry key="POEM3" value-ref="poem3"/>
</map>
</property>
<property name="poemProperties">
<props>
<prop key="poem3">POEM3</prop>
<prop key="poem2">POEM2</prop>
<prop key="poem1">POEM1</prop>
</props>
</property>
<property name="poemList">
<list>
<ref bean="poem1"/>
<ref bean="poem2"/>
<ref bean="poem3"/>
</list>
</property>
<property name="poemSet">
<set>
<ref bean="poem1"/>
<ref bean="poem2"/>
<ref bean="poem3"/>
</set>
</property>
</bean>
装配空值
<property name="someNonNullProperty"><null/></property>
除了<property>元素配置属性外,使用spring的命名空间p也可以装配属性,当然你得在<beans>元素中先引入命名空间p
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="poem" class="com.wjx.betalot.impl.Sonnet" p:line="Sonnet"/>
<bean id="poet" class="com.wjx.betalot.impl.Joe" p:poem-ref="poem"/>
</beans>
好啦,关于使用xml配置装配Bean的内容就讲到这边,当然有些缺漏,比如没有讲到装配内部Bean,使用SpEL表达式装配属性等,这些有兴趣的可以去了解下。
Spring装配Bean---使用xml配置的更多相关文章
- Spring装配Bean之XML装配bean
在Spring刚出现的时候,XML是描述配置的主要方式,在Spring的名义下,我们创建了无数行XML代码.在一定程度上,Spring成为了XML的同义词. 现在随着强大的自动化配置和Java代码的配 ...
- IoC容器装配Bean(xml配置方式)(Bean的生命周期)
1.Spring管理Bean,实例化Bean对象 三种方式 第一种:使用类构造器实例化(默认无参数) package cn.itcast.spring.initbean; /** * 使用构造方法 实 ...
- Spring实战——无需一行xml配置实现自动化注入
已经想不起来上一次买技术相关的书是什么时候了,一直以来都习惯性的下载一份电子档看看.显然,如果不是基于强烈的需求或强大的动力鞭策下,大部分的书籍也都只是蜻蜓点水,浮光掠影. 就像有位同事说的一样,有些 ...
- Spring 装配Bean
Spring 装配Bean 装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介 ...
- Spring装配bean
Spring配置的可选方案 Spring提供了如下三种装配机制: (1)在XML中显式配置 (2)在Java中显式配置 (3)隐式的bean发现机制和自动装配 Spring有多种方式可以装配bean, ...
- Spring 装配Bean入门级
装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介绍 1 :声明Bean B ...
- Spring装配Bean的过程
首先说一个概念:“懒加载” 懒加载:就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到容器中. spring配置文件中be ...
- 【转】spring 装配Bean中构造参数的注入
转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...
- Spring的配置文件ApplicationContext.xml配置头文件解析
Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...
随机推荐
- ELF文件格式分析--结构篇
ELF文件格式,全称为Excutable and Linking Format,是一个开放的可执行文件和链接文件格式,在LINUX上很流行,跨平台软件的设计也多以ELF格式作为标准,其结构扩展性兼容性 ...
- Matlab笔记
提取RGB分量,并显示出来 rgb=imread('mao.jpg'); rgb_r=rgb(:,:,); rgb_g=rgb(:,:,); rgb_b=rgb(:,:,); zero=zeros(, ...
- IOS开发中UIFont字体设置
我们在开发中很多时候要设置UIlabel,UIbutton,UItextfield的字体,这个时候我们就需要用到UIFont,下面简单介绍一下UIFont的用法,仅供参考. UIFont用于获取和设置 ...
- EF dbcontext上下文的处理
,那么我们整个项目里面上下文的实例会有很多个,我们又遇到了多次,当我们在编程的时候遇到多的时候,一般我们就要想想能不能解决多这个问题. (2)这里我要说的是EF上下文怎么管理呢?很简单啦,就是要保证线 ...
- Asp.NET开启一个线程,不停的执行
using System;using System.Threading;using System.Threading.Tasks; class StartNewDemo{ static void ...
- python获取绑定的IP,并动态指定出口IP
在做采集器的过程中,经常会遇到IP限制的情况,这时候可以通过切换IP能继续访问. 如果是多IP的服务器,那么可以通过切换出口Ip来实现. 1.首先是如何获取服务器绑定的IP import netifa ...
- Xcode 之 snippet 代码重用
1. 选中代码 2. 拖入xcode 右下侧的 snippet 区域 3. 修改名称 4. 修改快捷输入 (shortcut) 5. <#content#> ,可选修改项
- Poi之Word文档结构介绍
1.poi之word文档结构介绍之正文段落 一个文档包含多个段落,一个段落包含多个Runs,一个Runs包含多个Run,Run是文档的最小单元 获取所有段落:List<XWPFParagraph ...
- zabbix3.0.4 部署History
[root@zabbix-Test ~]# history 1 passwd root 2 exit 3 yum install ntpd* 4 yum inst ...
- Selenium2(java)selenium常用API 四
WebElement相关方法 1.点击操作 WebElement button = driver.findElement(By.id("login")); button.click ...