参考原文: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. com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼

    起初这样能短暂解决问题,后来发现每次机器重启了就还是有这样的错误,还是要执行SQL,很麻烦: show variables like '%time_zone%'; select now(); set ...

  2. 帮助大家理解java中的随机和继承,动态绑定.

    package com.ykmimi.javabianchengsixiang; /** * 形状的继承 随机形状生成器 * @author ukyor */ import java.util.Ran ...

  3. 项目梳理4——WebApi项目,使用注释填充Description字段

    web.config中添加连接字符串: 为webapi添加Description,使用注释来填充此字段 对于所有引用的xxxx.base项目设置生成的xml文档,注意release.debug下都需设 ...

  4. bind9安装配置

    BIND的安装配置: dns服务,程序包名叫bind,程序名named 程序包: bind bind-libs bind-utils bind-chroot: /var/named/chroot/ b ...

  5. AtCoder square869120 Contest #3 F sushi

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  6. Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song 算贡献+前缀和

    E. Pretty Song time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. pyqt 调用颜色选择器

    # -*- coding: utf- -*- from PyQt5.QtWidgets import QApplication, QPushButton, QColorDialog , QWidget ...

  8. idea运行main方法报错,提示Shorten command line for xxx

    在Intell IDEA运行main函数的时候遇到了如下错误: Error running' xxxxxx': Command line is too long. Shorten command li ...

  9. angular5表单验证问题

    例举一个patten的列子 可能出现的问题,表单元素需要添加name属性 还有的验证如maxlength,minlength,required等 一.验证某一个表单元素如下 *ngIf="s ...

  10. nginx常用命令汇总

    nginx基础命令: sudo nginx // 开启nginx服务器 sudo nginx -s reload // 重启nginx服务器 sudo nginx -s stop // 关闭nginx ...