码迷,mamicode.com
首页 > 编程语言 > 详细

【Spring实战】—— 2 构造注入

时间:2015-01-24 17:13:53      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

本文讲解了构造注入以及spring的基本使用方式,通过一个杂技演员的例子,讲述了依赖注入属性或者对象的使用方法。

  如果想要使用spring来实现依赖注入,需要几个重要的步骤:

  1 定义主要的类和需要分离的属性。这里主要的类,是指程序的主要对象,在例子中是Juggler杂技员。而想要分离构造的属性,是它手中的袋子的数目beanBags。

  2 配置bean.xml。通过配置文件,确定主要的类和属性之间的关系,以及实现类。

  3 通过应用上下文,获取bean,并进行使用。

  简单的注入例子,一个表演者仍袋子的故事。

  实例代码:

  1 表演者接口:Performer.java

package com.spring.test.action1;

public interface Performer {
    void perform() throws PerformanceException;
}

  2 杂技员:Juggler,继承了表演者接口

package com.spring.test.action1;

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

}

  3 Spring配置文件:bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="duke" class="com.spring.test.action1.Juggler">
         <constructor-arg value="15"/>
     </bean>
</beans>

  4 应用上下文获取指定的bean

package com.spring.test.action1;

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

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        Performer performer = (Performer)ctx.getBean("duke");
        try {
            performer.perform();
        } catch (PerformanceException e) {
            e.printStackTrace();
        }
    }
}

  执行结果如下:

Juggler 15 beanBags

 

  当主要的类中依赖注入的不是属性,而是对象。

  1 例如,上面的杂技演员不仅仅会仍袋子,还会表演诗歌,那么诗歌这个对象就需要注入到表演者的构造函数中,可以如下表示会朗诵诗歌的杂技演员:PoeticJuggler

package com.spring.test.action1;

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() throws PerformanceException {
        super.perform();
        System.out.println("While reciting...");
        poem.recite();
    }

}

  2 诗歌对象:Sonnet29

package com.spring.test.action1;

public class Sonnet29 implements Poem{
    private static String lines = "嘛咪嘛咪哄";
    
    public Sonnet29() {
        
    }
    
    public void recite() {
        System.out.println(lines);
    }

}

  3 Bean.xml配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="duke" class="com.spring.test.action1.Juggler">
         <constructor-arg value="15"/>
     </bean>
     <bean id="sonnet29" class="com.spring.test.action1.Sonnet29"/>
     <bean id="poeticDuke" class="com.spring.test.action1.PoeticJuggler">
         <constructor-arg value="15"/>
         <constructor-arg ref="sonnet29"/>
     </bean>
</beans>

  4 主要的执行代码,通过应用上下文获取制定的bean

package com.spring.test.action1;

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

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
//        Performer performer = (Performer)ctx.getBean("duke");
        Performer performer1 = (Performer)ctx.getBean("poeticDuke");
        try {
//            performer.perform();
            performer1.perform();
        } catch (PerformanceException e) {
            e.printStackTrace();
        }
    }
}

  5 执行结果如下

一月 24, 2015 4:40:46 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@35bf8de1: startup date [Sat Jan 24 16:40:46 CST 2015]; root of context hierarchy
一月 24, 2015 4:40:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
一月 24, 2015 4:40:46 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3401a0ad: defining beans [duke,sonnet29,poeticDuke]; root of factory hierarchy
Juggler 15 beanBags
While reciting...
嘛咪嘛咪哄

 

【Spring实战】—— 2 构造注入

标签:

原文地址:http://www.cnblogs.com/xing901022/p/4246090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!