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
  • 相关阅读:
    【NOIP模拟】排序
    【NOIP模拟】企鹅矩阵
    【NOIP模拟】花花森林
    【BZOJ1045】糖果传递
    【BZOJ1271】秦腾的教学评估
    【POJ3714】Raid
    【TYVJ1424】占卜DIY
    【POJ3190】The Pilots Brothers' refrigerator
    【HDU1055】Color a Tree
    【NOIP2012】国王游戏
  • 原文地址:https://www.cnblogs.com/studyjava/p/5194734.html
Copyright © 2011-2022 走看看