当前位置:网站首页>依赖注入方式
依赖注入方式
2022-07-17 06:53:00 【白小筠】
setter注入:
简单类型:
1.提供对应的set方法
public class BookDaoImpl implements BookDao {
public String databaseName;
public int num;
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
public void setNum(int num) {
this.num = num;
}
public void save() {
System.out.println("BookDao save!"+databaseName+num);
}
}
2,配置value
<bean id= "bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<property name="databaseName" value="mysql"></property>
<property name="num" value="10"></property>
</bean>![]()
引用类型:
1.提供对应的set方法
2.配置url
构造器注入:
简单类型
1.construct
public class BookDaoImpl implements BookDao {
public String databaseName;
public int num;
public BookDaoImpl(String databaseName, int num) {
this.databaseName = databaseName;
this.num = num;
}
public void save() {
System.out.println("BookDao save!"+databaseName+num);
}
}
2.配置
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<constructor-arg name="databaseName" value="mysql"></constructor-arg>
<constructor-arg name="num" value="10"></constructor-arg>
</bean>由于name表示的是形参的名字
通过以下方法减少耦合度
1.使用type
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<constructor-arg type="java.lang.String" value="mysql"></constructor-arg>
<constructor-arg type="int" value="10"></constructor-arg>
</bean>2.使用index
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<constructor-arg index="0" value="mysql"></constructor-arg>
<constructor-arg index="1" value="10"></constructor-arg>
</bean>引用类型
1.
public class BookServiceImpl implements BookService, InitializingBean, DisposableBean {
private BookDao bookDao ;
public BookServiceImpl(BookDao bookDao) {
this.bookDao = bookDao;
}
}2.配置
name是BooServiceImpl构造方法中的形参名字
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/>
</bean>依赖注入方式选择
1.强制依赖使用构造器,使用setter注入有概率不进行注入导致null对象出现
2.可选依赖使用setter注入进行,灵活性强
3.Spring框架倡导使用构造器
4.如果有必要可以两者同时使用,使用构造器注入完成强制依赖的注入,使用setter注入完成可选依赖的注入
5.实际开发中,如果受控对象没有提供setter方法就必须使用构造器注入
6.自己开发的模块推荐使用setter
依赖自动装配
自动装配优先级低于setter注入与构造器注入,同时出现自动装配配置失效
按类型:推荐使用,必须保证容器相同类型的bean唯一
按名称
按构造方法
不启用自动装配:
1.使用setter方法
public class BookServiceImpl implements BookService, InitializingBean, DisposableBean {
private BookDao bookDao ;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}2.配置
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byType"/>
边栏推荐
- How did "leek" give money to "sickle"? 2020-03-07
- First experience of openvino machine learning
- How to choose the right model
- Kingbasees can realize any of MySQL by constructing an aggregate function_ Value function.
- Beijing Jiewen technology, an acquiring outsourcing service provider, transferred 60% of its shares for about 480million
- Redis jump table implementation principle & time complexity analysis
- Ruffian Heng embedded bimonthly issue 58
- “韭菜”是怎么把钱送给“镰刀”的? 2020-03-07
- From the casino logic, analyze the investment value of platform currency 2020-03-03
- Ccf-csp "202206-2 - treasure hunt! Adventure!"
猜你喜欢

并发编程的核心问题

Jd.com's purchase intention forecast (IV)

代码学习(DeamNet)CVPR | Adaptive Consistency Prior based Deep Network for Image Denoising

Ku115 FPGA high performance 10G Optical fiber network hardware accelerator card / 2-way 10G Optical fiber data accelerator card

Shenzhen Prudential written examination record

Understand LSTM and Gru
![[MySQL] transaction: basic knowledge of transaction, implementation principle of MySQL transaction, detailed explanation of transaction log redolog & undo](/img/88/282a6ddfb37944e9cacc62c202364d.png)
[MySQL] transaction: basic knowledge of transaction, implementation principle of MySQL transaction, detailed explanation of transaction log redolog & undo

RNN convolutional neural network

Forecast sales xgboost

High performance integrated video image processing board based on ultrascale FPGA + Huawei Hisilicon arm / fpga+arm
随机推荐
First experience of openvino machine learning
Pytoch notes (2)
Precautions for MySQL statements
[C# 变量常量关键字]- C# 中的变量常量以及关键字
Mongodb index
深圳保诚笔试记录
redis集群
WPF 三维应用搭建(基础)
串口通讯到底有没有累积误差及对时钟精度的要求
牛客题目——打家劫舍一、打家劫舍二
CAD fill to polyline script
[C console] - C console class
如何在 Jenkins Pipeline 中使用curl 并处理响应结果
Paddleserving服务化部署 tensorrt报错, shape of trt subgraph is [-1,-1,768],
会话技术【黑马入门系列】
【Kernel】驱动开发学习之字符设备
[C # variable constant keyword] - variable constants and keywords in C #
[MySQL] transaction: basic knowledge of transaction, implementation principle of MySQL transaction, detailed explanation of transaction log redolog & undo
Pytoch notes (1)
并发编程的核心问题