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

项目结构

├── 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

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

猜你喜欢

转载自www.cnblogs.com/tangyouwei/p/10883928.html