1.创建一个maven项目。
不会配置maven环境的可以看这里:maven的下载安装与配置环境变量!!!(全网最详细)_明天更新的博客-CSDN博客
2.引入依赖
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>5.2.1.RELEASE</version></dependency></dependencies>
3.创建一个spring的配置文件
<?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="userEntity" class="com.entity.UserEntity"></bean>
</beans>
4.创建实体类
/** Copyright (c) 2020, 2023, All rights reserved.**/
package com.entity;/*** <p>Project: spring-dome - UserEntity</p>* <p>Powered by scl On 2023-09-10 15:43:25</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
public class UserEntity {public void show(){System.out.println("测试spring环境搭建!!!");}
}
5.获取Bean对象测试spring环境
/** Copyright (c) 2020, 2023, All rights reserved.**/import com.entity.UserEntity;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** <p>Project: spring-dome - Test01</p>* <p>Powered by scl On 2023-09-10 15:47:23</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
public class Test01 {public static void main(String[] args) {ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring.xml");UserEntity userEntity = classPathXmlApplicationContext.getBean("userEntity", UserEntity.class);System.out.println(userEntity);userEntity.show();}
}