Spring实战第2.1声明Bean

2.1声明Bean

参加选秀大赛,在比赛中,我们需要一些参赛者,为此定义Performer接口,Performer.java

package com.springinaction.springidol;

public interface Performer {
	void perform();
}

2.1.1 创建Spring配置

Spring是一个基于容器的框架,如果你没有配置Spring,那它就是一个空容器,对我们毫无作用。需要配置Spring来告诉容器它需要加载哪些Bean和如何装配这些Bean,这样才能确保他们能够彼此协作。

Spring容器提供了两种配置Bean的方式。1,传统的XML文件配置方式;2,在3.4节讲解基于java注解的配置方式。下面是一个典型的Spring XML配置文件:

spring-idol.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"
    xmlns:p="http://www.springframework.org/schema/p"      
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--Bean declaration go here-->
<bean/>

2.1.2 声明一个简单Bean

其中一位参赛者是杂技师(Juggler),Juggler.java

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(){
		System.out.println("JUGGLING "+beanBags+" BEANBAGS");
	}
}

有请第一位选手Duke,来到舞台。Duke被定义为一个Spring Bean,并在Spring配置文件(spring-idol.xml)中进行声明,加入代码

<bean id="duke" 
	class="com.springinaction.springidol.PoeticJuggler" />

        <bean>元素是Spring中最基本的配置单元,通过该元素Spring将创建一个对象。这里创建了一个由Spring容器管理的名字为Duke的Bean。id属性定义了Bean的名字,也作为该Bean在Spring容器中的引用。你可以更具class属性得知,duke是一个Juggler。

      测试代码。为了给Duke一个排练机会,可以使用如下代码加载Spring上下文TestJuggler.java

package com.springinaction.springidol;

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

import cn.hh.ioc.UserService;

public class TestJuggler {
	@Test
	public void test(){
		//1 加载spring配置文件,根据创建对象
		ApplicationContext ctx = 
				new ClassPathXmlApplicationContext("com/springinaction/springidol/spring-idol.xml");
		//2 得到配置创建的对象
		Performer performer = (Performer) ctx.getBean("duke");
		performer.perform();
	}
}

运行结果:JUGGLING 3 BEANBAG

2.1.3 通过构造器注入

我们可以使用<constructor-arg>元素来告诉Spring额外的消息。在Spring配置文件spring-idol.xml中加入代码

<bean id="duke" 
	class="com.springinaction.springidol.PoeticJuggler">
	<constructor-arg value = "15" />
</bean> 

将<constructor-arg>的value属性设置为15,Spring将调用Juggler带一个int形参的构造函数,运行程序,显示结果:JUGGLING 15 BEANBAG

通过构造器注入对象引用

Duke不仅是杂技师,还会诗歌朗诵,我们需要定义一个新的Juggler类型,为PoeticJuggler.java

package com.springinaction.springidol;

public class PoeticJuggler extends Juggler{
	
	private Poem poem;//注入Poem
	
	public PoeticJuggler(Poem poem){
		super();
		this.poem = poem;
	}
	
	public PoeticJuggler(int beanBags, Poem poem){//注入豆袋子数量和Poem
		super();
		this.poem = poem;
	}
	
	public void perform(){
		super.perform();
		System.out.println("While reciting...");
		poem.recite();
	}
}

这个新类型Juggler还持有一个朗诵诗歌Poem的接口引用。我们定义一个朗诵诗歌的接口Poem.java

package com.springinaction.springidol;

public interface Poem {
	void recite();
}

Sonnet29类实现了Poem接口到底并定义了这首诗Sonnet29.java

package com.springinaction.springidol;

public class Sonnet29 implements Poem{
	
	private static String[] LINES ={
		"When, in disgrace with fortune and men's eyes,",
		"I all alone beweep my outcast state",
		"That then I scorn to change my state with kings."};
	
	public Sonnet29(){};
	
	public void recite(){
		for(int i = 0;i<LINES.length;i++){
			System.out.println(LINES[i]);
		}
	}
}

使用下面XML配置将Sonnet29声明为一个Spring<bean>,在Spring配置文件spring-idol.xml中加入代码

<bean id = "sonnet29" class = "com.springinaction.springidol.Sonnet29"/>

太繁琐了,不想写了。2019/1/16

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值