参考原文:http://blog.csdn.net/binyao02123202/article/details/20387595

1.新建maven web 工程

2.编辑pom.xml添加依赖

                <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>

  

3.配置spring监听器

编辑web.xml添加监听器

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name> <!-- 设置Spring容器加载所有的配置文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param> <!-- 加载Spring容器配置,Spring监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>

  

4. applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd "> <bean id="person" class="com.yun.entity.Person">
<property name="name" value="张三"></property>
<property name="age" value="33"></property>
</bean> <bean id="helloWorld" class="com.yun.util.test.impl.IHelloWorldImpl">
<property name="person" ref="person"></property>
</bean> </beans>

  

5.编写测试类

package com.yun.entity;

public class Person {

	private String name;

	private int age;

	public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }

  

接口 IHelloWorld

package com.yun.util.test;

public interface IHelloWorld {

	String sayHello();

}

  

接口实现 IHelloWorldImpl

package com.yun.util.test.impl;

import com.yun.entity.Person;
import com.yun.util.test.IHelloWorld; public class IHelloWorldImpl implements IHelloWorld{ private Person person; public String sayHello() {
return "hello world!"+person.getName()+",i am "+person.getAge();
} public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} }

 

测试类 JunitTest

package com.yun.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yun.util.test.IHelloWorld; public class JunitTest { @Test
public void test(){
System.out.println("......................");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IHelloWorld helloWorld = (IHelloWorld)context.getBean("helloWorld");
System.out.println(helloWorld.sayHello());
}
}

  运行结果

6.工程中应用了 junit 如下添加junit

选中工程右键-->Properties-->Java Build Path-->add Library-->JUnit-->next

7.如果想透彻理解,请参考博文开头的参考文章。

工程将在百度云盘中给出。

百度云: http://pan.baidu.com/s/1pLvvxQb

搭建 spring 项目的更多相关文章

  1. IDEA使用maven搭建spring项目

    spring框架 Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅仅限于服务器端的开发.从简单 ...

  2. 搭建spring项目,无法创建RequestMappingHandlerMapping异常

    异常详情: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMa ...

  3. 使用java配置来构建spring项目

    java配置是Spring4.x推荐的配置方式,可以完全代替xml配置,java配置是通过@Configuration和@Bean来实现的.@Configuration声明当前类是一个配置类,相当于S ...

  4. 搭建spring mvc项目

    在之前搭建maven项目这篇的基础上继续集成,引入spring mvc支持 一.添加jar包引用 修改pom.xml文件,加入:(其他关联的jar包maven会自动引用) <!-- 项目属性 - ...

  5. Spring MVC 项目搭建 -1- 创建项目

    Spring MVC 项目搭建 -1- 创建项目 1.创建 Dynamic Web project (SpringDemo),添加相关jar包 2.创建一个简单的controller类 package ...

  6. 使用IDEA搭建Spring Boot入门项目

    简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置 ...

  7. Spring Boot入门-快速搭建web项目

    Spring Boot 概述: Spring Boot makes it easy to create stand-alone, production-grade Spring based Appli ...

  8. maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

    项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发 项目的截图: 搭建开始: 能上图 我少打字 1.首先搭建maven的聚合工程 ...

  9. Spring Boot搭建Web项目常用功能

    搭建WEB项目过程中,哪些点需要注意: 1.技术选型: 前端:freemarker.vue 后端:spring boot.spring mvc 2.如何包装返回统一结构结果数据? 首先要弄清楚为什么要 ...

随机推荐

  1. stm32 pwm 电调 电机

    先上代码 python 树莓派版本,通俗表现原理.stm32 C语言版本在后面 import RPi.GPIO as GPIO import time mode=2 IN1=11 def setup( ...

  2. Elasticsearch 原理

    Elasticsearch简介 Elasticsearch是一个基于Apache lucene的实时分布式搜索.具有以下优点: 1.实时处理大规模数据.2.全文检索,能够做到结构化检索和聚合分析.3. ...

  3. poj 1523 SPF 无向图求割点

    SPF Description Consider the two networks shown below. Assuming that data moves around these network ...

  4. C# 集合-并发处理-锁OR线程 (转载)

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  5. python 时间元组转时间戳

    #!/usr/bin/python # -*- coding: UTF- -*- import time print(time.mktime((, , , , , , , , ))) 输出 15382 ...

  6. Ubuntu 16.04 kinetic 编译指定包

    编译指定包 catkin_make -DCATKIN_WHITELIST_PACKAGES=baoming 使用上述命令后catkin_make会一直编译上面那个包,想要编译全部包,使用 catkin ...

  7. c++ 多继承 public

    以下代码会报错 #include <iostream> using namespace std; class Sofa { public: Sofa(); ~Sofa(); void si ...

  8. 学以致用 ---- vue子组件→父组件通信

    之前写过一篇关于 vue2.0中v-on绑定自定义事件 的随笔,但是今天实际应用的时候才发现根本就不理解,下面是实际工作中遇到的问题: [情景描述]页面中的[下拉搜索组件],因为多个页面中用到,所以抽 ...

  9. STL_算法_03_拷贝和替换算法

    ◆ 常用的拷贝和替换算法: 1.1.复制(容器A(全部/部分) 复制到 容器B(全部/部分)),返回的值==>iteratorOutBegin.end() iterator copy(itera ...

  10. 《剑指offer》第六题(重要!从尾到头打印链表)

    文件main.cpp // 从尾到头打印链表 // 题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值. #include <iostream> #include <sta ...