您的位置:首页 > 编程语言 > Java开发

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

2017-08-06 08:42 393 查看
一、配置文件

<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();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring java