Spring 依赖注入——构造器注入

一、配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">

    <bean id="blankDisc" class="com.hk.BlankDisc" c:_0="aaaa" c:_1="111"/>

</beans>

 

二、配置javaBean

package com.hk;

/**
* User: hk
* Date: 2017/8/5 下午8:00
* version: 1.0
*/
public interface CompactDisc {
void play();
}
package com.hk;

/**
 * User: hk
 * Date: 2017/8/5 下午8:46
 * version: 1.0
 */
public class BlankDisc implements CompactDisc {

    private String title;
    private String artist;

    public BlankDisc(String title, String artist) {
        this.title = title;
        this.artist = artist;
    }

    public void play() {
        System.out.println("title:" + title + ",artist:" + artist);
    }

}

 

三、测试类

package com.hk;

import javafx.application.Application;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{public static void main( String[] args )
    {
        ApplicationContext application = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        CompactDisc c = (CompactDisc) application.getBean("blankDisc");
        c.play();


    }
}

 

posted @ 2017-08-06 08:30  拓仲  阅读(483)  评论(0编辑  收藏  举报