Spring3实战第二章第二小节 IOC依赖注入 list和map集合
Spring有多种依赖注入的形式,本篇文章仅介绍Spring通过xml进行IOC配置的方式。
平常的Java开发中,程序员在某个类中需要依赖其它类的方法。
通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理。
Spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过Spring容器帮我们new指定实例并且将实例注入到需要该对象的类中。
依赖注入的另一种说法是"控制反转"。通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员。
而控制反转是指new实例工作不由我们程序员来做而是交给Spring容器来做。
构造器注入: 本文使用构造器注入和引用了一个bean
先写出需要引用的bean的接口
package com.spring;
public interface SpringAocInterface {
void show();
}
实现接口
package com.spring;
public class SpringAocInterfaceIm implements SpringAocInterface {
private final static String Lings = "最近的烦恼是小小的离别带来的寂寞,一瞬间的邂逅与分别,这一个一个的刹那,我想好好珍惜起来。";
@Override
public void show() {
System.out.println(Lings);
}
}
需要注入的类 (提供了一个String属性和一个对象 都是通过构造方法注入)
package com.spring; /**
* 使用构造方法
* @author Administrator
*
*/
public class SpringIoc {
private String Song; //构造方法注入 private SpringAocInterface springAocInterface; //构造方法引入对象注入 public SpringIoc(String song, SpringAocInterface springAocInterface) {
super();
Song = song;
this.springAocInterface = springAocInterface;
} public void show() {
System.out.println("构造方法传入的Song为 :" +Song);
System.out.print("构造方法引入的对象为:");
springAocInterface.show();
} }
xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 构造方法注入 -->
<bean id="SpringIoc" class="com.spring.SpringIoc">
<constructor-arg name="song" value="5"></constructor-arg> <!-- 给SpringIoc中的song属性注入-->
<constructor-arg name="springAocInterface" ref="SpringAocInterfaceIm"></constructor-arg><!-- 给SpringIoc中的springAocInterface 注入bean -->
</bean>
<bean id="SpringAocInterfaceIm" class="com.spring.SpringAocInterfaceIm"></bean>
<beans>
写一个测试类
package com.spring.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.SpringIoc;
public class SpringIocTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
SpringIoc bean = (SpringIoc) context.getBean("SpringIoc");
bean.show();
}
}
输出结果
十月 15, 2017 10:19:50 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e3bd51: startup date [Sun Oct 15 22:19:50 CST 2017]; root of context hierarchy
十月 15, 2017 10:19:50 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring.xml]
十月 15, 2017 10:19:50 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e40e825: defining beans [SpringIoc,SpringAocInterfaceIm,SpringSetIoc]; root of factory hierarchy
构造方法传入的Song为 :5
构造方法引入的对象为:最近的烦恼是小小的离别带来的寂寞,一瞬间的邂逅与分别,这一个一个的刹那,我想好好珍惜起来。
2:set注入(里面包括set注入 属性 list集合 map集合 引入bean)
package com.spring; import java.util.List;
import java.util.Map; /**
* Spring通过Set注入
* @author Administrator
*
*/
public class SpringSetIoc {
private String name; private int age; private SpringAocInterface like; private List<Object> list; private Map<String, Object> map ; public void show() {
System.out.println("通过set方法注入的姓名为:" + name);
System.out.println("通过set方法注入的年龄为:" + age);
System.out.print("通过set方法注入对象" + name + "最喜欢的一句话为:");
like.show();
System.out.print("通过set方法注入" + name + "最喜欢吃的水果为:");
for (Object object : list) {
System.out.print(object + ",");
}
System.out.println();
System.out.print("通过set方法注入map key为string : ");
for (String m : map.keySet()) {
System.out.print(map.get(m) + ",");
}
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public SpringAocInterface getLike() {
return like;
} public void setLike(SpringAocInterface like) {
this.like = like;
} public List<Object> getList() {
return list;
} public void setList(List<Object> list) {
this.list = list;
} public Map<String, Object> getMap() {
return map;
} public void setMap(Map<String, Object> map) {
this.map = map;
} }
xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="SpringAocInterfaceIm" class="com.spring.SpringAocInterfaceIm"></bean>
<!-- set注入 -->
<bean id="SpringSetIoc" class="com.spring.SpringSetIoc">
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
<!-- <property name="like" ref="SpringAocInterfaceIm"></property> --> <!-- 这个是注入公共bean 如果需要注入内部bean 并不想被共享可以使用下面的注入方式 -->
<property name="like">
<bean class="com.spring.SpringAocInterfaceIm"></bean>
</property>
<property name="list"> <!-- 这个是注入list集合 这个是注入的值 如果想注入bean的话 可以 写成 <ref bean = "bean的名称"></ref> -->
<list>
<value>香蕉</value>
<value>橘子</value>
<value>苹果</value>
</list>
</property>
<property name="map"><!-- map注入 -->
<map>
<entry key="男士" value="李四"></entry>
<entry key="男士" value="王五"></entry>
<entry key="女士" value="小花"></entry>
</map>
</property>
</bean>
<!-- spring还提供了P命名空间 减少了尖括号 使用p命名空间的时候 需要在上方引入xmlns:p="http://www.springframework.org/schema/p" 这句代码 不然会报错 两者方法取其一就好 我使用的上面的方式-->
<!-- <bean id="SpringSetIoc" class="com.spring.SpringSetIoc" p:name = "张三"
p:age = "18"
p:like-ref="SpringAocInterfaceIm"/> -->
</beans>
测试类
package com.spring.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.SpringSetIoc;
public class SpringSetIocTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
SpringSetIoc bean = (SpringSetIoc) context.getBean("SpringSetIoc");
bean.show();
}
}
输出结果
十月 15, 2017 10:26:14 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7f1633fd: startup date [Sun Oct 15 22:26:14 CST 2017]; root of context hierarchy
十月 15, 2017 10:26:15 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring.xml]
十月 15, 2017 10:26:15 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@741c42a9: defining beans [SpringIoc,SpringAocInterfaceIm,SpringSetIoc]; root of factory hierarchy
通过set方法注入的姓名为:张三
通过set方法注入的年龄为:18
通过set方法注入对象张三最喜欢的一句话为:最近的烦恼是小小的离别带来的寂寞,一瞬间的邂逅与分别,这一个一个的刹那,我想好好珍惜起来。
通过set方法注入张三最喜欢吃的水果为:香蕉,橘子,苹果,
通过set方法注入map key为string : 王五,小花,
最后奉上完整版的xml配置文件 包含构造注入和set注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 构造方法注入 -->
<bean id="SpringIoc" class="com.spring.SpringIoc">
<constructor-arg name="song" value="5"></constructor-arg> <!-- 给SpringIoc中的song属性注入-->
<constructor-arg name="springAocInterface" ref="SpringAocInterfaceIm"></constructor-arg><!-- 给SpringIoc中的springAocInterface 注入bean -->
</bean>
<bean id="SpringAocInterfaceIm" class="com.spring.SpringAocInterfaceIm"></bean>
<!-- set注入 -->
<bean id="SpringSetIoc" class="com.spring.SpringSetIoc">
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
<!-- <property name="like" ref="SpringAocInterfaceIm"></property> --> <!-- 这个是注入公共bean 如果需要注入内部bean 并不想被共享可以使用下面的注入方式 -->
<property name="like">
<bean class="com.spring.SpringAocInterfaceIm"></bean>
</property>
<property name="list"> <!-- 这个是注入list集合 这个是注入的值 如果想注入bean的话 可以 写成 <ref bean = "bean的名称"></ref> -->
<list>
<value>香蕉</value>
<value>橘子</value>
<value>苹果</value>
</list>
</property>
<property name="map"><!-- map注入 -->
<map>
<entry key="男士" value="李四"></entry>
<entry key="男士" value="王五"></entry>
<entry key="女士" value="小花"></entry>
</map>
</property>
</bean>
<!-- spring还提供了P命名空间 减少了尖括号 使用p命名空间的时候 需要在上方引入xmlns:p="http://www.springframework.org/schema/p" 这句代码 不然会报错 -->
<!-- <bean id="SpringSetIoc" class="com.spring.SpringSetIoc" p:name = "张三"
p:age = "18"
p:like-ref="SpringAocInterfaceIm"/> -->
</beans>
Spring3实战第二章第二小节 IOC依赖注入 list和map集合的更多相关文章
- Ionic 入门与实战之第二章第二节:Ionic 环境搭建之 Ionic Lab 使用
原文发表于我的技术博客 本文是「Ionic 入门与实战」系列连载的第二章第二节,主要对 Ionic Lab 工具作了介绍,并讲解了其使用方法,这也是一个开发 Ionic 比较好的调试工具. 原文发表于 ...
- 浅谈(IOC)依赖注入与控制反转(DI)
前言:参考了百度文献和https://www.cnblogs.com/liuqifeng/p/11077592.html以及http://www.cnblogs.com/leoo2sk/archive ...
- ASP.NET MVC IOC依赖注入之Autofac系列(二)- WebForm当中应用
上一章主要介绍了Autofac在MVC当中的具体应用,本章将继续简单的介绍下Autofac在普通的WebForm当中的使用. PS:目前本人还不知道WebForm页面的构造函数要如何注入,以下在Web ...
- ASP.NET MVC IOC依赖注入之Autofac系列(一)- MVC当中应用
话不多说,直入主题看我们的解决方案结构: 分别对上面的工程进行简单的说明: 1.TianYa.DotNetShare.Model:为demo的实体层 2.TianYa.DotNetShare.Repo ...
- IOC依赖注入简单实例
转自:http://hi.baidu.com/xyz136299110/item/a32be4269e9d0c55c38d59e6 相信大家看过相当多的IOC依赖注入的例子. 但大家在没有明白原理的情 ...
- IoC 依赖注入容器 Unity
原文:IoC 依赖注入容器 Unity IoC 是什么? 在软件工程领域,“控制反转(Inversion of Control,缩写为IoC)”是一种编程技术,表述在面向对象编程中,可描述为在编译时静 ...
- springboot启动流程(九)ioc依赖注入
所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 在前面的几篇文章中,我们多次提到这么一个转化过程: Bean配置 --> Bean ...
- Spring-DI控制反转和IOC依赖注入
Spring-DI控制反转和IOC依赖注入 DI控制反转实例 IDEAJ自动导入Spring框架 创建UserDao.java接口 public interface UserDao { public ...
- Spring学习-spring核心机制-IOC依赖注入
转载自:http://www.cnblogs.com/chenssy/archive/2012/11/11/2765266.html 今天复习一下spring两大特性之一:IOC依赖注入,看了一下大佬 ...
随机推荐
- ios模拟器安装测试包方法
1. 使用ios-sim ios-sim是一个可以用命令空着ios模拟器的工具.利用这个命令,我们可以启动模拟器.安装app.启动app.查询ios sdk,它可以使我们像自动化测试一样打开xcode ...
- python学习 条件控制
if - else if 条件a : 内容a else : 内容 非a if - elif - else if 条件a : 内容a elif 条件b: 内容b-a else : 内容 非(a∪b) ...
- 前端中用到的图片(png图片)
作为前端工程师,将设计师的设计稿转化为html页面,其中有一个必不可少的环节就是将psd文件中的一些图片随心所欲的使用,而我们经常用到的就是png图片. 第一部分:基本介绍 首先我们先对比几种图片: ...
- springboot整合jsp踩坑
springboot以其高效的开发效率越来越多的用在中小项目的开发,并且在分布式开发中的使用也很广泛,springboot官方推荐的前端框架却是thymeleaf,并且默认不支持jsp,而大部分jav ...
- 怎么高效查找和正确改变谷歌浏览器编码格式(新版和旧版Google Chrome)(图文详解)
前言 今天,无意中在解决一个乱码问题,后台是有过判断解决兼容性问题,但是有个别电脑还是有乱码问题,就去想改变下前台的编码格式,突然发现一向好用的谷歌,居然找不到编码格式了! 上网百度了半天,查阅各种网 ...
- WINDOWS安装mysql5.7.20
MSI安装包链接 http://pan.baidu.com/s/1mhI0SMO 提取密码 gaqu 安装前要把老版本的MYSQL卸载干净 之前用官网的archive免安装版安装一直失败,放弃,用MS ...
- 在ASPNETCORE中获得所有Action
在ASPNETCORE中获得所有Action 本文旨在记录自己在aspnetcore工作中需要获取所有Action,在查询了资料后进行了几种方法的记录.后期有发现其它方式再进行追加. 一.通过 反射 ...
- Firebird SEQUENCE
Firebird3 以后可以有自增列,也可以类似Oracle.Postgresql手动添加序列,产生新值,灵活操作. 创建序列: INCREMENT ; 修改序列最大值: ; 产生新值: 1. ) f ...
- spring web.xml基本配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...
- css三栏布局方案整理
日常开发中,经常会用到css三栏布局,现将工作中常用的css 三栏布局整理如下: 什么是三栏布局: 三栏布局,顾名思义就是两边固定,中间自适应. 一. float布局 <!DOCTYPE htm ...