6、依赖注入

6.1、构造器注入

前面已经说过了

6.2、Set方式注入【重点】

  • 依赖注入:Set注入

    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入

【环境搭建】

1.复杂类型

public class Address {private String address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}

2.真实测试对象

public class Student {private String name;private Address address;private String[] books;private List<String> hobbys;private Map<String,String> card;private Set<String> games;private String wife;private Properties info;//... set/get/toString}

3.beans.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="student" class="com.gongyi.pojo.Student"><!--第一种,普通值注入,value--><property name="name" value="工一"/></bean>
</beans>

4.测试类:

public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Student student = (Student) context.getBean("student");System.out.println(student.getName());System.out.println(student.getAddress());}
}

完善注入信息

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="address" class="com.gongyi.pojo.Address"><property name="address" value="北京"/></bean><bean id="student" class="com.gongyi.pojo.Student"><!--第一种,普通值注入,value--><property name="name" value="工一"/><!--第二种,Bean注入,ref--><property name="address" ref="address"/><!--数组--><property name="books"><array><value>红楼梦</value><value>西游记</value><value>水浒传</value><value>三国演义</value></array></property><!--List--><property name="hobbys"><list><value>听歌</value><value>敲代码</value><value>看电影</value></list></property><!-- Map--><property name="card"><map><entry key="身份证" value="123456"/><entry key="银行卡" value="123456"/></map></property><!--Set--><property name="games"><set><value>LOL</value><value>COC</value><value>BOB</value></set></property><!--null--><property name="wife"><null/></property><!--Properties--><property name="info"><props><prop key="dirver">123456</prop><prop key="url">男</prop><prop key="username">gongyi</prop><prop key="password">123456</prop></props></property></bean>
</beans>

6.3、拓展方式注入

我们可以使用p命名空间和c命名空间进行注入

官方解释:

使用:

<?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:p="http://www.springframework.org/schema/p"xmlns:c="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--p命名空间注入,可以直接注入属性的值:properties--><bean id="user" class="com.gongyi.pojo.User" p:name="工一" p:age="22"/><!--c命名空间注入,通过构造器注入:construct-args--><bean id="user2" class="com.gongyi.pojo.User" c:name="木子" c:age="22"/>
</beans>

在父模块引入junit包依赖:

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version>
</dependency>

测试:

@Test
public void test2() {ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");User user = context.getBean("user2", User.class);System.out.println(user);}

注意点:p命名空间和c命名空间不能直接使用,需要导入xml约束

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

6.4、bean的作用域

1.单例模式(Spring默认机制)

<bean id="user2" class="com.gongyi.pojo.User" c:name="木子" c:age="22" scope="singleton"/>

官网解释:

2.原型模式:每次从容器中get的时候,都会产生一个新对象

<bean id="user3" class="com.gongyi.pojo.User" c:name="木子" c:age="22" scope="prototype"/>

官网解释:

3.其余的request,session,application,这些只能在web开发中使用到

代码show

代码结构图:

1.新建模块:spring-04-di

2.新建pojo包及类

Address.java

public class Address {private String address;//set/get/toString
}

Student.java

public class Student {private String name;private Address address;private String[] books;private List<String> hobbys;private Map<String,String> card;private Set<String> games;private String wife;private Properties info;//set/get/toString
}

User.java

public class User {private String name;private int age;public User() {}public User(String name, int age) {this.name = name;this.age = age;}//set/get/toString
}

3.新建资源文件:

beans.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="address" class="com.gongyi.pojo.Address"><property name="address" value="北京"/></bean><bean id="student" class="com.gongyi.pojo.Student"><!--第一种,普通值注入,value--><property name="name" value="工一"/><!--第二种,Bean注入,ref--><property name="address" ref="address"/><!--数组--><property name="books"><array><value>红楼梦</value><value>西游记</value><value>水浒传</value><value>三国演义</value></array></property><!--List--><property name="hobbys"><list><value>听歌</value><value>敲代码</value><value>看电影</value></list></property><!-- Map--><property name="card"><map><entry key="身份证" value="123456"/><entry key="银行卡" value="123456"/></map></property><!--Set--><property name="games"><set><value>LOL</value><value>COC</value><value>BOB</value></set></property><!--null--><property name="wife"><null/></property><!--Properties--><property name="info"><props><prop key="dirver">123456</prop><prop key="url">男</prop><prop key="username">gongyi</prop><prop key="password">123456</prop></props></property></bean>
</beans>

userbean.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:p="http://www.springframework.org/schema/p"xmlns:c="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--p命名空间注入,可以直接注入属性的值:properties--><bean id="user" class="com.gongyi.pojo.User" p:name="工一" p:age="22"/><!--c命名空间注入,通过构造器注入:construct-args--><bean id="user2" class="com.gongyi.pojo.User" c:name="木子" c:age="22" scope="singleton"/><bean id="user3" class="com.gongyi.pojo.User" c:name="木子" c:age="22" scope="prototype"/>
</beans>

4.新建测试类:

public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Student student = (Student) context.getBean("student");System.out.println(student.getName());System.out.println(student.getAddress());System.out.println(student);System.out.println(student.toString());/*** Student{* name='工一',* address=Address{address='北京'},* books=[红楼梦, 西游记, 水浒传, 三国演义],* hobbys=[听歌, 敲代码, 看电影],* card={身份证=123456, 银行卡=123456},* games=[LOL, COC, BOB], wife='null',* info={password=123456, dirver=123456, url=男, username=gongyi}* }*/}@Testpublic void test2() {ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");User user = context.getBean("user2", User.class);System.out.println(user);User user2 = context.getBean("user2", User.class);System.out.println(user==user2);User user3 = context.getBean("user3", User.class);User user4 = context.getBean("user3", User.class);System.out.println(user3==user4);System.out.println(user3.hashCode());System.out.println(user4.hashCode());}}

彩蛋

1.CPX输入自动提示:

2.依赖注入相关官方文档地址

3.idea同时看两个文件(左右对比)

4.查看properties子标签

5.技巧:

看spring等纯英文的官方文档时,英文看不懂时,可以先关注页面的代码【类似于当时看英文版的Thinking In Java的感觉】

6.判断两个对象是否相等,直接比较hashCode即可【直接相等比较,其实也是比较的hashCode】

7.本文章代码地址

6、Spring之依赖注入相关推荐

  1. factorybean 代理类不能按照类型注入_《Spring入门经典》:使用Spring进行依赖注入

    第二章:使用Spring进行依赖注入 重点:配置并使用Spring容器 使用不同类型的配置元数据来配置Spring容器 理解依赖解析 了解自动装配的优缺点 在容器中执行显式Bean查找 学习不同的Be ...

  2. 在ABAP里模拟实现Java Spring的依赖注入

    Dependency Injection- 依赖注入,在Java Spring框架中有着广泛地应用.通过依赖注入,我们不必在应用代码里繁琐地初始化依赖的资源,非常方便. 那么ABAP能否从语言层面上也 ...

  3. Spring Setter依赖注入示例

    学习如何编写Spring Setter依赖注入示例 . Setter注入是Spring依赖注入的一种 . Spring支持字段注入,Setter注入以及构造函数注入,以将依赖项注入Spring托管的b ...

  4. Spring字段依赖注入示例

    学习如何编写Spring Field Injection示例 . 字段注入是Spring框架 依赖注入的一种 . 在本教程中,我们将编写几个类,并看一看现场注入工程. 有关Spring依赖注入的更多信 ...

  5. 据说,80%的人没有真正理解了Spring的依赖注入

    前言 提起Spring,大家肯定不陌生,它是每一个Java开发者绕不过去的坎.Spring 框架为基于 java 的企业应用程序提供了一整套解决方案,方便开发人员在框架基础快速进行业务开发. 在官网中 ...

  6. Spring框架----Spring的依赖注入

    1.spring的依赖注入的概念 依赖注入:dependency Injection IOC的作用:降低程序之间的依赖关系,但不是消除. 依赖关系的管理:以后都交给了spring来维护 在当前类中需要 ...

  7. Spring和依赖注入的价值

    javaeye上看到有帖子,置疑spring和依赖注入的价值,回复内容整理如下: 依赖注入对设计有利,而spring则促进了依赖注入的使用. 如果业务处理类,它所使用的倚赖,都是依靠在这个类内部实现或 ...

  8. spring(一)依赖注入与 SPEL

    Spring之依赖注入与 SPEL 一.控制反转与依赖注入 二.helloworld 三.构造注入 四.级联注入 五.单例与多例 六.工厂方法创建 Bean 七.包扫描管理 bean 八.SPEL与资 ...

  9. Spring实现依赖注入的几种方式

    Spring实现依赖注入的几种方式 1.基于有参构造实现 <bean id="user" class="com.ccu.twj"><const ...

  10. 【Spring】依赖注入的几种方式

    在上篇文章中我着重介绍了Spring的控制反转和依赖注入的概念,那么依赖注入有那几种方式呢?他们的优缺点分别是什么,我将在本章中详细讲解. Spring的依赖注入根据对象类型注入可以分为属性注入和对象 ...

最新文章

  1. 每日一shell(八)nginx日志切割
  2. springboot+vue+element+mybatisplus项目(后端)
  3. Web前端工程师,互联网行业,炙手可热的翘楚!
  4. 删除MySql表中的大量记录后,文件不变小的解决方法
  5. Java中获取request对象的几种方法
  6. Boot2Docker 安装运行出现客户端与服务端版本不一致的解决办法
  7. 谭浩强c语言入门prd,完整C语言谭浩强学习笔记.docx
  8. c#元胞自动机_元胞自动机+生命游戏
  9. iperf3 linux源码下载
  10. 天涯明月刀手游为什么服务器维护,天涯明月刀手游12.7日更新公告 更新内容详情一览...
  11. 弦图(Echarts)
  12. Win11家庭版没有本地组策略编辑器怎么办?
  13. 我们写的程序就像我们的孩子
  14. 微信小程序 使用webview 缓存解决办法
  15. 【Django】有效解决django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_FROM, but settings
  16. 阿里云OSS绑定自定义域名
  17. 首都师范大学计算机技术复试分数线,首都师范大学2019年考研复试分数线已公布...
  18. linux下matlab2014卸载
  19. 04_好莱坞百万级电影评论数据分析
  20. JAVADOC注释详解

热门文章

  1. 前端3大JS框架走势图:vue增长最快,react或被Preact 取代
  2. 成功团队就是汇聚好的人才?谷歌“亚里士多德项目”为你揭开“惊人”谜底
  3. Windows各种系统文件无法打开故障解决方法!
  4. 点面科技金融银行解决方案
  5. GDAL对图像文件格式的转换
  6. Java毕设 SSM中药店商城系统(含源码+论文)
  7. 关于echarts的雷达图比较详细的参数说明
  8. echarts雷达图自定义射线颜色、边框效果和背景样式
  9. Android 长时间 卡顿,安卓手机用的久了会卡顿?这几点才最有用!
  10. 2019暑假牛客第5场-F.maximum clique 1-最大独立集(输出方案)