Spring Bean的作用域和XML配置

所有的Spring Bean默认都是单例的。当容器分配一个bean时(不论是通过装配,还是调用容器的getBean() 方法),它总是返回一个同一个bean对象。当需要多个实例时(比如票对象,电影票每个人都是不一样的),需要额外的配置

<bean id="ticket" class="com.ticket" scope="prototype">
</bean>

可以对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配置
这里写图片描述

package bean;

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

/**
 * 杂技师
 * 
 * @author SJ-PC
 *
 */
public class Juggler implements Performer {
    private int beanBags = 3;
    public Juggler() {
    }
    public Juggler(int beanBags){
        this.beanBags=beanBags;
    }
    @Override
    public void perform() {
        System.out.println("表演杂技"+" "+beanBags);
    }
}
package bean;

public interface Poem {
    void recite();
}
package bean;

public class Sonnet29 implements Poem {

    @Override
    public void recite() {
        System.out.println("Sonnet29 recite");

    }

}
package bean;

public class PoeticJuggler extends Juggler {
    Poem poem;

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

    public PoeticJuggler(Poem poem, int beanbags) {
        super(beanbags);
        this.poem = poem;
    }
    @Override
    public void perform() {

        super.perform();
        System.out.println("while reciting");
        poem.recite();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
    http://www.springframework.org/schema/cache  
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="duke" class="bean.Juggler">
        <constructor-arg value="15" />
    </bean>
    <bean id="sonnet29" class="bean.Sonnet29">
    </bean>
    <bean id="poeticjuggler" class="bean.PoeticJuggler">
        <constructor-arg value="15"/>
        <constructor-arg ref="sonnet29" />
    </bean>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import bean.Performer;


public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-idol.xml");
        Performer performer = (Performer)context.getBean("poeticjuggler");
        performer.perform();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值