前言
简略的说一下spring的搭建流程,内容偏根底,底子不好的能够看一下!
配置Maven
如图所示:找到之前筹备好的文件门路
流程
1、新建UserService接口
2、新建UserService实现类
3、通过bean将UserService放入容器
4、通过context的getbean办法拿到UserService对象
加载根底包:
代码如下:
<dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-core --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.13.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.13.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.13.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.2.13.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.13.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-jcl --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-jcl</artifactId> <version>5.2.13.RELEASE</version></dependency>
后刷新Maven.
配置文件
内容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd "> </beans>
点右上角提醒的“Configure application context”就能够了。
编写代码
接口类
代码如下
新建class文件UserServiceImpl
实现
配置文件
写mian办法
public class DemoTest {public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext ("classpath:applicationContext.xml"); UserSvice service = (UserSvice) context.getBean("userService"); service.saveUser();}}
如图:
运行mian办法,
测试后果为
最初
在文章的最初作者为大家整顿了很多材料!包含java外围知识点+全套架构师学习材料和视频+一线大厂面试宝典+面试简历模板+阿里美团网易腾讯小米爱奇艺快手哔哩哔哩面试题+Spring源码合集+Java架构实战电子书等等!欢送关注公众号:前程有光,自行下载!