1.新建Maven项目
(1)File → New → Project → Maven → 按默认直接NEXT
(2)填写项目组GroupId 及项目ArtifactId然后NEXT
(3)Project name项目名称IDEA会自动识别先前填写ArtifactId,修改好要存放的路径接着FINISH,空的Maven项目就建立完成了
(4)如果需要添加web模块
2.打开pom.xml文件添加Spring依赖
<!--添加属性,用于统一定义spring版本,以下spring依赖的版本version使用${spring.version}引用-->
<properties>
<spring.version>5.2.12.RELEASE</spring.version>
</properties>
<dependencies>
<!--spring核心依赖-->
<!--spring-core、spring-beans、spring-context-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring dao依赖(提供JDBC事务支持)-->
<!--spring-jdbc、spring-tx-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring web依赖-->
<!--spring-web、spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring test依赖-->
<!--spring-test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
第一次下载依赖包可能会比较久,可以使用阿里镜像
配置完成后在External Libraies包中应该有先前添加的依赖包文件
3.配置spring
在resources资源目录下创建spring.xml配置文件,当pom文件依赖注入成功时会有Spring Config选项
4.测试spring
(1)创建User测试对象,目录为src.main.java.com.cx.bean.User
package com.cx.bean;
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
(2)配置sping.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="user" class="com.cx.bean.User">
<property name="id" value="1"/>
<property name="username" value="张三"/>
<property name="password" value="111"/>
</bean>
</beans>
(3)编写测试代码
package com.cx.test;
import com.cx.bean.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
@Test
public void userTest() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
User user = (User)applicationContext.getBean("user");
System.out.println(user);
}
}
此处需要添加junit依赖
(4)测试结果
即在Maven项目搭建spring配置成功