前言

简单的说一下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架构实战电子书等等!欢迎关注公众号:前程有光,自行下载!


前程有光
936 声望618 粉丝