Spring xml配置实例

原文转载:http://blog.csdn.net/u010902721/article/details/51649715

Spring中如何装配Bean

spring中,对象无需自己查找或者创建与它相关联的其他对象,由容器负责将需要相互协作的对象引用传递给各个对象。容器所做的这个工作就是依赖注入的本质,这是Spring提供的最基本的功能。 
Spring是一个基于容器的框架,如果没有配置,它就是一个没有任何作用的空容器。从Spring3.0开始,Spring提供了两种配置Bean的方式:使用XML文件配置和使用注解配置。 
表1 Spring核心框架自带的xml命名空间

命名空间 用途
aop 为声明切面以及将@AspectJ注解的类代理为Spring切面提供了配置元素
beans 声明bean和装配bean
context 配置Spring应用上下文,自动检测、自动装配bean,注入非Spring直接管理的对象
jee 提供了与java ee API的集成,例如JNDI和EJB
jms 为声明消息驱动的pojo提供了配置元素
lang 支持由Groovy、JRuby和BeanShell等脚本实现的bean
mvc 启用Spring MVC
oxm 支持Spring的对象到xml映射配置
tx 提供声明式事务配置
util 提供工具类元素

实例:

整个工程在github上:https://github.com/simon166/demo/tree/master/springAction

一个Performer接口:

package springAction;
public interface Performer {
    void perform();
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

一个Juggler类实现了Performer接口:

package springAction;

public class Juggler implements Performer{
    private int beanBags = 3;
    public Juggler(){

    }

    public Juggler(int beanBags){
        this.beanBags = beanBags;
    }

    public void perform(){
        System.out.println("Juggling " + beanBags + " beanbags");
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

一个 PoeticJuggler类继承了Juggler类:

package springAction;

public class PoeticJuggler extends Juggler {
    private Poem poem;

    public PoeticJuggler(Poem poem){
        super();
        this.poem = poem;
    }

    public PoeticJuggler(int beanBags, Poem poem){
        super(beanBags);
        this.poem = poem;
    }

    public void perform(){
        super.perform();
        System.out.println("PoeticJugger reciting...");
        poem.recite();
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

一个Poem接口:

package springAction;

public interface Poem {
    void recite();
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

一个单例类Stage:

package springAction;

public class Stage {
    private Stage(){

    }

    private static class StageSingletonHolder{
        static Stage instance = new Stage();
    }

    public static Stage getInstance(){
        return StageSingletonHolder.instance;
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

看看xml文件怎么写(springAction.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

       <!-- 配置一个bean -->
       <bean id="duke"  class="springAction.Juggler" >
            <!--给构造函数传递参数,没有的话则调用默认构造方法  -->
            <constructor-arg value="15"/> 
       </bean>

       <bean id="sonnet29" class="springAction.Sonnet29"></bean>

       <bean id="poeticDuke" class="springAction.PoeticJuggler">
            <constructor-arg value="22"></constructor-arg>
            <!-- 基本数据类型参数用value=字符串(Spring根据构造参数类型自动解析字符串),
                             引用类型的参数用ref= bean id -->
            <constructor-arg ref="sonnet29"></constructor-arg>
       </bean>

       <!-- factory-method通过工厂方法将单例类配置为bean,因为Stage没有构造函数 -->
       <bean id="theStage" class="springAction.Stage" factory-method="getInstance">
       </bean>

</beans>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

加个main函数运行一下,看看效果:

package springAction;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class springActionMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "springAction/springAction.xml");
        try{
            Performer performer = (Performer)ctx.getBean("duke");
            performer.perform();
            performer = (Performer)ctx.getBean("poeticDuke");
            performer.perform();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            ((ClassPathXmlApplicationContext)ctx).close();
        }

    }

}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

效果: 
Juggling 15 beanbags 
Juggling 22 beanbags 
PoeticJugger reciting… 
balblabla… 
以上是一个xml文件配置bean的简单例子,下面看看bean的作用域。

bean的作用域

所有的Spring Bean默认是单例,当容器分配一个Bean时,它总是返回同一个实例。但,spring是灵活的,不需要单例模式时,可以如下配置:

<bean id="ticket" class="省略号" scope="prototype"/>
   
   
  • 1
  • 1

scope表示作用域,prototype表示每次返回不同的实例。bean的作用域还有:

作用域 简介
singleton 单例模式(默认)
prototype 每次返回不同的实例
request 该作用域仅用于基于web的spring上下文(spring mvc),在一个HTTP 请求中每个bean定义对应一个实例
session 该作用域仅用于基于web的spring上下文(spring mvc),在一个HTTP Session中每个bean定义对应一个实例
global-session 该作用域仅在Portlet上下文中才有效,在一个全局HTTP Session中每个bean定义对应一个实例

初始化Bean和销毁Bean

当Spring容器实例化一个Bean时可能需要做一些初始化的工作,当Spring销毁一个Bean时,可能需要做一些清理工作。Spring支持Bean提供自定义的方法来做初始化工作和销毁工作。

<bean id="beanName"
    class="classPath"
    init-method="init_method_in_bean"  bean中的初始化方法
    destroy-method="destory_method_in_bean"/>  bean中的销毁方法
   
   
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

如上配置,Spring会自动的在实例化该bean时调用init-method指定的初始化方法,在销毁该bean时调用destory-method指定的销毁方法。

为Bean注入属性

一般地,Java Bean会有一些私有属性,并有一些set和get方法。Spring可以利用set方法为bean的私有属性注入值。 
例子:

package springAction;

public class Instrumentalist implements Performer{
    private String song;
    private Instrument instrument;
    private int age;

    public Instrumentalist(){

    }

    public void perform(){
        System.out.println("age = " + age);
        System.out.println("Playing "+song);
        instrument.play();
    }

    public String getSong() {
        return song;
    }

    public void setSong(String song) {
        this.song = song;
    }

    public Instrument getInstrument() {
        return instrument;
    }

    public void setInstrument(Instrument instrument) {
        this.instrument = instrument;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

xml文件配置:

 <bean id="saxophone" class="springAction.Saxophone"/>

       <bean id="kenny" class="springAction.Instrumentalist">
            <!-- 配置属性song -->
            <property name="song" value="ABC"></property>
            <!-- 配置属性age,虽然此处是字符串“14”,但是Spring会识别age的类型,然后把字符串“14”转变后赋值给age -->
            <property name="age" value="14"></property>
            <!-- 配置instrument对象,用ref的方式赋引用,和构造函数一致 -->
            <property name="instrument" ref="saxophone"></property>
       </bean>


   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值