• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
seven_hu
博客园    首页    新随笔    联系   管理    订阅  订阅

建立一个Hello World级别的Spring项目

package com.sevenhu.domain;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by hu on 2016/3/31.
 */
public class HelloWorld {
    private String userName;

    //Spring项目中,配置在容器中的类,其属性都必须有setter方法
    public void setUserName(String userName) {
        this.userName=userName;
    }
    public void hello(){
        System.out.println("Hello: "+userName);
    }
    public static void main(String[] args){
        //1.创建Spring的IOC容器
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
       //2.从容器中获取bean
        HelloWorld helloWorld= (HelloWorld) applicationContext.getBean("helloWorld");
        System.out.println(helloWorld);
        //3.调用方法
        helloWorld.hello();
    }
}

  Spring的配置文件的代码如下:

<?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">

       <bean id="helloWorld" class="com.sevenhu.domain.HelloWorld">
              <property name="userName" value="Spring"></property>
       </bean>
</beans>

  

posted @ 2016-03-31 16:44  seven_hu  阅读(231)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3