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. seajs模块标识命名和解析规则

    模块标识采用路径形式,但要注意与路径的区别.require.require.async的第一个参数是模块标识.而seajs.use第一个参数为文件路径. use是全局的,require是局部的.模块标 ...

  2. springmvc 导出excel

    1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  3. 【Netty】第一个Netty应用

    一.前言 前面已经学习完了Java NIO的内容,接着来学习Netty,本篇将通过一个简单的应用来了解Netty的使用. 二.Netty应用 2.1 服务端客户端框架图 下图展示了Netty中服务端与 ...

  4. NSLog (Log信息的输出)

    概述 NSLog是 cocoa的框架中提供的一个方法 NSLog的定义 NSLog定义在NSObjCRuntime.h中 NSLog与printf的差异 1)NSLog传递进去的格式化字符是NSStr ...

  5. [刷题]算法竞赛入门经典(第2版) 5-4/UVa10763 - Foreign Exchange

    题意:有若干交换生.若干学校,有人希望从A校到B校,有的想从B到C.C到A等等等等.如果有人想从A到B也刚好有人想从B到A,那么可以交换(不允许一对多.多对一).看作后如果有人找不到人交换,那么整个交 ...

  6. RabbitMQ集群和失败处理

    RabbitMQ内建集群的设计用于完成两个目标:允许消费者和生产者在RabbitMQ节点在奔溃的情况下继续运行,以及通过添加更多的节点来线性扩展消息通信的吞吐量.当失去一个RabbitMQ节点时客户端 ...

  7. 【转】解决response.AddHeader("Content-Disposition", "attachment; fileName=" + fileName) 中文显示乱码

    如果fileName为中文则乱码.解决办法是 方法1: response.setHeader("Content-Disposition", "attachment; fi ...

  8. Charles Proxy代理使用简要说明

    1.去官网下载免费试用版: http://www.charlesproxy.com/ (需要机器有Java运行时)或下载破解注册版:http://charles.iiilab.com/,安装后开启默认 ...

  9. 最优雅SSM框架:SpringMVC + Spring + MyBatis

    在写代码之前我们先了解一下这三个框架分别是干什么的? 相信大以前也看过不少这些概念,我这就用大白话来讲,如果之前有了解过可以跳过这一大段,直接看代码! SpringMVC:它用于web层,相当于con ...

  10. 《Android进阶》之第七篇 NDK的使用

    <Android进阶>之第一篇 在Java中调用C库函数 这一篇列举的方法是在NDK没有出来时候用的方式 在Android发布NDK之后,可以这样使用 一.首先下载android-ndk ...