所有的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();
}
}