zoukankan      html  css  js  c++  java
  • 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程序

  • 相关阅读:
    2017.3.11[bzoj2440][中山市选2011]完全平方数
    2017.3.6[hihocoder#1415]后缀数组三·重复旋律3
    2017.3.4[hihocoder#1407]后缀数组二·重复旋律2
    [NOI2013]快餐店
    [HNOI2014]米特运输
    [HNOI2015]亚瑟王
    [JLOI2013]卡牌游戏
    [SDOI2010]地精部落
    [ZJOI2007]棋盘制作
    [AHOI2009]中国象棋
  • 原文地址:https://www.cnblogs.com/jeremy-blog/p/4023743.html
Copyright © 2011-2022 走看看