Spring 依赖注入——构造器注入
2017-08-06 08:42
393 查看
一、配置文件
二、配置javaBean
三、测试类
<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第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入
- Spring-依赖注入之构造器方式注入
- (6) Spring 如何装配 集合类 以及 【第二种 依赖注入 方式】使用 构造器 注入
- (5) Spring 如何装配 集合类 以及 【第二种 依赖注入 方式】使用 构造器 注入
- Spring依赖注入 — 构造器注入
- Spring的依赖注入 引用注入 构造器注入
- spring四种依赖注入方式 (set,构造器,工厂,注解 )
- spring 使用构造器方式依赖注入
- Spring依赖注入 — 构造器注入
- Spring依赖注入方式_属性setter方法注入_构造器注入_注解注入
- 使用Spring(三)依赖注入DI(Setter DI,构造器注入)
- Spring的核心技术(五)---基于构造器的依赖注入
- spring四种依赖注入方式
- Spring 依赖注入
- Spring依赖注入和控制反转详解
- spring四种依赖注入方式
- Spring各种依赖注入注解的区别
- spring四种依赖注入方式
- Spring系列之-Spring IOC容器设计:依赖注入设计
- Spring依赖注入类型