欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Bean简单应用实例 博客分类: Spring Spring Bean简单应用实例装配beanbean注入 

程序员文章站 2024-03-15 13:14:41
...

学习的Spring实战(第三版)Bean的简单应用
Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 
1.创建一个表演者的接口

package com.performer.service;

public interface Performer {
	void perform();
}

 

2.创建一个杂技师表演者Bean,杂技师空中抛球(beanBags),抛的越多越厉害,默认有3个

package com.performer.service.impl;

import com.performer.service.Performer;

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("Juggler "+beanBags+" beanBags");
	}
}

 

3.配置文件Spring1.xml中加载Bean,实例化杂技师(Duke)

<?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">

<bean id="duke" class="com.performer.service.impl.Juggler">
</bean>
</beans>

 

4.加载配置文件Spring1.xml,测试Duke,输出他拥有的beanBags

package com.test;

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

import com.performer.service.impl.Juggler;

public class SpringTest {
	public static void main(String []args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring1.xml");
		Juggler juggle = (Juggler) context.getBean("duke");
		juggle.perform();
	}
}

 

5.测试结果,如下所示

 Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 

6.通过构造器,让Duke拥有15个beanBags,配置文件如下

<bean id="duke" class="com.performer.service.impl.Juggler">
	<constructor-arg value="15"></constructor-arg>
</bean>
</beans>

 

7.加载配置文件Spring2.xml,测试Duke,输出他拥有的beanBags 

public class SpringTest {
	public static void main(String []args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring2.xml");
		Juggler juggle = (Juggler) context.getBean("duke");
		juggle.perform();
	}
}

 

8.测试结果如下所示

Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 

现在创建一个会唱诗歌的杂技师

 

9.创建诗歌借口 

package com.poem.service;

public interface Poem {
	void recite();
}

 

10.创建一首诗 

package com.poem.service.impl;

import com.poem.service.Poem;

public class Sonnet implements Poem {
	Sonnet() {
	}
	
	private String[] s={"百日依山尽","黄河入海流","欲穷千里目","更上一层楼"};
	
	@Override
	public void recite() {
		for(int i=0;i<s.length;i++) {
			System.out.println(s[i]);
		}
	}
}

 

11.创建会朗诵诗歌的杂技师PoeticJuggler 

package com.performer.service.impl;

import com.poem.service.Poem;

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("while reciting...");
		poem.recite();
	}
}

 

12.将诗歌给Duke,让Duke成为PoeticJuggler,配置文件如下 

<bean id="duke" class="com.performer.service.impl.PoeticJuggler">
	<constructor-arg value="15"></constructor-arg>
	<constructor-arg ref="sonnet"></constructor-arg>
</bean>

<bean id="sonnet" class="com.poem.service.impl.Sonnet"></bean>

 

13.加载Spring3.xml,进行测试,查看结果Duke成为会诗歌的杂技师 

public class SpringTest {
	public static void main(String []args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring3.xml");
		Juggler juggle = (Juggler) context.getBean("duke");
		juggle.perform();
	}
}

 

14.运行结果如下所示

Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 

其它装配Bean的写法,如下

 

15.注入简单值,见Spring4.xml 

<bean id="jake" class="com.performer.service.impl.Instrumentalist">
	<property name="song" value="small star"></property>
</bean>

 

16.引用其他Bean,见Spring5.xml 

<bean id="jake" class="com.performer.service.impl.Instrumentalist">
	<property name="song" value="small star"></property>
	<property name="instrument" ref="piano"></property>
</bean>
<bean id="piano" class="com.instrument.service.impl.Piano"></bean> 

 

17.注入内部Bean,见Spring6.xml和Spring7.xml 

<bean id="jake" class="com.performer.service.impl.Instrumentalist">
	<property name="song" value="small star"></property>
	<property name="instrument">
		<bean class="com.instrument.service.impl.Saxophone"></bean>    
	</property>
</bean>
<bean id="saxophone" class="com.instrument.service.impl.Saxophone"></bean>

 

<bean id="duke" class="com.performer.service.impl.PoeticJuggler">
	<constructor-arg value="15"></constructor-arg>
	<constructor-arg >
		<bean class="com.poem.service.impl.Sonnet"></bean>
	</constructor-arg>
</bean>

 

18.使用Spring命名空间p装配属性Spring8.xml

Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 
 

19.装配集合

 

<bean id="hank" class="com.performer.service.impl.OneManBand">
	<property name="instruments" >
		<!-- 使用set -->
		<!-- <set>
			<ref bean="guitar"/>
			<ref bean="piano"/>
			<ref bean="saxophone"/>
		</set> -->
		
		<!-- 使用list -->
		<!-- <list>
			<ref bean="guitar"/>
			<ref bean="piano"/>
			<ref bean="saxophone"/>
		</list> -->
		
		<!-- 使用map -->
		<map>
			<entry key="GUITAR" value-ref="guitar"></entry>
			<entry key="PIANO" value-ref="piano"></entry>
			<entry key="SAXOPHONE" value-ref="saxophone"></entry>
		</map>  
		
		<!-- 使用props -->
	        <!-- <props>
			<prop key="GUITAR">wing wing wing...</prop>
			<prop key="PIANO">ding ding ding...</prop>
			<prop key="SAXOPHONE">plink plink plink...</prop>
		</props> -->
	</property>
</bean>

<bean id="guitar" class="com.instrument.service.impl.Guitar"></bean>

<bean id="saxophone" class="com.instrument.service.impl.Saxophone"></bean>

<bean id="piano" class="com.instrument.service.impl.Piano"></bean>

 

20.测试装配集合 

public class OneManBand implements Performer {
	public OneManBand() {
	}
	
	/*使用list*/
	
	private List<Instrument> instruments; 
	
	@Override
	public void perform() {
		for(Instrument instrument : instruments) {
			instrument.play();
		}
	}

	public List<Instrument> getInstruments() {
		return instruments;
	}

	public void setInstruments(List<Instrument> instruments) {
		this.instruments = instruments;
	}
	
	/*使用Map*/
	
    /*private Map<String,Instrument> instruments; 
	
	@Override
	public void perform() {
		for(String key : instruments.keySet()) {
			System.out.print(key+":");
			Instrument instrument = instruments.get(key);
			instrument.play();
		}
	}
	
	public Map<String,Instrument> getInstruments() {
		return instruments;
	}

	public void setInstruments(Map<String,Instrument> instruments) {
		this.instruments = instruments;
	}*/

	/*使用Collection*/
	
	/*private Collection<Instrument> instruments;  
	
	@Override
	public void perform() {
		for(Instrument instrument : instruments) {
			instrument.play();
		}
	}
	
	public Collection<Instrument> getInstruments() {
		return instruments;
	}
	
	public void setInstruments(Collection<Instrument> instruments) {
		this.instruments = instruments;
	}*/
}

 

21.运行结果如下所示

Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 
 

 

 

 

 

  • Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 
  • 大小: 274.8 KB
  • Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 
  • 大小: 926 Bytes
  • Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 
  • 大小: 753 Bytes
  • Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 
  • 大小: 2.3 KB
  • Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 
  • 大小: 71.1 KB
  • Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 
  • 大小: 71.1 KB
  • Spring Bean简单应用实例
            
    
    博客分类: Spring Spring Bean简单应用实例装配beanbean注入 
  • 大小: 2 KB