idea创建spring项目

一、环境

window10

IDEA 2022.2.3

maven-3.8.6

二、创建spring项目

1、新建Maven项目

File -> New -> Project···

然后如下图选中Maven Archetype,在Archetype,选中maven-archetype-webapp,点击Create

2、配置maven

默认是使用IDEA内置的maven,此外还可以配置本地安装的maven

File -> Settings...

3、目录结构如下

4、在main目录下新建java文件

java文件夹,用来存放我们的源码,在java文件下右键make directory as 选择Sources root 目录

5、在src目录下创建test文件

test文件夹,用来存放我们的测试源码,在test文件下右键make directory as 选择Test sources root 目录

6、添加Spring pom依赖

添加以下依赖,如果jar包没有依赖到项目,在pom文件上右键,选择Maven->reload project

xml 复制代码
	<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.10</version>
    </dependency>

7、在resource目录下新建一个spring文件夹

在resource目录下新建一个spring文件夹,在spring文件夹中创建一个applicationContext.xml文件

配置applicationContext.xml

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="helloWord" class="HelloWorld">
        <property name="message" value="hello world" />
    </bean>
</beans>

8、创建类

在java目录下创建Package包,名称为modules

modules包下创建HelloWorld.java类

java 复制代码
package modules;

public class HelloWorld {
    private String message;
    public void setMessage(String message){
        this.message  = message;
    }
    public void getMessage(){
        System.out.println("Your Message : " + message);
    }
}

java目录下创建Application.java类

java 复制代码
import modules.HelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
    }
}

9、jar路径

在Project Structure(File->Project Structure)窗口,在Output Layout标签中找到Available Elements,去右键选择put into output root

10、配置Tomcat

点击 Edit Configurations...

server中点configure中配置Tomcat安装路径

Deployment中点击+,添加artifacts,选择带exploded的,然后OK

11、启动Tomcat


相关推荐
HanhahnaH7 分钟前
Spring集合注入Bean
java·spring
未定义.22113 分钟前
电子削铅笔刀顺序图详解:从UML设计到PlantUML实现
java·软件工程·uml
雾月5530 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
小妖6661 小时前
vscode vue文件单行注释失效解决办法
ide·vscode·编辑器
24k小善1 小时前
Flink TaskManager详解
java·大数据·flink·云计算
想不明白的过度思考者2 小时前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
.生产的驴2 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
Geek__19922 小时前
VSCode远程图形化GDB
ide·vscode·编辑器
猿周LV2 小时前
JMeter 安装及使用 [软件测试工具]
java·测试工具·jmeter·单元测试·压力测试
晨集2 小时前
Uni-App 多端电子合同开源项目介绍
java·spring boot·uni-app·电子合同