zoukankan      html  css  js  c++  java
  • Spring入门之HelloWorld

    1、开发工具 Myeclipse 

    2、步骤

    (1)新建Java项目

    (2)右键点击项目->MyEclipse->Add Spring Capabilities,选择对应的Spring版本号和需要导入的Spring jar包。

    (3)新建类HellWorld.java和测试的Test.java,修改配置文件名为beans.xml(文件名称可自定义,个人习惯修改为beans.xml)。

    目录清单如下图:

            1.png

    (4)HelloWorld.java如下:


    package com.spring.bean;
    
    public class HelloWorld {
    	private String name;
    	
    	public void show(){
    		System.out.println("Spring,hello,"+name);
    	}
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    }
    
    (5)beans.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"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    	<bean id="hellobean" class="com.spring.bean.HelloWorld">
    		<property name="name" value="jeff"></property>
    	</bean>
    
    </beans>
    其中:id为自定义的bean标识名称,唯一,class是和id对应的类全名,property可以该类中的属性进行配置赋值。
    (6)测试
    package com.spring.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.spring.bean.HelloWorld;
    
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		ApplicationContext content = 
                             new ClassPathXmlApplicationContext("beans.xml");
    		HelloWorld obj = (HelloWorld) content.getBean("hellobean");
    		obj.show();
    	}
    
    }
    
    三行代码的意思分别是: 读取配置文件beans.xml
    获取bean的idhellobean并返回,转换为HelloWorld类型
    调用方法
    运行效果如下图:



     



  • 相关阅读:
    d3.js--04(enter和exit)
    d3.js--03(增删改查)
    d3.js--02(data和datum原理)
    css文件和js文件后面带一个问号----2015-1103
    json_decode()和json_encode()区别----2015-0929
    随记--更新于20150925
    个人火狐插件汇总--20150924
    『jQuery』.html(),.text()和.val()的概述及使用--2015-08-11
    矩阵求导
    傅里叶变换
  • 原文地址:https://www.cnblogs.com/pilihaotian/p/8823115.html
Copyright © 2011-2022 走看看