IoC 构造器注入

<!-- 声明TelePhone -->
<bean id="phone" class="model.TelePhone"><!-- index指定构造器的第几个参数,name指定参数名,value指定参数值 --><constructor-arg name="cpu" index="0" value="高通"></constructor-arg><constructor-arg name="ram" index="1" value="迅闪"></constructor-arg>
</bean>

测试方法:

    @org.junit.Testpublic void test5() {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");TelePhone t = ac.getBean("phone",TelePhone.class);t.show();}

bean对应的java类 TelePhone.java

/*** 手机类,模拟构造器注入* @author haifeng**/
public class TelePhone {private String cpu;private String ram;public TelePhone() {super();}public TelePhone(String cpu, String ram) {this.cpu = cpu;this.ram = ram;}public void show() {System.out.println( "TelePhone [cpu:" + cpu + ", ram:" + ram + "]");}
}

SET方式注入对象:
注入的对象需要在beans中声明,使用ref进行注入

<!-- 声明Computer -->
<bean id="student" class="model.Student"><property name="name" value="haydn"></property><property name="computer" ref="computer"></property><property name="phone" ref="phone"></property>
</bean>

测试方法:

    @org.junit.Testpublic void test6() {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");Student stu = ac.getBean("student",Student.class);stu.show();}

bean的对应JAVA类,Student.java

/*** student实体类,模拟注入类对象* @author haifeng**/
public class Student {private String name;private Computer computer;private TelePhone phone;public void setName(String name) {this.name = name;}public void setComputer(Computer computer) {this.computer = computer;}public void setPhone(TelePhone phone) {this.phone = phone;}public void show() {System.out.println("学生姓名:" + name);computer.show();phone.show();}}

List set map propetries 注入

<!-- 注入集合 --><bean id="message" class="bean.Message"><!-- 字符串注入null值,方式一:不写该配置,方式2:不写value,加个null标签 --><property name="name" ><null/></property><property name="age" value="23"></property><!-- 注入list --><property name="friends"><list><value>摩严</value><value>白子画</value><value>杀阡陌</value><value>花千骨</value><value>洛十一</value><value>糖宝</value></list></property><!-- 注入set --><property name="cities"><set><value>北京</value><value>上海</value></set></property><!-- 注入MAP --><property name="score"><map><entry key="语文" value="98"> </entry><entry key="数学" value="99"></entry><entry key="英语" value="100"></entry></map></property><!-- 注入配置文件 --><property name="dbParams"><props><prop key="url">localhost</prop><prop key="username">root</prop><prop key="password">123</prop></props></property><!-- 表达式注入,类似EL表达式用#{bean对象名.属性}标记, --><property name="password" value="#{dbProps.password}"></property></bean><!-- spring标签注入 ,其他bean中使用ref调用,set Map Propties都可以,使用时需要加入命名空间--><util:list id="someList"><value>摩严</value><value>白子画</value><value>杀阡陌</value><value>花千骨</value><value>洛十一</value><value>糖宝</value></util:list><!-- 注入list --><property name="friends" ref="someList"></property><!-- 加载properties文件创建Properties对象,location="classpath:dbcp.properties" location表示指定文件路径classpath表示绝对路径 --><util:properties id="dbProps" location="classpath:dbcp.properties"></util:properties>  

测试类JAVA代码:

@org.junit.Testpublic void test7() {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");Message msg = ac.getBean("message",Message.class);msg.show();}

bean的对应JAVA类,Message.java

/*** 消息类,模拟各种类型对象注入* @author haifeng**/
public class Message {private String name;private int age;private List<String> friends;private Set<String> cities;private Map<String,Integer> score;private Properties dbParams;private String password;public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}public void setFriends(List<String> friends) {this.friends = friends;}public void setCities(Set<String> cities) {this.cities = cities;}public void setScore(Map<String, Integer> score) {this.score = score;}public void setDbParams(Properties dbParams) {this.dbParams = dbParams;}public void setPassword(String password) {this.password = password;}public void show() {System.out.println( "Message [name:" + name + ", age:" + age +", friends"+friends.toString()+",cities:" +cities.toString()+",score:"+score.toString()+",properties:"+dbParams.toString()+"password:"+password+"]");}/*** map的遍历*/public void showMap(){Set<Entry<String,Integer>> map = score.entrySet();for(Entry<String,Integer> e : map) {System.out.println(e.getKey()+e.getValue());}}/*** protries的遍历*/public void showProp(){Set<Object> keys = dbParams.keySet();for(Object o : keys) {System.out.println(dbParams.get(o));}}}

自动装配(bean对象注入简配)
autowire:
byName:按名称匹配,beanid名与setXXX名字匹配植入,否则不注入
byType:按类型匹配,bean的class与类型待注入的bean属性类型一致时注入,否则不注入

<!-- 自动装配,autowire:byName根据名称,byType根据类型,不写默认会自动装配,但可读性很差 -->
<!-- 自动装配的bean的id需要和待注入的bean的属性name一致,注入的名字需要和set方法的名字一致(不含set,首字母小写) -->
<!-- byType和名字没关系,根据类型自动装配 -->
<!-- 但是byType的bean中有相同类型的bean出现则会出错,而且还要以单例模式声明要注入的bean -->
<bean id="student" class="model.Student" autowire="byName"><property name="name" value="haydn"></property>
</bean>

Spring IoC 详解(下篇)相关推荐

  1. Spring IoC详解

    Spring IoC详解 原文地址:Spring IoC详解 写在最前 本文将主要写Spring最核心的部分,为什么写这篇的原因也是因为在刚开始学习Spring的时候,学得太粗糙了.感觉学了个皮毛,从 ...

  2. Spring(2)——Spring IoC 详解

    Spring IoC 概述 IoC:Inverse of Control(控制反转) 读作"反转控制",更好理解,不是什么技术,而是一种设计思想,就是将原本在程序中手动创建对象的控 ...

  3. Spring IOC详解 以及 Bean生命周期详细过程 可以硬刚面试官的文章

    面试题 来自面试官发自灵魂深处的拷问:谈谈你对spring的理解: 一脸懵逼的求职者的内心活动:啥?  具体的问题是什么?现在的面试都不按套路出牌了吗?  抛出一个这么大的问题,你让我怎么回答? 一脸 ...

  4. Spring IOC详解与配置

    Spring: 框架: 具有一定功能的半成品软件,基于框架会节省开发成本 框架作用: 提高开发效率.复用性.编写规范.节约维护成本.解耦底层实现原理 Spring: Spring是分层的JavaSE/ ...

  5. Spring ioc 详解

    引述:IoC(控制反转:Inverse of Control)是Spring容器的内核,AOP.声明式事务等功能在此基础上开花结果.但是IoC这个重要的概念却比较晦涩隐讳,不容易让人望文生义,这不能不 ...

  6. Spring 体系结构详解

    Spring 体系结构详解 核心容器(Core Container) Core和Beans模块提供了Spring最基础的功能,提供IOC和依赖注入特性.这里的基础概念是BeanFactory,它提供对 ...

  7. Spring入门详解

    typora-copy-images-to: upload Spring入门详解 Spring框架是Java开发中最常用的框架,功能非常强大 源码下载:Spring Framework jar包.文档 ...

  8. Spring AOP详解(转载)所需要的包

    上一篇文章中,<Spring Aop详解(转载)>里的代码都可以运行,只是包比较多,中间缺少了几个相应的包,根据报错,几经百度搜索,终于补全了所有包. 截图如下: 在主测试类里面,有人怀疑 ...

  9. Spring JDBC详解

    <Spring JDBC详解> 本文旨在讲述Spring JDBC模块的用法.Spring JDBC模块是Spring框架的基础模块之一. 一.概述 在Spring JDBC模块中,所有的 ...

  10. WCF服务端运行时架构体系详解[下篇]

    作为WCF中一个核心概念,终结点在不同的语境中实际上指代不同的对象.站在服务描述的角度,我们所说的终结点实际上是指ServiceEndpoint对象.如果站在WCF服务端运行时框架来说,终结点实际上指 ...

最新文章

  1. Centos查找命令清单
  2. 解密Kernel:为什么适用任何机器学习算法?
  3. 分割BiSeNet笔记
  4. tensorflow2.0支持的python版本-TensorFlow 版本兼容性
  5. 【Demo】改变SO项目状态并取消拒绝原因实现
  6. 转:文件系统read,write缓存,有点意思
  7. phonegap html 缩放,phonegap常用事件总结(必看篇)
  8. mysql学习整理(一)
  9. python删除链表中重复的节点_删除链表中所有值与Python相同的节点的程序
  10. 游戏服务器停机维护,网络游戏是如何做到服务器不停机维护的?
  11. python开发k8s管理平台_运维开发和k8s运维如何选择,请各位大神指导一下?
  12. 进化算法求解TSP问题
  13. 进制转换(二进制、十进制、十六进制)
  14. java ftps_如何基于FTP4J实现FTPS连接过程解析
  15. Elasticsearch blocked by: [SERVICE_UNAVAILABLE/1/state not recovered / initialized];
  16. mysql 1114_ERROR 1114 (HY000): The table 'adv_date_tmp' is full(Mysql临时表应用)
  17. 英国AI创业公司Hazy获180万美元种子轮融资
  18. SCARA——OpenGL入门学习五六(三维变换、动画)
  19. 计算机软件著作权登记范文,计算机软件著作权登记申请表范本
  20. matlab 函数 平移,MATLAB图线先下平移

热门文章

  1. powerdesign 逆向工程
  2. 充电书库-study
  3. 计算机磁盘修复工具,CHKDSK磁盘修复工具使用教程
  4. 利用python暴力破解rar压缩文件密码
  5. google服务框架
  6. 数字中国城市巡礼之乌兰察布:红山口上的“草原硅谷”
  7. Northwind 数据库 多版本
  8. NorthWind 数据库整体关系
  9. ET Reporter
  10. 音响发烧友---HiFi音频功放