使用IDEA详解Spring中依赖注入的类型(上)
使用IDEA详解Spring中依赖注入的类型(上)
在Spring中实现IoC容器的方法是依赖注入,依赖注入的作用是在使用Spring框架创建对象时动态地将其所依赖的对象(例如属性值)注入Bean组件中。
Spring框架的依赖注入通常有两种实现方式,一种是使用构造方法注入,另一种是使用属性的setter方法注入。
使用构造方法注入
Spring框架可以采用Java反射机制,通过构造方法完成依赖注入。
创建项目及导入Maven模块过程请看《使用IDEA开发Spring入门程序》,在这就不赘述了。在这继续前面的项目,按照下面的步骤补充:
创建entity包,创建Person类
package entity;
public class Person {
private String name;
private String sex;
public Person() {
System.out.println("无参构造调用了...");
}
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
复制代码
构造方法注入方式一
编写配置文件
配置文件的具体代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring创建其实例-->
<!--
一个Bean标签可以注册一个组件(对象、类)
class:写要注册的组件的全类名
id:这个对象的唯一标识
-->
<bean id="test" class="dao.TestDaoImpl"/>
<bean id="person1" class="entity.Person"/>
<!--使用构造方法注入-->
<bean id="person2" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg name="name" value="泰斗贤若如"></constructor-arg>
<constructor-arg name="sex" value="男"></constructor-arg>
</bean>
</beans>
复制代码
在测试类TestDemo中测试
package test;
import dao.TestDao;
import entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
public class TestDemo {
@Test
public void test4(){
//初始化spring容器ApplicationContext,加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取test实例
Person person2 =(Person) applicationContext.getBean("person2");
System.out.println("姓名:"+person2.getName()+";"+"性别:"+person2.getSex());
}
}
复制代码
测试结果
构造方法注入方式二
编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring创建其实例-->
<!--
一个Bean标签可以注册一个组件(对象、类)
class:写要注册的组件的全类名
id:这个对象的唯一标识
-->
<bean id="test" class="dao.TestDaoImpl"/>
<bean id="person1" class="entity.Person"/>
<!--使用构造方法注入-->
<bean id="person2" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg name="name" value="泰斗贤若如"></constructor-arg>
<constructor-arg name="sex" value="男"></constructor-arg>
</bean>
<!--可以省略name属性,严格按照构造器参数的位置赋值-->
<bean id="person3" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg value="泰斗贤若如"></constructor-arg>
<constructor-arg value="男"></constructor-arg>
</bean>
</beans>
复制代码
在测试类TestDemo中测试
package test;
import dao.TestDao;
import entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
public class TestDemo {
@Test
public void test5(){
//初始化spring容器ApplicationContext,加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取test实例
Person person3 =(Person) applicationContext.getBean("person3");
System.out.println("姓名:"+person3.getName()+";"+"性别:"+person3.getSex());
}
}
复制代码
测试结果
需要注意的是,如果使用这种方法,要严格按照构造器参数的位置赋值,如果不这样赋值,当然也不会报错,但会造成赋值错乱,比如会把姓名赋值成性别,这当然是我们不愿意看到的,如果你非不按要求赋值(有点极端,皮一下),有种方法可以避免你赋值错乱,请看下面代码:
编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring创建其实例-->
<!--
一个Bean标签可以注册一个组件(对象、类)
class:写要注册的组件的全类名
id:这个对象的唯一标识
-->
<bean id="test" class="dao.TestDaoImpl"/>
<bean id="person1" class="entity.Person"/>
<!--使用构造方法注入-->
<bean id="person2" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg name="name" value="泰斗贤若如"></constructor-arg>
<constructor-arg name="sex" value="男"></constructor-arg>
</bean>
<!--可以省略name属性,严格按照构造器参数的位置赋值-->
<bean id="person3" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg value="泰斗贤若如"></constructor-arg>
<constructor-arg value="男"></constructor-arg>
</bean>
<bean id="person4" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<!--index="1",为参数指定索引,从0开始-->
<constructor-arg value="男" index="1"></constructor-arg>
<constructor-arg value="泰斗贤若如" ></constructor-arg>
</bean>
</beans>
复制代码
在测试类TestDemo中测试
package test;
import dao.TestDao;
import entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
public class TestDemo {
@Test
public void test5(){
//初始化spring容器ApplicationContext,加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取test实例
Person person4 =(Person) applicationContext.getBean("person4");
System.out.println("姓名:"+person4.getName()+";"+"性别:"+person4.getSex());
}
}
复制代码
测试结果
不要以为这样就完了,我在想,如果出现重载的情况,该如何办?且看我向下分解:
将entity包下的Person类修改如下
package entity;
public class Person {
private String name;
private String sex;
private Integer age;
private String email;
public Person() {
System.out.println("无参构造调用了...");
System.out.println("Person创建了...");
}
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
System.out.println("有参构造器");
}
public Person(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public Person(String name, String sex, String email) {
this.name = name;
this.sex = sex;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
复制代码
编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--将指定类TestDaoImpl配置给Spring,即注册一个TestDaoImpl对象,让Spring创建其实例-->
<!--
一个Bean标签可以注册一个组件(对象、类)
class:写要注册的组件的全类名
id:这个对象的唯一标识
-->
<bean id="test" class="dao.TestDaoImpl"/>
<bean id="person1" class="entity.Person"/>
<!--使用构造方法注入-->
<bean id="person2" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg name="name" value="泰斗贤若如"></constructor-arg>
<constructor-arg name="sex" value="男"></constructor-arg>
</bean>
<!--可以省略name属性,严格按照构造器参数的位置赋值-->
<bean id="person3" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<constructor-arg value="泰斗贤若如"></constructor-arg>
<constructor-arg value="男"></constructor-arg>
</bean>
<bean id="person4" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!-- public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}-->
<!--index="1",为参数指定索引,从0开始-->
<constructor-arg value="男" index="1"></constructor-arg>
<constructor-arg value="泰斗贤若如" ></constructor-arg>
</bean>
<bean id="person5" class="entity.Person">
<!--使用有参构造器进行创建对象并赋值-->
<!--public Person(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public Person(String name, String sex, String email) {
this.name = name;
this.sex = sex;
this.email = email;
} -->
<!--重载的情况下type可以指定参数的类型-->
<constructor-arg value="男" index="1"></constructor-arg>
<constructor-arg value="泰斗贤若如" index="0"></constructor-arg>
<constructor-arg value="22" index="2" type="java.lang.Integer"></constructor-arg>
</bean>
</beans>
复制代码
在测试类TestDemo中测试
package test;
import dao.TestDao;
import entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
public class TestDemo {
@Test
public void test6(){
//初始化spring容器ApplicationContext,加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取test实例
Person person5 =(Person) applicationContext.getBean("person5");
System.out.println("姓名:"+person5.getName()+";"+"性别:"+person5.getSex()+";"+"年龄:"+person5.getAge());
}
}
复制代码
测试结果
不过话又说过来了,明明name能搞定的事情弄这么复杂干嘛,所以常用的还是方式一
使用属性的setter方法注入
这部分放到下一篇讲解吧,篇幅有点多了,请持续关注!
使用IDEA详解Spring中依赖注入的类型(上)的更多相关文章
- 用IDEA详解Spring中的IoC和DI(挺透彻的,点进来看看吧)
用IDEA详解Spring中的IoC和DI 一.Spring IoC的基本概念 控制反转(IoC)是一个比较抽象的概念,它主要用来消减计算机程序的耦合问题,是Spring框架的核心.依赖注入(DI)是 ...
- 详解Spring中Bean的作用域与生命周期
摘要:在利用Spring进行IOC配置时,关于bean的配置和使用一直都是比较重要的一部分,同时如何合理的使用和创建bean对象,也是小伙伴们在学习和使用Spring时需要注意的部分,所以这一篇文章我 ...
- Spring中依赖注入的四种方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...
- spring中依赖注入
理解依赖注入:参考https://blog.csdn.net/taijianyu/article/details/2338311 一.依赖注入让bean与bean之间以配置文件组织在一起,而不是以硬编 ...
- spring中依赖注入与aop讲解
一.依赖注入 这个属于IOC依赖注入,也叫控制反转,IOC是说类的实例由容器产生,而不是我们用new的方式创建实例,控制端发生了改变所以叫控制反转. 1 2 3 4 5 6 7 8 9 10 11 1 ...
- 【Spring】详解Spring中Bean的加载
之前写过bean的解析,这篇来讲讲bean的加载,加载要比bean的解析复杂些,该文之前在小编原文中有发表过,要看原文的可以直接点击原文查看,从之前的例子开始,Spring中加载一个bean的方式: ...
- Spring点滴七:Spring中依赖注入(Dependency Injection:DI)
Spring机制中主要有两种依赖注入:Constructor-based Dependency Injection(基于构造方法依赖注入) 和 Setter-based Dependency Inje ...
- 详解Spring中的Profile
前言 由于在项目中使用Maven打包部署的时候,经常由于配置参数过多(比如Nginx服务器的信息.ZooKeeper的信息.数据库连接.Redis服务器地址等),导致实际现网的配置参数与测试服务器参数 ...
- Spring中依赖注入的使用和配置
使用方法1: //在执行此实例化的时候就会完成所有注入 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( ...
随机推荐
- DNS自述:我是如何为域名找到家的
对于互联网一代的我们,一出生就学会使用电脑.当我们对着浏览器地址栏输入www.baidu.com的时候,百度的首页就出现在面前.但你可曾想过,为什么我们输入www.baidu.com就可以弹出百度首页 ...
- Mysql 性能优化Explain详解
explain 功能我们在日常使用中,使用慢查询找到执行时间比较久的查询,然后使用SHOW STATUS.SHOW PROFILE.和explain做单条语句的分析.使用explain关键字可以模拟优 ...
- JVM之GC(一)
Java较C而言,最大的区别在于内存管理.JVM设有无用内存空间自动回收复用机制,也就是我们所说的GC. 之前说过,栈是为线程.为函数的执行分配内存的地方,用完即“销毁”,这里留待以后做深入探讨:堆是 ...
- 命令别名设定:alias,unalias 历史命令:history
1.别名设定举例 alias lm=‘ls -al | more’ 还可以取代现有指令 alias rm='rm -i' 查询现有别名 alias 取消别名 unalias lm 2.历史命令:his ...
- 曹工说Spring Boot源码(11)-- context:component-scan,你真的会用吗(这次来说说它的奇技淫巧)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- JS对JSON的使用【转】
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意味 ...
- IDEA需要修改的配置
自动编译开关 忽略大小写开关 智能导包开关 如下图所示,将 自动导入不明确的结构 智能优化包 这两个选项勾上.那么有什么效果呢? 你在代码中,只要敲list,就会出现提示,自动导入java.util. ...
- idea命令行、撤销commit
原文地址:https://blog.csdn.net/chzphoenix/article/details/38090349 近期在使用git,最开始在idea界面操作,后来要求用命令行.刚开始还不是 ...
- qt中的拖拽及其使用技巧
关于qt中的拖放操作,首先可以看这篇官方文档:http://doc.qt.io/qt-5.5/dnd.html 一.QDrag 首先是创建QDrag,可以在mousePressEvent或者mouse ...
- Irrelevant Elements UVA-1635 (二项式定理)
vjudge链接 原题链接 乍一看似乎没什么思路,但是写几个简单的例子之后规律就变得很明显. 比如当 n=5 时,每一步计算后的结果如下: a1 a1+a2 a1+2a2+a3 a1+3a2+3a3+ ...