IOC容器学习笔记

参考博客

概念

IOC容器就是具有依赖注入功能的容器,IOC容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。在Spring中BeanFactory是IOC容器的实际代表者。Spring IOC容器通过读取配置文件中的配置元数据,通过元数据对应用中的各个对象进行实例化及装配。一般使用基于xml配置文件进行配置元数据,而且Spring与配置文件完全解耦的,可以使用其他任何可能的方式进行配置元数据,比如注解、基于java文件的、基于属性文件的配置都可以。

Bean概念

Bean表示组成应用程序的对象,由Spring容器初始化、装配及管理。

HelloWorld例子

  1. HelloWorld接口
package com.example.demo.Service;

public interface IHelloWorld {

    String sayHelloImp();
    
}
  1. HelloWorld实现
package com.example.demo.Service.Imp;

import com.example.demo.Service.IHelloWorld;
import java.text.MessageFormat;

public class HelloWorldImp implements IHelloWorld {

    private String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String sayHelloImp(){
        return MessageFormat.format("Hello, {0}!", userName);
    }
}
  1. helloworld.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- id表示组件名字,class表示组件类 -->
    <bean id="helloWorld" class="com.example.demo.Service.Imp.HelloWorldImp">
        <property name="userName" value="Potato"/>
    </bean>
</beans>
  1. HelloWorld调用
package com.example.demo.controller;

import com.example.demo.Service.Imp.HelloWorldImp;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RestController
public class HelloWorldController {
    @Resource(name="helloWorld")
    private HelloWorldImp helloWorldImp;

    @RequestMapping("/hello")
    public String sayHello(){
        return helloWorldImp.sayHelloImp();
    }
}
  1. 启动文件中导入配置文件
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@ImportResource(locations = {"classpath:helloworld.xml"})
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);
    }
}
  1. 测试代码:
package com.example.demo;

import com.example.demo.Service.Imp.HelloWorldImp;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import static org.junit.Assert.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:helloworld.xml"})
public class HelloWorldControllerTest {
    @Resource(name="helloWorld")
    HelloWorldImp helloWorldImp;
    @Test
    public void testSayHello(){
        assertEquals("Hello, Potato!", helloWorldImp.sayHelloImp());
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容