Resource实现 | 目的 |
org.springframework.core.io.ByteArrayResource | 定义内容由一组字节给定的资源 |
org.springframework.core.io.ClassPathResource | 定义可从classpath提取的资源 |
org.springframework.core.io.DescruptiveResource | 定义包含资源描述符但是实际没有可读资源的资源 |
org.springframework.core.io.FileSystemResource | 定义可从文件系统提取的资源 |
org.springframework.core.io.InputStreamResource | 定义可从输入流提取的资源 |
org.springframework.web.portlet.context.PortletContextResource | 定义可用在portlet上下文中的资源 |
org.springframework.web.context.support.ServletContextResource | 定义可用在servlet上下文中的资源 |
org.springframework.core.io.UrlResource | 定义可从给定URL提取的资源 |
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("c:/beans.xml")); MyBean myBean = (MyBean)factory.getBean("myBean"); |
ApplicationContext ctx = new FileSystemXmlApplicationContext("foo.xml"); ApplicationContext ctx = new ClassPathXmlApplicationContext("foo.xml"); |
public interface Performer { void perform() throws PerformanceException; } |
package com.springinaction.springidol; public class Juggler implements Performer { private int beanBags = 3; public Juggler() {} public juggler(int beanBags) { this.beanBags = beanBags; } public void perform() throws PerformanceException { System.out.println("JUGGLING" + beanBags + "BEANBAGS"): } } |
<bean id="duke" class="com.springinaction.springidol.Juggler" /> |
<bean id="duke" class="com.springinaction.springidol.Juggler"> <constructor-arg value="15" /> </bean> |
public interface Instrument { void play(); } public class Saxophone implements Instrument { public Saxophone() {} public void play() { System.out.println("TOOT TOOT TOOT"); } } publci class Instrumentalist implement Performer { public Instrumentalist() {} public void perform() throws PerformanceException { System.out.print("Playing" + song + ":"); instrument.play(); } private int age; public void setSong(int age) { this.age= age; } private String song; public void setSong(String song) { this.song = song; } private Instrument instrument; public void setInstrument (Instrument instrument) { this.instrument= instrument; } } |
<bean id="instrument" class="com.springinaction.springidol.Saxophone" /> <bean id="kenny" class="com.springinaction.springidol.Instrumentalist"> <property name="age" value="37" /> <property name="song" value="Jingle Bells" /> <property name="instrument" value="instrument" /> </bean> |
package org.spring.beans.collection;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class OneManBand implements Performer {
public OneManBand(){}
public void perform() {
System.out.println("---------------List--------------------");
for(Instrument instrument : instruments){
instrument.play();
}
System.out.println("---------------Set--------------------");
for(Instrument instrument : instruments2){
instrument.play();
}
System.out.println("---------------Map--------------------");
for(String key : instruments3.keySet()){
System.out.print(key+" : ");
Instrument instrument = instruments3.get(key);
instrument.play();
}
System.out.println("------------Properties----------------");
for(Iterator iter = instruments4.keySet().iterator();iter.hasNext();){
String key = (String)iter.next();
System.out.println(key+" : " + instruments4.getProperty(key));
}
}
private Collection<Instrument> instruments;
public void setInstruments(Collection<Instrument> instruments) {
this.instruments = instruments;
}
private Collection<Instrument> instruments2;
public void setInstruments2(Collection<Instrument> instruments2) {
this.instruments2 = instruments2;
}
private Map<String,Instrument> instruments3;
public void setInstruments3(Map<String,Instrument> instruments3) {
this.instruments3 = instruments3;
}
private Properties instruments4;
public void setInstruments4(Properties instruments4) {
this.instruments4 = instruments4;
}
}
|
<bean id="Hank" class="org.spring.beans.collection.OneManBand">
<property name="instruments">
<list>
<ref bean="guitar"/>
<ref bean="cymbal"/>
<ref bean="harmonica" />
</list>
</property>
<property name="instruments2">
<set>
<ref bean="guitar"/>
<ref bean="cymbal"/>
<ref bean="harmonica" />
<ref bean="harmonica" />
</set>
</property>
<property name="instruments3">
<map>
<entry key="GUITAR" value-ref="guitar" />
<entry key="CYMBAL" value-ref="cymbal" />
<entry key="HARMONICA" value-ref="harmonica" />
</map>
</property>
<property name="instruments4">
<props>
<prop key="GUITAR">STRUM STRUM STRUM</prop>
<prop key="CYMBAL">CRASH CRASH CRASH</prop>
<prop key="HARMONICA">HUM HUM HUM</prop>
</props>
</property>
</bean>
<bean id="guitar" class="org.spring.beans.collection.Saxophone">
<property name="str" value="STRUM STRUM STRUM" />
</bean>
<bean id="cymbal" class="org.spring.beans.collection.Saxophone">
<property name="str" value="CRASH CRASH CRASH" />
</bean>
<bean id="harmonica" class="org.spring.beans.collection.Saxophone">
<constructor-arg value="HUM HUM HUM"></constructor-arg>
</bean>
|
public class CollectionApp {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("org/spring/beans/collection/collection.xml");
Performer performer = (Performer)ctx.getBean("Hank");
performer.perform();
}
}
|
<bean id="saxophone" class="org.spring.beans.collection.Saxophone" scope="prototype" /> |
范围 | 完成任务 |
singleton | 定义Bean的范围为每个Spring容器一个实例(默认值) |
Prototype | 允许Bean可以被多次实例化(使用一次就创建一个) |
request | 定义Bean的范围是HTTP请求。只有使用有web能力的Spring上下文(例如Spring MVC)时才有效 |
session | 定义Bean的范围是HTTP会话。只有使用有web能力的Spring上下文(例如Spring MVC)时才有效 |
plobal-session | 定义Bean的范围是全局HTTP会话。只有在portlet上下文中才有效 |
package org.spring.beans.controlbean;
public class Stage {
private Stage() {}
//简单的装在实例
private static class StageSingletonHolder {
static Stage instance = new Stage();
}
//返回实例
public static Stage getInstance() {
return StageSingletonHolder.instance;
}
}
|
<bean id="theStage" class="org.spring.beans.controlbean.Stage" factory-method="getInstance" /> |
package org.spring.beans.controlbean;
public class Instrumentalist {
private Instrument instrument;
public Instrument getInstrument() {
return instrument;
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public void tuneInstrument() {
instrument.tune();
}
public void cleanInstrument() {
instrument.clean();
}
}
|
<bean id="kenny" class="org.spring.beans.controlbean.Instrumentalist"
init-method="tuneInstrument" destroy-method="cleanInstrument">
<property name="instrument" ref="saxophone" />
</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-2.0.xsd"
default-init-method="initInstrument"
default-destroy-method="cleanInstrument">
</beans>
|