Spring学习随记1
spring-IOC之依赖注入
官网地址:
https://docs.spring.io/spring/docs/5.2.2.RELEASE/spring-framework-reference/core.html#beans-factory-collaborators
涉及名词:
IOC:Inversion of Control控制反转
DI:dependency injection 依赖注入
IOC和DI之间的关系:IOC是一种解耦思想,DI是IOC的一种实现方式
依赖注入的实现
1.构造器注入
2.setter方法注入
3.接口注入(传闻早期有,但不人性故被移除,自由鸡没找相关文档,!·_·!)
代码结构
POM依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
构造器注入
代码实现
package com.csh.test.project;
import com.csh.test.project.service.ManagementService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
//加载xml元数据
ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext("beanXml.xml");
// 从容器中获取ManagementService bean
ManagementService managementService = cxt.getBean(ManagementService.class);
}
}
EmployeeService.java
package com.csh.test.project.service.impl;
import com.csh.test.project.service.EmployeeService;
/**
* Created on 2020-01-17.
*
* @author chensihong
*/
public class EmployeeServiceImpl implements EmployeeService {
public EmployeeServiceImpl() {
//init
System.err.println("EmployeeServiceImpl constructor");
}
@Override
public void print() {
System.err.println("EmployeeServiceImpl print!!!");
}
}
ManagementService.java
package com.csh.test.project.service.impl;
import com.csh.test.project.service.EmployeeService;
import com.csh.test.project.service.ManagementService;
/**
* Created on 2020-01-17.
*
* @author chensihong
*/
public class ManagementServiceImpl implements ManagementService {
public ManagementServiceImpl(EmployeeService employeeService) {
//init
System.err.println("ManagementServiceImpl");
employeeService.print();
}
}
增加Schema
红圈部分为引入模块的名字
beanXml.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <context:component-scan base-package="com.csh.test.project.service"/>-->
<bean id="employeeService" class="com.csh.test.project.service.impl.EmployeeServiceImpl"></bean>
<bean id="managementService" class="com.csh.test.project.service.impl.ManagementServiceImpl">
<constructor-arg ref="employeeService"/>
</bean>
</beans>
结果
setter方法注入
beanXml.xml
package com.csh.test.project.service.impl;
import com.csh.test.project.service.EmployeeService;
import com.csh.test.project.service.ManagementService;
/**
* Created on 2020-01-17.
*
* @author chensihong
*/
public void setEmployeeService(EmployeeService employeeService) {
System.out.println("setter mother");
employeeService.print();
}
public ManagementServiceImpl() {
//init
System.err.println("ManagementServiceImpl");
}
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="employeeService" class="com.csh.test.project.service.impl.EmployeeServiceImpl"></bean>
<bean id="managementService" class="com.csh.test.project.service.impl.ManagementServiceImpl">
<property name="employeeService" ref="employeeService"/>
</bean>
</beans>
结果
拓展
循环依赖
使用构造函数注入,Class A 引Class B,Class B引Class A,导致死循环, bean无法创建成功,官方建议修改源代码,不要使用构造函数注入,但可以使用setter方法解决
更多相关xml配置查看[官方文档](https://docs.spring.io/spring/docs/5.2.2.RELEASE/spring-framework-reference/core.html#beans-value-element)