Spring-IOC(DI)的三种注入方式
spring为方便不同的需求,为我们提供了3中不同的注入方式分别是set、get方法注入,构造注入还有p命名空间注入,老规矩,直接上代码
首先创建实体类Student
public class Student {
private String name;
private int age; 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;
} //重写toString方法,方便测试
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
第一种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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--set、get注入-->
<bean id="student" class="com.lhf.entity.Student">
<property name="name" value="张三"/>
<property name="age" value="16"/>
</bean>
</beans>
测试类:
public class App
{
public static void main( String[] args )
{
//spring读取xml文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从spring容器中获取service层的对象
Student bean = (Student) context.getBean("student");
//输出
System.out.println(bean);
}
}
第二种,构造注入
创建实体类,并写出成员的构造方法,这里建议将该类的无参构造也写上
public class Student {
private String name;
private int age; public Student() {
} public Student(String name, int age) {
this.name = name;
this.age = age;
} //重写toString方法,方便测试
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
配置文件:(在进行构造注入的时候,我们需要按照参数的顺序逐个进行注入,否则回报错。在不同的场合我们需要进行的参数赋值也是不同的,所以构造注入也是有局限性的,至于构造注入那一定也是有他的好处的,比如在对象实例化的时候我们就可以将参数的赋值,效率高)
<bean id="student" class="com.lhf.entity.Student">
<!--可以通过参数类型进行注入-->
<constructor-arg type="java.lang.String" value="张三"/>
<!--也可以通过参数在构造方法中的位置进行注入-->
<constructor-arg index="1" value="16"/>
</bean>
第三种,p命名空间:(这种注入说白了就是set注入的简化,也就是说,我们还需要重写set方法,这里我就不写了)
<bean id="student" class="com.lhf.entity.Student" p:name="张三" p:age="16"/>
以上三种方法测试类相同,这里就投个懒只写一次,至于结果自行运行
以下是数组和集合的注入方式
实体类:
public class Student {
private Map<String,Integer> map;//map集合注入
private Set<String> set;//set集合的注入
private List<String>lists;//list集合注入
private Properties properties;//properties注入 public void setMap(Map<String, Integer> map) {
this.map = map;
} public void setSet(Set<String> set) {
this.set = set;
} public void setLists(List<String> lists) {
this.lists = lists;
} public void setProperties(Properties properties) {
this.properties = properties;
}
//重写toString方法,方便测试 @Override
public String toString() {
return "Student{" +
"map=" + map +
", set=" + set +
", lists=" + lists +
", properties=" + properties +
'}';
}
}
配置文件
<bean id="student" class="com.lhf.entity.Student">
<property name="lists">
<list>
<value>张三</value>
<value>李四</value>
</list>
</property>
<property name="map">
<map>
<entry key="张三" value="16"></entry>
<entry key="李四" value="18"></entry>
</map>
</property>
<property name="set">
<set>
<value>张三</value>
<value>李四</value>
</set>
</property>
<property name="properties">
<props>
<prop key="张三">22</prop>
<prop key="李四">66</prop>
</props>
</property>
</bean>
Spring-IOC(DI)的三种注入方式的更多相关文章
- Spring IOC 中三种注入方式
项目错误知识点记录 正文 最近在项目的时候,用到Spring框架,Spring框架提供了一种IOC的自动注入功能,可以很轻松的帮助我们创建一个Bean,这样就省的我们四处写new Object()这样 ...
- spring ioc三种注入方式
spring ioc三种注入方式 IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转 什么是控制反转? 控制反转是一种将组件依赖关系的创建和管理置于程序外部的技术. 由容 ...
- Spring IOC 三种注入方式
1. 接口注入 2. setter注入 3. 构造器注入 对象与对象之间的关系可以简单的理解为对象之间的依赖关系:A类需要B类的一个实例来进行某些操作,比如在A类的方法中需要调用B类 ...
- Spring IOC以及三种注入方式
IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...
- Spring学习日记01_IOC_xml的三种注入方式
什么是IOC 控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理 使用IOC目的:为了耦合度降低 做入门案例就是IOC实现 IOC底层原理 xml解析 工厂模式 反射 原始方式 cla ...
- .NetCore中三种注入方式的思考
该篇内容由个人博客点击跳转同步更新!转载请注明出处! .NetCore彻底诠释了"万物皆可注入"这句话的含义,在.NetCore中到处可见注入的使用.因此core中也提供了三种注入 ...
- Spring IOC 三种注入方式(构造器,setter,接口)
Spring的依赖注入有三种方式: 1,构造器注入 2,setter注入 3,接口注入 下面我给大家介绍一下这三种注入 1.构造器注入 构造器注入主要是依赖于构造方法去实现,构造方法可以是有参也可以是 ...
- Spring的三种注入方式(Setter、构造函数和自动注入)
一.Setter注入 这里我是希望在Student.java中调用Course.java中的内容. public class Course { public String name = "数 ...
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)(摘抄)
IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转, Spring 框架的核心基于控制反转原理. 什么是控制反转?控制反转是一种将组件依赖关系的创建和管理置于程序外部的技 ...
- Spring学习笔记(6)——IoC的三种注入方式
1.接口注入(不推荐) 2.构造器注入(死的应用) 3.getter,setter方式注入(比较常用) Type1 接口注入 我们常常借助接口来将调用者与实现者分离.如: public class C ...
随机推荐
- malloc函数、calloc函数和free函数
malloc函数和free函数 malloc函数原型:void *malloc(long NumBytes) malloc原型说明:mallco函数在堆分配了NumBytes个字节的内存空间,用来存放 ...
- Hadoop的伪分布式安装和部署流程
在opt目录创建install software test other四个目录 /opt/installed #安装包/opt/software #软件包/opt/other #其他/opt/test ...
- CentOS系统安装过程中配置软RAID-0或RAID-1
什么是RAID-0 RAID-0 (等量模式, stripe):效能最佳.这种模式如果使用相同型号与容量的磁碟来组成时,效果较佳.这种模式的 RAID 会将磁碟先切出等量的区块 (举例来说, 4KB) ...
- 86.QuerySet API常用的方法详解:get方法
get方法的查询条件只能有一条数据满足,如果匹配到多条数据都满足,就会报错:如果没有匹配到满足条件的数据,也会报错. 示例代码如下: from django.http import HttpRespo ...
- Juniper srx新增接口IP,使PC直连srx(转)
转自:https://www.jianshu.com/p/bc27134bde3d Juniper srx新增接口IP,使PC直连srx 2018.11.19 14:24:15字数 424 概述 需求 ...
- Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)
C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...
- PAT甲级——1073 Scientific Notation (20分)
Scientific notation is the way that scientists easily handle very large numbers or very small number ...
- Python语言学习:字典常用的方法
1. 增加:字典[key]=value(不存在的key和value) info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' ...
- 箭头函数,闭包函数中的this指向
在javscript中,this 是在函数运行时自动生成的一个内部指针,它指向函数的调用者. 箭头函数有些不同,它的this是继承而来, 默认指向在定义它时所处的对象(宿主对象),而不是执行时的对象. ...
- ZJNU 1367 - Party--中高级
寻找从i到X,再从X到i的最短路 可以在正向图中从X开始跑一遍最短路,每个点的距离dis1[i]当作从X回到点i的距离 再将图反向从X再跑一遍,每个点的距离dis2[i]当作从i到点X的距离 最后搜索 ...