Spring IoC

Spring IoC


前言

IoC(控制反转)是Spring框架的基础,也是Spring框架的核心理念,下面将介绍IoC的基本概念,容器以及依赖注入的类型等内容和代码演示


一、SpringIoC是什么?

SpringIoC我自己理解为Spring容器帮我们创建Java对象,用于减少调用者与被调用者之间的耦合性。Spring容器会负责控制程序之间的关系(例如面包负责控制您与面包的关系)。而不是由调用者的程序代码直接控制。这样,控制权由调用者转移到Spring容器,控制发生了反转,这就是Spring的控制反转。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入

二、SpringIoC容器

实现控制反转的就是SpringIoC容器,

1.BeanFactory

这里不做详细介绍,因为使用BeanFactory实例加载Sprring配置文件在实际开发中并不多见,了解即可。
常用XmlBeanFactory装配Bean,在创建BeanFactory实例时需要提供XML文件的绝对路径

public static void main(String [] args){
	BeanFactory beanFac = new XmlBeanFactory(new FileSystemResource(D
	:\eclipse-workspace\test\src\applicationContext.xml")
	):
	TestDao tt = (TestDao)beanFac,getBean("test");
	test.sayHello():
}

2.ApplicationContext

ApplicationContext是BeanFactory的子接口,也称为应用上下文,除了包含BeanFactory所有功能外,还添加对国际化,资源访问,事件传播等内容的支持。
创建实例通常有三种方法

1.通过ClassPathXmlApplicationContext创建(从类路径目录(src根目录))

 public static void main(String [] args){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestDIService ts = (TestDIService) applicationContext.getBean("testDIservice");
        ts.sayhello();
    }

2.通过FileSystemXmlApplicationContext创建(绝对路径)

 public static void main(String [] args){
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\workspace\test\src\applicationContext.xml");
        TestDIService ts = (TestDIService) applicationContext.getBean("testDIservice");
        ts.sayhello();
    }

3.在Web应用中,ApplicationContext容器的实例化工作将交给Web服务器完成。一般使用基于ContextLoaderListener的实现方法(需要导入spring-web-5.0.2.RELEASE.jar)并在web.xml中添加如下代码

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

三、依赖注入的类型

实现IoC容器的方法就是依赖注入

1.使用构造方法

即通过构造方法在创建Bean对象是动态的将其所依赖的对象(属性值之类的)注入Bean组件中
下面通过实例演示

public interface TestDIDao {
    public void sayHello();
}
public class TestDIDaoimpl implements TestDIDao{
    @Override
    public void sayHello() {
        System.out.println("hello!");
    }
}


public interface TestDIService {
    public void sayhello();
}

public class TestDIServiceimpl implements TestDIService {
    private TestDIDao testDIDao;

    public TestDIServiceimpl(TestDIDao testDIDao) {
        this.testDIDao = testDIDao;
    }

    @Override
    public void sayhello() {
        testDIDao.sayHello();
        System.out.println("TestDIservice构造方法注入say:hello!");
    }
}


public class TestDI {
    public static void main(String [] args){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestDIService ts = (TestDIService) applicationContext.getBean("testDIservice");
        ts.sayhello();
    }
}

<?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">
    <bean id="myTestDiDao" class="dao.TestDIDaoimpl"/>

    <bean id="testDIservice" class="service.TestDIServiceimpl">
        <constructor-arg index="0" ref="myTestDiDao"/>
    </bean>
</beans>

运行结果
在这里插入图片描述

2.使用setter方法注入

即通过setter方法注入Bean中的属性值,在上面那个例子中将serviceimpl类中的TestDao对象添加一个setter方法,修改配置文件中的Bean为

<bean id="testDIservice" class="service.TestDIServiceimpl">
        <property name="testDIdao" ref="myTestDiDao"/>
    </bean>

这就是setter方法注入的简单模拟


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值