Spring 源码分析:分析 Hello World 程序


#Spring


(未完成)

Main.java :

package demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        SayHello sayHello = (SayHello) ctx.getBean("sayHello");
        System.out.println(sayHello.sayHello("world"));
    }

}

SayHello.java :

package demo;

public class SayHello {

    public SayHello() {
        System.out.println("init SayHello");
    }

    public String sayHello(String name) {
        return "Hello, " + name;
    }

}

SayHello 单例的生成

获取单例

在执行 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 时,Spring 容器中会生成 SayHello 类的单例。



( 本文完 )