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. JAVA控制台版斗地主

    一.核心思路: 1.首先分析流程: A>B>C>A>B>C>A>B>C 等于  while(true){ A>B>C } 然后完善细节 发牌 ...

  2. unity游戏设计之背包系统

    这次任务是模仿上图的样子,制作一个类似的背包系统. 上面的链接为:http://www.tasharen.com/ngui/exampleX.html 我们的目标是: 1.实现背包系统的UI界面 2. ...

  3. linux 内核的各种futex

    futex 设计成用户空间快速锁操作,由用户空间实现fastpath,以及内核提供锁竞争排队仲裁服务,由用户空间使用futex系统调用来实现slowpath.futex系统调用提供了三种配对的调用接口 ...

  4. FileZilla可以连接但是传输文件失败

    在linux本地创建文件夹后,用FileZilla传输文件失败了. 如果用的是普通用户,创建的文件夹是属于root用户.所以不能传输,没有权限. 修改权限:sudo chown -R lishengn ...

  5. hdu1150 Machine Schedule 经典二分匹配题目

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1150 很经典的二分题目 就是求最小点覆盖集 二分图最小点覆盖集=最大匹配数 代码: #include& ...

  6. hdu1312 Red and Black 简单BFS

    简单BFS模版题 不多说了..... 直接晒代码哦.... #include<cstdlib> #include<iostream> #include<cstdio> ...

  7. Redis学习-持久化

    Redis 提供了多种不同级别的持久化方式: RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AOF 持久化记录服务器执行的所有写操作命令 ...

  8. yaf学习之——生成yaf示例框架

    windows 下面的生成yaf示例框架 1,下载 https://github.com/laruence/yaf 2,解压以后得到yaf-master文件夹 3,cmd命令窗口cd到上面解压的文件夹 ...

  9. 如何自学成为一个WEB前端

    WEB前端是做什么的? 那些什么高大上的介绍作者就略过了,简单来说就是做网页的,我们上网浏览的网站界面就是WEB前端工程师做的. 在互联网迅速发展的近几年,你上网冲浪的时候是不是感觉WEB网站越来越漂 ...

  10. Day3-递归函数、高阶函数、匿名函数

    一.递归函数 定义:函数内部可以调用其它函数,如果调用自身,就叫递归. 递归特性: 1.必须有结束条件退出: >>> def calc(n): ... print(n) ... re ...