1. 使用Maven创建project并配置依赖项

首先创建一个Maven Project:

然后选择创建Maven 的webapp实例,当然也能够通过命令行方式创建Maven webapp的项目再转化并导入到MyEclipse中。

在pom.xml中须要对于Spring依赖项进行配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>wx.spring</groupId>
<artifactId>wx.spring.helloworld</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>wx.spring.helloworld Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>wx.spring.helloworld</finalName>
</build>
<properties>
<spring.version>4.0.6.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>

2.框架配置(Spring MVC)

在本演示样例代码中,对于Spring MVC架构採取xml配置的方式,而对于IOC操作,採取Java Based Annotated方式。

l web.xml

web.xml是整个项目的整体配置文件,在该文件里须要制定项目Servlet的匹配方式以及整个项目的上下文的配置文件。在该web.xml文件里配置了一个Servlet,该Servlet是Spring MVC所须要的。而假设须要在Struts等其它框架中使用Spring,就须要声明一个listener。换言之,dispatcher-servlet与applicationContext是应用于不同场景的同样的配置文件。

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>Archetype Created Web Application</display-name>

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>

org.springframework.web.servlet.DispatcherServlet

</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

</web-app>

2 dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

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-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="wx.spring.helloworld.controller" />

<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix">

<value>/WEB-INF/views/</value>

</property>

<property name="suffix">

<value>.jsp</value>

</property>

</bean>

</beans>

3 applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

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-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="wx.spring.helloworld.controller" />

<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix">

<value>/WEB-INF/views/</value>

</property>

<property name="suffix">

<value>.jsp</value>

</property>

</bean>

</beans>

3.代码

1 HelloWorldController

package wx.spring.helloworld.controller;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

@Controller

public class HelloWorldController {

@RequestMapping("/hello")

public String hello(

@RequestParam(value = "name", required = false, defaultValue = "World") String name,

Model model) {

model.addAttribute("name", name);

return "helloworld";

}

}

2 /WEB-INF/views/helloworld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Spring4 MVC -HelloWorld</title>

</head>

<body>

<h1>Hello : ${name}</h1>

</body>

</html>

3 MessagePrinter.java:服务接口类,详细的功能实现依赖于MessageService接口。

该类中的MessageService成员,会在执行时由Spring自己主动注入,而不须要自身初始化。

package wx.spring.helloworld.bean;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

@Component

public class MessagePrinter {

private MessageService service;

@Autowired

public void setMessageService(MessageService service) {

this.service = service;

}

public void printMessage() {

System.out.println(this.service.getMessage());

}

}

4 MessageService:接口类

package wx.spring.helloworld.bean;

public interface MessageService {

String getMessage();

}

5 MyConfiguration:配置类

package wx.spring.helloworld.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import wx.spring.helloworld.bean.MessagePrinter;

import wx.spring.helloworld.bean.MessageService;

@Configuration

@ComponentScan(value="wx.spring.helloworld.test")

public class MyConfiguration {

@Bean

MessageService getMessageService() {

return new MessageService() {

public String getMessage() {

return "Hello World!IOC&DI";

}

};

}

@Bean

MessagePrinter getMessagePrinter(){

return new MessagePrinter();

}

}

6 Application

package wx.spring.helloworld.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.*;

import wx.spring.helloworld.bean.MessagePrinter;

import wx.spring.helloworld.bean.MessageService;

import wx.spring.helloworld.config.MyConfiguration;

public class Application {

public static void main(String[] args) {

ApplicationContext context =

new AnnotationConfigApplicationContext(MyConfiguration.class);

MessagePrinter printer = context.getBean(MessagePrinter.class);

printer.printMessage();

}

}

5.执行

首先执行mvn package命令将整个项目打包,然后右击选择在Tomcat中执行。最好设置下文件编译的输出:


项目直接执行时,显示的是MVC的结果:

直接执行Main Application:

Spring FrameWork4(MVC + IOC)高速入门实例的更多相关文章

  1. Spring中IoC的入门实例

    Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...

  2. Spring学习笔记IOC与AOP实例

    Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...

  3. Spring.Net---1、IOC第一个实例

    Spring.NET IoC容器的用法. 通过简单的例子学习Spring.Net 1.先创建一个控制台程序项目. 2.添加IUserInfoDal 接口. namespace Spring.Net { ...

  4. Spring核心(IoC) 入门解读

    什么是IoC容器和Bean? 说到容器,就是用来装东西,那么这里边装的是什么那?是bean对象.那么你会问什么是bean?这就要从很久很久以前说起,当我们创建对象的时候,我们会new一个对象出来,但是 ...

  5. Spring入门1. IoC入门实例

    Spring入门1. IoC入门实例 Reference:Java EE轻量级解决方案——S2SH 前言: 之前学习过关于Spring的一点知识,曾经因为配置出现问题,而总是被迫放弃学习这些框架技术, ...

  6. Spring MVC 教程,快速入门,深入分析

    http://elf8848.iteye.com/blog/875830/ Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门  资源下载: ...

  7. Spring - Sring MVC入门

    2.1.Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职 ...

  8. Spring oxm入门实例

    O/XMapper是什么? Spring3.0的一个新特性是O/XMapper.O/X映射器这个概念并不新鲜,O代表Object,X代表XML.它的目的是在Java对象(几乎总是一个plainoldJ ...

  9. Spring初识及其IOC入门

    一.框架 框架是一些类和接口的集合,它一个半成品,已经对基础的代码进行了封装并提供相应的API,开发者在使用框架时直接调用封装好的api可以省去很多代码编写,从而提高工作效率和开发速度. 二.Spri ...

随机推荐

  1. hdu4283(区间dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4283 题意:有一个队列,每个人有一个愤怒值D,如果他是第K个上场,不开心指数就为(K-1)*D.但是边 ...

  2. SQL SERVER中的流程控制语句

    流程控制语句 是指用来控制程序运行和流程分至点额命令.一般指的是逻辑计算部分的控制. 1.Begin End语句 封装了多个T-SQL语句组合,将他们组成一个单元来处理. 一般在条件查询或者循环等控制 ...

  3. 我的Android学习之旅(转)

    去年大概在七月份的时候误打误撞接触了一阵子Android,之后由于工作时间比较忙,无暇顾及,九月份的时候自己空闲的时间比较多,公司相对来说加班情况没以前严重.开启了个人的Android学习之旅,初衷是 ...

  4. CSDN开源夏令营 百度数据可视化实践 ECharts(8)问题分析

    ECharts问题描写叙述: 问题就是折线图上的点是显示的,有人问能不能一開始不显示,当你点击的时候或者是当鼠标移动到上面的时候,折线上的点才显示? 例如以下图所看到的: 分析:让折线上的点不显示能够 ...

  5. WPF界面设计技巧(4)—自定义列表项样式

    原文:WPF界面设计技巧(4)-自定义列表项样式 有前面修改按钮样式的基础,我们可以尝试来定制一个即好看又好用的 ListBox ,今天先来讲“好看”部分. 打开 Microsoft Visual S ...

  6. python学习笔记之二:使用字符串

    这里会介绍如何使用字符串格式化其他的值,并了解一下利用字符串的分割,连接,搜索等方法能做些什么. 1.基本字符串操作 所有标准的序列操作(索引,分片,乘法,判断成员资格,求长度,取最大值和最小值)对字 ...

  7. 为什么不要在android或者ios上直连mysql或者sqlserver之类的数据库(跳大神)

    很多同学 都有直连这些数据库的想法,假设我说了下面二个问题之后你还想直连,那我也没办法 数据库是一个服务端最重要的部分,也是最脆弱的部分,更是最敏感的部分 假设直连会造成例如以下问题 1.安全问题,你 ...

  8. Windows Phone开发(5):室内装修

    原文:Windows Phone开发(5):室内装修 为什么叫室内装修呢?呵呵,其实说的是布局,具体些嘛,就是在一个页面中,你如何去摆放你的控件,如何管理它们,你说,像不像我们刚搬进新住所,要&quo ...

  9. 【原创】poj ----- 1182 食物链 解题报告

    题目地址: http://poj.org/problem?id=1182 题目内容: 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  10. [WebView五学习]:调试Web Apps

    上一篇我们学习了([WebView学习之四]:迁移到Android4.4版本号的WebView),今天我们来继续学习. (博客地址:http://blog.csdn.net/developer_jia ...