Spring-helloWorld

Spring的helloWorld程序
1配置Spring的开发环境,只需要把下面几个包添加到lib文件夹下面即可:

2编写Bean类,并为其属性提供setter方法,

package com.jeremy.spring.beans;

public class HelloWorld {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    
    public void helloworld() {
        System.out.println("helloworld:  " + name);
        
    }
}

3添加在src目录下新建ApplicationContext.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">


</beans>

4在ApplicationContext.xml文件里配置我们写好的Bean类

    
    <bean id="helloWorld" class="com.jeremy.spring.beans.HelloWorld"> 
    <property name="name" value="Spring"></property>
    
    </bean>
    

5测试

@Test
    public void testMain(){
        //1获取ApplicationContext(IOC)容器
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        
        //2从ApplicationContext容器里获取Bean实例
        HelloWorld helloWorld=(HelloWorld) applicationContext.getBean("helloWorld");
        
        //3利用获取到的Bean实例调用Bean的方法
        helloWorld.helloworld();
    }

以上就是Spring的HelloWold程序

posted @ 2014-10-14 09:52  Jeremy_software  阅读(283)  评论(0编辑  收藏  举报