2019独角兽企业重金招聘Python工程师标准>>>

本文讲解了构造注入以及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/beanshttp://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/beanshttp://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...
嘛咪嘛咪哄

转载于:https://my.oschina.net/u/204616/blog/545195

【Spring实战】—— 2 构造注入相关推荐

  1. 【Spring 工厂】注入详解 — Set注入(JDK内置类型,用户自定义类型)、构造注入(重载)

    Spring 注入(Injection) 什么是注入? 为什么要注入? 如何进行注入[开发步骤] Spring注入的原理分析(简易版) Set注入详解 JDK内置类型 String+8种基本类型 数组 ...

  2. [Spring实战系列](8)Spring注入方式之setter注入

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/50631178 通常,JavaBean 的属性 ...

  3. Spring构造注入

    构造注入 注入:通过 Spring 的配置文件,为成员变量赋值: Set注入:Spring 调用 Set 方法 通过 配置文件 为成员变量赋值: 构造注入:Spring 调用 构造方法 通过 配置文件 ...

  4. Spring实战——无需一行xml配置实现自动化注入

    已经想不起来上一次买技术相关的书是什么时候了,一直以来都习惯性的下载一份电子档看看.显然,如果不是基于强烈的需求或强大的动力鞭策下,大部分的书籍也都只是蜻蜓点水,浮光掠影. 就像有位同事说的一样,有些 ...

  5. Spring的构造注入

    1.开发步骤 2.构造方法的重载 2.1参数个数不同 2.2构造参数个数相同时 3.注入总结 注入:通过Spring的配置文件,为成员变量赋值 Set注入:Spring调用Set方法,通过配置文件,为 ...

  6. Spring常见错误 - Bean构造注入报空指针异常

    Spring常见错误 - Bean构造注入报空指针异常 前言 一. 构造器内报NPE 1.1 案例 1.2 原理分析 1.2.1 空指针发生在哪一个阶段? 1.2.2 studentService字段 ...

  7. 【Spring实战】注入非Spring Bean对象

    2019独角兽企业重金招聘Python工程师标准>>> 大家经常用到Spring IOC去管理对象之间的依赖关系,但一般情况下都有一个前提:这些Bean对象必须是通过Spring容器 ...

  8. spring容器的设值注入和构造注入

    例如我们现在有一个Computer类: public class Computer {private String cpu;private String hdd;//硬盘private String ...

  9. 《spring实战第四版》的读书笔记

    <spring实战第四版>的读书笔记 1 概述 <Spring实战第四版>描述了Spring4架构的设计,看完了以后,最大感觉是Spring的IOC与aop理念实在是太强大了, ...

最新文章

  1. 来客推开源商城与你浅析:B2B2C多商户商城系统
  2. python中类的嵌套_python中的嵌套类 | 学步园
  3. 移动应用开发过程中的迭代式原型设计
  4. 微信跳一跳高分系列一:解读 adb 工具
  5. 快速排序 java导包_排序算法-快速排序(Java实现)
  6. 阿里云+wordpress搭建个人网站及博客
  7. c语言程序设计单元小测,C语言程序设计单元小测2.doc
  8. 推荐一本学javascript的书籍---经典
  9. js识别用户设备是移动端手机时跳转到手机网站
  10. 苹果亮度自动调节怎么关闭_iPhone 总是自动亮屏,该怎么关闭?
  11. 利用ENVI绘制土地利用图
  12. 常见计算机英语词汇翻译,常见计算机英语词汇翻译_0.doc
  13. vhdl8三种方式实现38译码器
  14. wifi mouse hd for linux,wifi mouse hd客户端PC版下载_wifi mouse hd客户端PC版官方下载-太平洋下载中心...
  15. Java方法及方法的重载
  16. mysql 批量执行sql语句_MySQL中批量执行SQL语句
  17. 计算机中guest用户是灰的,来宾帐户状态不适用呈灰色状
  18. 尚硅谷YYDS (课件资料)
  19. 用树莓派搜寻地外文明
  20. matlab计算惯性矩,梁单元有限元计算程序(matlab)

热门文章

  1. 苹果Mac系统无法输入密码怎样解决
  2. 苹果Mac系统如何安装pip命令?
  3. 著名外企“卸磨杀驴”案例剖析
  4. 联想凌拓官宣:陆大昕为首席执行官,“掌舵人”就此诞生!
  5. 计算机软件技术发展的利弊分析,信息技术发展的利弊
  6. CY4500PD协议分析仪软件安装问题
  7. charts框架 横向 纵向柱状图
  8. 长期总结:关于计算为什么不稳定
  9. 酷我音乐盒2013 v7.6.0.2 官方免费版
  10. 米家扫地机器人按键没反应_米家扫地机器人如何恢复出厂设置