本节主要介绍如何利用Maven搭建 Spring 开发环境,使用 Spring 之前需要安装 JDK 、Maven和 IDEA

建议一定要从 Maven 项目开始,而不是从空项目开始,空项目开始会出现很多奇怪的错误

  1. JDK 安装

    能看到这篇文章的大概都有(

  2. Maven 安装和配置(IDEA 版)

    详情:Here

  3. 编写 pom.xml

    在工程的根目录找到 pom.xml 如果没有就创建一个,在文件中添加如下代码,保存后会自动下载相关 jar 包

    <?xml version="1.0" encoding="UTF-8"?>
    <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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
    <artifactId>MySpring</artifactId>
    <version>1.0-SNAPSHOT</version> <properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <packaging>war</packaging>
    <dependencies>
    <!-- Spring 依赖包 -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.5.RELEASE</version>
    </dependency>
    <!-- junit 单元测试包 -->
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
    </dependency>
    </dependencies> </project>
  4. 配置 bean 的 xml 文件

    在工程的源码目录(resources)下 创建Spring Config 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userDao" class="com.riotian.dao.impl.UserDaoImpl" scope="prototype" ></bean>
    <!-- id 唯一标识 -->
    <!-- 现在可以不用关注 scope -->
    </beans>
    <!--文件名字都可以 只是约定熟成为 applicationContext-->
  5. 编写一个测试类

    在 IDEA 中新建一个接口和实现类,如 com.riontian.dao.UserDao 接口以及 com.riotian.dao.impl.UserDaoImpl ,具体代码如下:

    // UserDao.java
    package com.riotian.dao; public interface UserDao {
    public void save();
    }
    // UserDaoImpl.java
    package com.riotian.dao.impl; import com.riotian.dao.UserDao; public class UserDaoImpl implements UserDao {
    // 默认构造函数
    public UserDaoImpl() {
    System.out.println("UserDaoImpl 创建....");
    } @Override
    public void save() {
    System.out.println("save running....");
    }
    }
  6. 验证 Spring 是否配置正确

    我们定义完spring的配置后,新建一个测试类,只需要按照下面的代码即可获取到“DaoUser”实例对象,调用代码如下:

    package com.riotian.demo;
    
    import com.riotian.dao.UserDao;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext; // Spring 入门程序
    public class UserDaoDemo {
    public static void main(String[] args) {
    ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserDao userDao = (UserDao) app.getBean("userDao");
    userDao.save();
    }
    }

Spring | 利用Maven搭建Spring的开发环境的更多相关文章

  1. 利用Docker搭建java项目开发环境

    一.需求 一台 Ubuntu 16.0.4 LTS ,安装了Docker服务,Rancher服务,也制作了Tomcat相关的image,接下来我们就来说一下如何快速的构建一个开发环境和测试环境 二.步 ...

  2. 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解

    http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...

  3. 搭建基于SSI(struts2,spring,ibatis)的javaEE开发环境

    搭建基于SSI(struts2,spring,ibatis)的javaEE开发环境 最近有很多人不知道如何搭建基于SSI(struts2,spring,ibatis)的J2EE开发环境,这里给大家一个 ...

  4. Myeclipse下使用Maven搭建spring boot项目

    开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...

  5. IDEA使用maven搭建spring项目

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

  6. 使用IntelliJ IDEA和Maven管理搭建+Web+Tomcat开发环境

    使用IntelliJ IDEA和Maven管理搭建+Web+Tomcat开发环境 前言:原来一直使用Eclipse,换工作后使用IDEA,初识IDEA发现,哇,它的快捷键可真多啊,但是一路用下来,觉得 ...

  7. Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创

    原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...

  8. Maven 搭建spring boot多模块项目

    Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...

  9. Maven搭建Spring Security3.2项目详解

    本来是打算在上一篇SpringMVC+Hibernate上写的,结果发现上面那篇 一起整合的,结果发现上一篇内容实在是太长了,就另起一篇,这篇主要是采用 Maven搭建Spring+SpringMVC ...

  10. 如何在Eclipse中搭建MyBatis基本开发环境?(使用Eclipse创建Maven项目)

    实现要求: 在Eclipse中搭建MyBatis基本开发环境. 实现步骤: 1.使用Eclipse创建Maven项目.File >> New >> Maven Project ...

随机推荐

  1. 🔥🔥Java开发者的Python快速进修指南:自定义模块及常用模块

    好的,按照我们平常的惯例,我先来讲一下今天这节课的内容,以及Java和Python在某些方面的相似之处.Python使用import语句来导入包,而Java也是如此.然而,两者之间的区别在于Pytho ...

  2. 如何自学 PS、PR 等软件?

    学习Photoshop(PS)和Premiere Pro(PR)等软件需要一定的时间和耐心,以下是非常详细的自学指南. 第一部分:规划学习路线 1. 确定学习目标 在自学PS和PR之前,首先要明确自己 ...

  3. The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission,iphone手机video标签报错

    The request is not allowed by the user agent or the platform in the current context, possibly becaus ...

  4. Java自定义ClassLoader实现插件类隔离加载

    为什么需要类隔离加载 项目开发过程中,需要依赖不同版本的中间件依赖包,以适配不同的中间件服务端 如果这些中间件依赖包版本之间不能向下兼容,高版本依赖无法连接低版本的服务端,相反低版本依赖也无法连接高版 ...

  5. [ABC235G] Gardens

    Problem Statement Takahashi has $A$ apple seedlings, $B$ banana seedlings, and $C$ cherry seedlings. ...

  6. Diffusion Model扩散模型

    1.扩散模型基本原理: 扩散模型包括两个步骤: 固定的(或预设的)前向扩散过程q:该过程会逐渐将高斯噪声添加到图像中,直到最终得到纯噪声. 2.可训练的反向去噪扩散过程pθ:训练一个神经网络,从纯噪音 ...

  7. bash shell笔记整理——tail命令

    作用 Print the last 10 lines of each FILE to standard output. With more than one FILE, precede each wi ...

  8. ubuntu 20.0.4 LTS 配置国内apt-get源

    https://blog.csdn.net/wangyijieonline/article/details/105360138 更换阿里源 要知道当前系统的代号,可以用以下命令: lsb_releas ...

  9. ORA-28579 在从外部过程代理程序回调时,发生网络错误,ORA-06512 在"SDE.ST_GEOMETRY_SHAPELIB_PKG"

    如图所示,在执行sde.st_transform方法时报错. 环境是arcgis10.8  oracle是12C,版本号是v12.1.0.2.0 但是执行ST_GEOMETRY方法是可以的,说明配置没 ...

  10. vue上传文件显示进度条,当上传完成后间隔一秒进度条消失

    <template> <el-upload class="avatar-uploader" action="api/file/upload" ...