干货分享:spring di 依赖注入有几种方式? - 编程思维

Spring 实例化 bean 的方式

  • Set注入
  • 构造器注入
  • 静态工厂注入
  • 实例化工厂注入

案例实操

Set注入

xml配置(同时spring也提供了对于基本数据类型的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.or...d">

<bean id="userDao" class="com.xxx.demo.UserDao"></bean>
<!-- setter注入 -->
<bean id="userService" class="com.xxx.demo.UserService">
<!--ref是对于外部bean对象引用,与被引用的bean对象的id保持一致-->
<property name="userDao" ref="userDao"></property>
</bean>
</beans>

UserDao.java

public class UserDao {
public String userLogin() {
return "我是UserDao中的userLogin()的方法";
}
}

UserService.java

public class UserService {
//一定要提供属性的setter方法
private UserDao userDao;

public void userlogin() {
String res=userDao.userLogin();
System.out.println(res);
}

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}

App.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
UserService userService=applicationContext.getBean("userService", UserService.class);
userService.userlogin();
}
}

构造器注入

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/beans
http://www.springframework.or...d">

<bean id="userDao" class="com.xxx.demo.UserDao"></bean>
<!-- 构造器注入 -->
<bean id="userServiceV2" class="com.xxx.demo.UserServiceV2">
<constructor-arg index="0" ref="userDao"></constructor-arg>
<constructor-arg index="1" value="印度三哥"></constructor-arg>
</bean>
</beans>

构造器注入有三种形式:

index属性为参数顺序,如果只有一个参数index可以不设置。

name属性根据构造器中属性的名字。

type属性,是根据构造器中属性的类型来匹配的。如果相同类型属性不唯一,注入的属性按照顺序注入进来。

UserServiceV2.java类提供构造函数

/**

  • 实现构造器注入
  • @author Best Liu

*
*/
public class UserServiceV2 {
private UserDao userDao;
private String name;
public void userlogin() {
String res=userDao.userLogin();
System.out.println(res);
System.out.println(name);
}
public UserServiceV2(UserDao userDao,String name) {
super();
this.userDao = userDao;
this.name = name;
}
}

静态工厂注入

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/beans
http://www.springframework.or...d">
<!-- 静态工厂注入 -->
<bean id="userDao01" class="com.xxx.demo.StaticFactory" factory-method="createuserDao"></bean>
<bean id="userService01" class="com.xxx.demo.UserService">
<property name="userDao" ref="userDao01"></property>
</bean>
</beans>

StaticFactory.java

public class StaticFactory {
public static UserDao createuserDao(){
return new UserDao();
}
}

UserService.java

public class UserService {
private UserDao userDao;

public void userlogin() {
String res=userDao.userLogin();
System.out.println(res);
}

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}

tips:静态工厂注入就是IoC静态工厂和DI的setter注入,将需要注入的属性对象利用静态工厂创建出来.

2.4 实例化工厂

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/beans
http://www.springframework.or...d">

<!-- 实例化工厂 -->
<bean id="instanceFactory" class="com.xxx.demo.InstanceFactory"></bean>
<bean id="userDao3" factory-bean="instanceFactory" factory-method="createUserDao"></bean>
<bean id="userService02" class="com.xxx.demo.UserService">
<property name="userDao" ref="userDao3"></property>
</bean>
</beans>

InstanceFactory.java

public class InstanceFactory {
public UserDao createUserDao(){
return new UserDao();
}
}

tips:重点掌握set,构造器注入,工厂方式了解即可,实际开发中基本使用set方式注入bean。

扩展

循环依赖的问题的产生

Bean通过构造器注入,之间彼此相互依赖对方导致bean无法实例化。

注入的选择:开发项目中set方式注入首选

使用构造注入可以在构建对象的同时一并完成依赖关系的建立,对象一建立则所有的一切也就准备好了,但如果要建立的对象关系很多,使用构造注入会在构建函数上留下一长串的参数,且不易记忆,这时使用Set注入会是个不错的选择。

使用Set注入可以有明确的名称,可以了解注入的对象会是什么,像setxxx()这样的名称比记忆Constructor上某个参数的位置代表某个对象更好。

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/beans
http://www.springframework.or...d">
<bean id="goodsService" class="com.xxx.demo.GoodsService">
<!-- <constructor-arg index="0" ref="userService"></constructor-arg> -->
<property name="userService" ref="userService"></property>
</bean>
<bean id="userService" class="com.xxx.demo.UserService">
<!-- <constructor-arg index="0" ref="goodsService"></constructor-arg> -->
<property name="goodsService" ref="goodsService"></property>
</bean>
</beans>

GoodsService.java

public class GoodsService {
private UserService userService;
/*public GoodsService(UserService userService) {
super();
this.userService = userService;
}*/
public void setUserService(UserService userService) {
this.userService = userService;
}
}

UserService.java

public class UserService {
private GoodsService goodsService;
/* public UserService(GoodsService goodsService) {
super();
this.goodsService = goodsService;
}
*/
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}
}

版权声明:本文版权归作者所有,遵循 CC 4.0 BY-SA 许可协议, 转载请注明原文链接
https://segmentfault.com/a/1190000038632350

面试一家小公司,被问了 1 个小时并发编程。。全程已记录! - 编程思维

最近有读者投简历去一家小公司去面试的时候,和面试官聊了一个小时的并发编程,整个过程已全记录下来。面试的时候小心翼翼,如履薄冰,生怕说错一句话,就错失机会,担心,面试紧张、卡壳。不知道大家有没有这个问题?​_面试官:_你知道CAS吗,能跟我讲讲吗?_我:_CAS(Compare And Swap),比较并交换。整个AQS

推荐5款最好用的内网穿透工具 - 编程思维

公众号开发过程中,频繁修改线上代码,开发过程比较繁琐,同时一旦出错将会影响到线上的其他正常业务,因此搭建一个微信公众号开发的本地环境能达到不影响线上业务的同时调试代码。这里推荐几款非常好用的内网穿透工具,如有其它推荐,欢迎补充1.frpfrp 是一个高性能的反向代理应用,可以帮助您轻松地进行内网穿透,对外网提供服务,支

如何用一句话证明你是程序员? - 编程思维

  很有意思的一个话题,程序员因为其职业的特殊性,很多时候一个简单的细节就能暴露程序员的身份,特别是经常和媳妇在下班之前的交谈体会更加明显,其实这种细节体现在方方面面,很简单一句话很容易就能辨别真伪,现在就本人的一些生活细节简单罗列下。1.今晚要出版本  这基本上说的最多的一句话,正常情况下媳妇听到这句话就知道,晚饭不

github标星7.3k+,这款程序员代码补全工具,让你的编程效率飞起来! - 编程思维

今天小编推荐一款代码补全工具,堪称代码神器,他叫TabNine。TabNine支持23种编程语言、5种编辑器,使用简单,效果惊艳。不少使用过的网友说:TabNine是他们用过的最好的代码补全工具,这是属于程序员的杀手级应用。在VS Code的扩展商店里,TabNine已经被下载1.3万次,获得全5星好评。Deep Ta

一个老程序员的忠告:你这辈子输就输在以为靠技术就能生存下 - 编程思维

一、 在一个地方工作8小时就是“穷”1、在中国你千万不要因为学习技术就可以换来稳定的生活和高的薪水待遇,你更不要认为那些从事市场开发,跑腿的人,没有前途。不清楚你是不是知道,咱们中国有相当大的一部分软件公司,他们的软件开发团队都小的可怜,甚至只有1-3个人,连一个项目小组都算不上,而这样的团队却要承担一个软件公司所有的

用python画一棵带音乐的雪夜圣诞树 - 编程思维

本文我们用 Python 来画一棵带音乐效果的雪夜圣诞树,基本思路如下:用 Python 画一棵圣诞树作为背景图在圣诞树背景图中添加雪落效果及音乐下面来看一下具体实现。首先,我们来画一棵圣诞树,主要用到的 Python 库为 turtle,主要代码实现如下:n = 80.0 turtle.setup(700, 700,

面试一家小公司,被问了 1 个小时并发编程。。全程已记录! - 编程思维

最近有读者投简历去一家小公司去面试的时候,和面试官聊了一个小时的并发编程,整个过程已全记录下来。面试的时候小心翼翼,如履薄冰,生怕说错一句话,就错失机会,担心,面试紧张、卡壳。不知道大家有没有这个问题?​_面试官:_你知道CAS吗,能跟我讲讲吗?_我:_CAS(Compare And Swap),比较并交换。整个AQS

spl 中调用 python 程序 - 编程思维

【摘要】      集算器 SPL 集成了对 python 程序的调用,也提供对建模算法接口支持。具体开发要求、使用详细情况,请前往乾学院:SPL 中调用 Python 程序!集算器是强大的数据计算引擎,但目前对于机器学习算法的提供还不够丰富。而 python 中有许多此类算法。借助 YM 外部库,就可以让集算器 SP

彻底搞懂python 中的 import 与 from import - 编程思维

以下文章来源&作者:青南(谢乾坤)摄影:产品经理;kingname 的第一套乐高你好,我是谢乾坤,前网易高级数据挖掘工程师。现任微软最有价值专家(Python 方向),有6年 Python 开发经验,善于解决各种业务场景下的棘手问题,进一步提升代码质量。对不少 Python 初学者来说,Python 导入其他模

python bytes 反斜杠转义问题解决方法 - 编程思维

一、问题因为前文提到工作中需要使用 Go 调 Python 脚本执行加解密,但是 Go 那边执行命令行输出的是 str 类型。因此需要转回 bytes 进行解密,但是发现转回 bytes 和原来的不一样。下面以一个小例子做演示。old_bytes = b"=\x1di\xab\xc1~)]9H\xdf\x0c\x7f`