zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    2020/3/21 简单的学习
    2020/3/7 A-B
    2020/3/6 旋转骰子
    2020/3/6 美丽数组
    面向对象程序设计寒假作业2
    自我介绍
    深度优先搜索-迷宫问题(走迷宫题解)
    开机方案题解
    好吃的巧克力题解
    数楼梯题解
  • 原文地址:https://www.cnblogs.com/studyjava/p/5194734.html
Copyright © 2011-2022 走看看