1、从官网下载相关JAR包

spring-framework-4.2.1.RELEASE-dist(下载地址:http://maven.springframework.org/release/org/springframework/spring/)

commons-logging-1.2-bin(下载地址:http://commons.apache.org/proper/commons-logging/download_logging.cgi)

2、新建JAVA PROJECT项目

3、从项目中引入(1)内的JAR包

4、SRC内新建2个类文件Helloworld.java & Main.java代码如下。

package com.bai.spring.beans;
public class Helloworld {
private String name;
public void setName(String name){
this.name=name;
}
public void hello(){
System.out.println("hello:"+name);
}
}
package com.bai.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String []args){
/* 不使用springde 情况
* //创建一个HelloWorld对象
HelloWorld helloworld=new HelloWorld();
//为对象赋值
helloworld.setName("baixl");
//调用hello()方法
*/
ApplicationContext ctx=new ClassPathXmlApplicationContext("SPRING.xml");
Helloworld helloworld=(Helloworld) ctx.getBean("helloworld");
helloworld.hello();
}
}

5、新建SRPING.XML文件

在SRC根目录建立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"> <!-- 配置bean -->
<bean id="helloworld" class="com.bai.spring.beans.Helloworld">
<property name="name" value="Spring"></property>
</bean>
</beans>

6、RUN AS -> JAVA APPLICATION 运行。

7、完成。正确结果显示如下

十月 13, 2015 3:51:16 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19a5dff: startup date [Tue Oct 13 15:51:16 CST 2015]; root of context hierarchy
十月 13, 2015 3:51:17 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationcontext.xml]
hello:Spring

--------------------------------

重点提示:

1、在我们运行的时候很多时候会提示”Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogF“这个信息,很多时候是因为我们没有引用commons-logging-1.2-bin的JAR包,引用上即可。

2、如上为SRPING的基本入门。

运行流程:

首先会运行main()语句,Spring框架使用ClassPathXmlApplicationContext()首先创建一个容器。
这个容器从Beans.xml中读取配置信息,并根据配置信息来创建bean(也就是对象),每个bean有唯一的id。
然后通过context.getBean()找到这个id的bean,获取对象的引用。
通过对象的引用调用printMessage()方法来打印信息。
好了,这是你的第一个Spring应用。你已经学会用Eclipse新建Java项目,导入Spring和commons-logging库,编写Java源代码和XML配置文件,并且成功运行了。如果要更改输出,只需要修改XML文件中的value值,而不需要更改Java源文件。

spring4 之 helloworld的更多相关文章

  1. Spring4 MVC HelloWorld 注解和JavaConfig实例

    在这一节中,我们以 Spring4 MVC HelloWorld 注释/JavaConfig为示例,一步一步以简单的方式学习Spring4 MVC 的注解,项目设置,代码,部署和运行. 在先前的 Sp ...

  2. SpringMVC HelloWorld实例开发及部署

    SpringMVC HelloWorld实例开发及部署 2017-01-24 目录 1 Tomcat及Eclipse Tomcat插件安装配置  1.1 Tomcat的安装  1.2 Eclipse ...

  3. IDEA+Maven+Spring MVC HelloWorld示例

    用Maven创建Web项目 选择webapp模板 创建成功后点Enable Auto-Import idea给我们创建出来的结构是这样的,这还不标准,需要自己修改. 在main文件夹下创建java文件 ...

  4. Spring FrameWork4(MVC + IOC)高速入门实例

    使用Maven创建project并配置依赖项 首先创建一个Maven Project: 然后选择创建Maven 的webapp实例,当然也能够通过命令行方式创建Maven webapp的项目再转化并导 ...

  5. spring mvc:练习:javaConfig配置和注解

    Spring4 MVC HelloWorld 注释/JavaConfig为示例,一步一步以简单的方式学习Spring4 MVC 的注解,项目设置,代码,部署和运行. 我们已经使用XML配置开发了一个H ...

  6. springMVC入门一

    一.准备工作 eclipse使用maven搭建项目,项目名称为HelloSpringMVC 二.搭建好的项目如下: 项目介绍:实现简单的helloworld 三.具体代码 controller类:He ...

  7. 峰Spring4学习(1)HelloWorld

    HelloWorld.java: package com.cy.test; public class HelloWorld { public void say(){ System.out.printl ...

  8. 1 Spring4 之环境搭建和HelloWorld

    1 Spring 是什么? 具体描述 Spring: 轻量级:Spring 是非侵入性的- 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API 依赖注入(DI --- dep ...

  9. Spring4学习回顾之路01—HelloWorld

    以前公司一直使用的是spring3.0,最近一段时间开始用了4.0,官网上都已经有了5.0,但是很多知识点已经忘了差不多了,趁现在项目不忙写写随笔,一来回顾自己的知识点,二来如果能帮助比我还小白的小白 ...

随机推荐

  1. jQuery修炼心得-DOM节点的插入

    1. 内部插入append()与appendTo() append:这个操作与对指定的元素执行原生的appendChild方法,将它们添加到文档中的情况类似. appendTo:实际上,使用这个方法是 ...

  2. Lock使用实例

    using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; usin ...

  3. String类理解

    1.首先应该知道的是:String是一个类,不属于基本类型,而是引用类型. 2.创建String对象的三种方式的比较: 第一种是先去String pool检查是否存在"abc"常量 ...

  4. Python函数之lambda,内置函数,yield生成器等

    lambda 1,用于处理简单逻辑 2,自动返回数据(return) 单参数 >>> func2 = lambda a: a+1>>> result = func2 ...

  5. js原型链部分详细使用说明案例

    1. 'index.html'文件 ```html <!DOCTYPE html> <html lang="en"> <head> <me ...

  6. vue视频学习笔记05

    video 5 vue2.0:bower info vue http://vuejs.org/到了2.0以后,有哪些变化? 1. 在每个组件模板,不在支持片段代码组件中模板:之前:<templa ...

  7. CI 经常失败?可能是这 5 大原因…

    本文翻译自文章 Top 5 Reasons for CI Failure,主要介绍了 CI 失败的五个原因,包括 CI 服务的错误选择.CI 工程师的不专业性.随意更改CI服务器配置.CI服务器性能差 ...

  8. linux统计多个文件大小总和

    首先:查看当前文件夹大小的命令是: [root@host1 test]# du -sh 39M . 查看当前文件夹下所有文件的大小: [root@host1 test]# du -sh * 108K ...

  9. java面向对象--内部类

    将一个类定义在另一个类里面,里面的那个类称为内部类,与属性.方法等一样视作外部类的成员.内部类提供了更好的封装,不允许同包中的其他类访问该内部类. 内部类作为外部类的成员,同样可以被4个访问限定符修饰 ...

  10. 自定义控件pickView

    package com.example.healthembed.util; import java.util.ArrayList; import java.util.List; import java ...