首页 > 编程语言 > 详细

Spring源码解析(一)实现IOC的几种方式

时间:2019-05-17 22:29:31      阅读:167      评论:0      收藏:0      [点我收藏+]

项目结构

├── iocinit.iml
├── pom.xml
├── src
│?? ├── main
│?? │?? ├── java
│?? │?? │?? └── com
│?? │?? │??     └── terwergreen
│?? │?? │??         └── spring
│?? │?? │??             └── iocinit
│?? │?? │??                 ├── IOCTest1.java
│?? │?? │??                 ├── IOCTest2.java
│?? │?? │??                 ├── IOCTest3.java
│?? │?? │??                 └── beans
│?? │?? │??                     ├── America.java
│?? │?? │??                     ├── Chinese.java
│?? │?? │??                     └── Person.java
│?? │?? └── resources
│?? │??     └── applicationContext.xml
│?? └── test
│??     └── java

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.terwergreen.spring</groupId>
    <artifactId>iocinit</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <!-- spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <!-- spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
    </dependencies>
</project>

beans

先建立几个bean

America.java

package com.terwergreen.spring.iocinit.beans;

public class America implements Person {
    public String sayHello(String name) {
        return "hello," + name;
    }
}

Chinese.java

package com.terwergreen.spring.iocinit.beans;

/**
 * Chinese
 *
 * @author Terwer
 * @version 1.0
 * 2019/5/6 17:39
 **/
public class Chinese implements Person {
    public String sayHello(String name) {
        return "你好," + name;
    }
}

Person.java

package com.terwergreen.spring.iocinit.beans;

/**
 * Person
 *
 * @author Terwer
 * @version 1.0
 * 2019/5/6 17:39
 **/
public interface Person {
    String sayHello(String name);
}

使用 XmlBeanFactory 实现IOC

package com.terwergreen.spring.iocinit;

import com.terwergreen.spring.iocinit.beans.Person;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

/**
 * IOCTest1
 *
 * @author terwer
 * @version 1.0
 * 2019-05-17 21:38
 **/
public class IOCTest1 {

    public static void main(String[] args) {
        ClassPathResource resource = new ClassPathResource("applicationContext.xml");
        XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
        Person p1 = (Person) beanFactory.getBean("chinese");
        String result = p1.sayHello("Terwer");
        System.out.println(result);

        Person p2 = (Person) beanFactory.getBean("america");
        String result2 = p2.sayHello("Green");
        System.out.println(result2);

    }
}

输出

你好,Terwer
hello,Green

使用 DefaultListableBeanFactory 实现IOC

package com.terwergreen.spring.iocinit;

import com.terwergreen.spring.iocinit.beans.Person;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;

/**
 * IOCTest2
 *
 * @author terwer
 * @version 1.0
 * 2019-05-17 21:45
 **/
public class IOCTest2 {

    public static void main(String[] args) {
        // 定位
        ClassPathResource resource = new ClassPathResource("applicationContext.xml");
        // 载入
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        // 注册
        reader.loadBeanDefinitions(resource);

        // 从beanFactory获取bean
        Person p1 = (Person) beanFactory.getBean("chinese");
        String result = p1.sayHello("Green");
        System.out.println(result);
    }
}

输出

你好,Green

使用 ClassPathXmlApplicationContext 实现IOC

package com.terwergreen.spring.iocinit;

import com.terwergreen.spring.iocinit.beans.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * IOCTest3
 *
 * @author terwer
 * @version 1.0
 * 2019-05-17 21:47
 **/
public class IOCTest3 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        Person p1 = (Person) applicationContext.getBean("america");
        String result = p1.sayHello("Test3 from applicationContext");
        System.out.println(result);
    }
}

输出

hello,Test3 from applicationContext

Spring源码解析(一)实现IOC的几种方式

原文:https://www.cnblogs.com/tangyouwei/p/10883928.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!