SpringStudy

- 学习IOC/DI
- 联系构造器注入
- Setter注入
- 自动注入
This commit is contained in:
2026-09-23 12:04:05 +08:00
parent fed81f31f8
commit 26824ac4ad
4 changed files with 51 additions and 14 deletions
+14
View File
@@ -0,0 +1,14 @@
## IOC/DI(控制反转/依赖注入)
IOC/DI只解决一件事: 对象不new自己, 而是改由外部给他
#### 构造器注入和Setter注入:
1. 构造器在创建对象时注入, 能保证依赖非空, 适合必须的依赖, 官方推荐
2. Setter在对象创建后注入, 不能保证依赖非空, 适合可选的依赖
#### 自动注入 autowire 取值:
     取值:           含义:
1. no            不自动(默认)
2. byName   按属性名找Bean
3. byType    按类型找Bean
4. constructor    按构造器参数类型找
@@ -1,6 +1,7 @@
package com.ycjy;
import com.ycjy.dao.DeptDao;
import com.ycjy.dao.DeptDaoImpl;
import com.ycjy.service.DeptService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -10,7 +11,7 @@ public class Main {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 按照实现类取bean
// DeptDao deptDao = context.getBean("deptDao", DeptDao.class);
// DeptDaoImpl deptDao = context.getBean("deptDao", DeptDaoImpl.class);
// deptDao.doSth();
// 按照接口取bean
@@ -8,7 +8,10 @@ public class DeptServiceImpl implements DeptService {
private String test1;
private DeptDao deptDao;
public DeptServiceImpl(DeptDao deptDao) {
public DeptServiceImpl() {
}
public void setDeptDao(DeptDao deptDao) {
this.deptDao = deptDao;
}
@@ -17,10 +20,6 @@ public class DeptServiceImpl implements DeptService {
this.deptDao = deptDao;
}
public void setDeptDao(DeptDao deptDao) {
this.deptDao = deptDao;
}
@Override
public void doSth() {
System.out.println(this.test1);
@@ -1,14 +1,37 @@
<?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">
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byType"
default-autowire-candidates="*">
<bean id="deptDao" class="com.ycjy.dao.DeptDaoImpl"/>
<bean id="deptService" class="com.ycjy.service.impl.DeptServiceImpl">
<!-- 注入方式: 1. 构造函数注入 2.属性注入 3.setter注入 -->
<!-- 注入方式: 构造函数注入 -->
<constructor-arg value="abc"/>
<constructor-arg ref="deptDao"/>
</bean>
<!-- 说明:
value: 普通值(字符串, 数字, 浮点型等...会自动转换)
ref: ref = reference = 引用另一个Bean
-->
<bean id="deptDao" class="com.ycjy.dao.DeptDaoImpl"/>
<bean id="deptService" class="com.ycjy.service.impl.DeptServiceImpl"/>
<!-- 取消下面 deptDao2 的注释, 并删掉 default-autowire-candidates, 可复现 byType 冲突 -->
<!-- <bean id="deptDao2" class="com.ycjy.dao.DeptDaoImpl"/>-->
<!-- 构造函数和setter注入 -->
<!-- <bean id="deptService" class="com.ycjy.service.impl.DeptServiceImpl">-->
<!-- &lt;!&ndash; 注入方式: 1. 构造函数注入 2.setter注入 &ndash;&gt;-->
<!-- &lt;!&ndash; 注入方式: 构造函数注入(顺序要对应) &ndash;&gt;-->
<!-- <constructor-arg value="abc"/>-->
<!-- <constructor-arg ref="deptDao"/>-->
<!-- &lt;!&ndash; 注入方式: setter注入 &ndash;&gt;-->
<!-- &lt;!&ndash;-->
<!-- 这里记得要加无参构造函数, 因为上面已经写了有参的构造函数, 如果这里不加的话-->
<!-- 就会报No default constructor found(找不到无参构造函数)-->
<!-- &ndash;&gt;-->
<!-- &lt;!&ndash; name: 写属性名 &ndash;&gt;-->
<!--&lt;!&ndash; <property name="deptDao" ref="deptDao"/>&ndash;&gt;-->
<!-- </bean>-->
<!-- 自动注入 autowire -->
<!-- 如果同类型有多个Bean, 会报错(使用第35行代码时, 记得将beans里的default-autowire-candidates删掉) -->
<!-- <bean id="deptService_at" class="com.ycjy.service.impl.DeptServiceImpl" autowire="byType" />-->
</beans>