spring-IOC之依赖注入——XML篇

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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值