Spring 1 利用idea搭建spring hello world程序

来源:互联网 发布:越狱重启后软件消失 编辑:程序博客网 时间:2024/06/10 00:31

参考http://blog.csdn.net/cflys/article/details/70598903

1 HelloWorld.java

public class HelloWorld {    private String name;    public void setName(String name){        this.name = name;    }    public void sayHello(){        System.out.println("Hello" + name);    }}

Main.java

import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main{    public static void main(String[] args){        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");        helloWorld.sayHello();    }

spring-config.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">    <bean id="helloWorld" class="HelloWorld">        <property name="name" value="Spring"></property>    </bean></beans>

利用spring来对对象进行创建和管理

原创粉丝点击