spring简单示例

1 配置config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!-- 定义一个bean id表示唯一一个bean class表示bean的来源 -->
    <bean id="HelloWorld" class="com.gc.action.HelloWorld">
        <!-- 将其变量msg通过依赖注入 -->
        <property name="msg">
            <value>HelloWorld</value>
        </property>
    </bean>
</beans>
View Code

 

2 实体类

package com.gc.action;

public class HelloWorld {
    public String msg = null;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}
View Code

 



3 编写测试示例

package com.gc.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.gc.action.HelloWorld;
public class TestHelloWorld {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext actx = new FileSystemXmlApplicationContext("config.xml");
        HelloWorld hello=(HelloWorld)actx.getBean("HelloWorld");
        System.out.println(hello.getMsg());
    }
}
View Code

 

posted on 2016-02-17 11:14  以勤补拙  阅读(173)  评论(0编辑  收藏  举报