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>

时间: 2024-08-24 07:01:11

Spring装配Bean---使用xml配置的相关文章

框架 day36 Spring3 入门,DI依赖注入,装配bean基于xml/注解, 整合Junit4,配置约束自动提示

1 什么是spring 1.1官网 spring.io 1.2介绍 Spring的核心是控制反转(IoC)和面向切面(AOP). IoC(Inverse of Control 反转控制) AOP(Aspect Oriented Programming 面向切面编程为内核) 简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架. *轻量级:依赖其他内容较小,使用资源消耗也少.对比:EJB 重量级 *分层:经典三层体系架构,spring 提供解决方案

Spring 装配Bean

Spring 装配Bean 装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介绍 1 :声明Bean Bean的概念:beans 本身是一个大工厂,beans中的每一个bean就等于定义了一个组件,每个组件中就是我们具体的某个功能 1.1 :创建Spring 配置Spring是很重要的,如果没有配置Spring,那么就等于声明了一个空的容器,毫无意义. 通过配置Spring容

Spring装配Bean的过程

首先说一个概念:“懒加载” 懒加载:就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到容器中. spring配置文件中bean默认是lazy-init=“false”为非懒加载.下面具体说明. 1.默认情况下bean实例化过程: AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("/beans.xml"); //随着spri

spring管理SessionFactory中XML配置

1. 综合练习目标 2. 综合练习需求 3.模块划分 1. 综合练习目标 <1>复习 Java 基本语法 <2>熟悉掌握Java开发常用API <3>尝试建立面向对象思想 2. 综合练习需求 <1>接收用户的命令行输入 <2>以文件为基础完成数据的增删改查操作          3.模块划分 UI模块:(Java存在文本中都是以字符型式) 数据验证模块:验证用户输入是否合法 spring管理SessionFactory中XML配置,布布扣,bub

Spring装配Bean的过程补充

对上一篇的<Spring装配Bean的过程>的过程说一下,不然真产生了误区. 误区在哪里呢?那就是spring bean的作用域问题. 说哈常用的两种作用域:默认是scope = singletonsingleton:在每个Spring IoC容器中一个bean定义对应一个对象实例. prototype:一个bean定义对应多个对象实例,每次获取bean就是实例化新的bean. 下面说重点了: 当scope=singleton,即默认情况,会在容器初始化时实例化.但我们可以指定Bean节点的l

Spring的配置文件ApplicationContext.xml配置头文件解析

Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applicationContext.xml配置头文件解析 <?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"

Spring装配bean(在java中进行显式配置)

1.简单介绍 Spring提供了三种装配机制: 1.在XML中进行显式配置: 2.在java中进行显式配置: 3.隐式的bean发现机制和自动装配. 其中,1和3项在项目中经常使用,而在java中进行显示配置方式很少使用.本文专门介绍第2种方式. 如果在项目中,我们需要将第三方库装配到spring中,这时候就没法使用隐式装配方式(没法在第三方库中加@Component等注解),这时候, 就需要在两种显式配置中选方法配置. 其中在java中进行显式配置方式是更好的方案,因为它更为强大.类型安全并且

Spring MVC的web.xml配置详解(转)

出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(listener-class) ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web.

spring对mytatis的xml配置以及quartz

前言:使用spring+mybatis+mysql的框架构建Java代码已经有段时间了,但是对于applicationContext.xml文件中的配置项并没有很明确的概念性知识,经过阅读<spring实战3>,结合项目中运用的知识,对本文件的配置做一些说明,同时,非常迫切的希望有这方面经验的朋友对datasource的配置项提出一些指导性的建议. Java项目 applicationContext.xml配置项中包含了 引入jdbc配置文件.创建jdbc数据源.quartz等,见以下内容 &