一、环境
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
<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 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类
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类
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