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"
       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
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <bean id="blankDisc" class="com.hk.BlankDisc" p:title="aaaa" p:artist="cccc"/>

</beans>

 

二、JAVA类

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 void setTitle(String title) {
        this.title = title;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

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

}

 

三、测试类

package com.hk;

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

/**
 * 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 13:34  拓仲  阅读(123)  评论(0编辑  收藏  举报