Spring工程搭建

一.搭建配置Spring

创建项目:

1.使用IDEA创建Maven工程:
(1)打开开发工具IDEA,点击创建新项目在这里插入图片描述

(2)核实项目所使用的JDK,是否是已经安装好的JDK
(3)选择Maven工程 点击下一步在这里插入图片描述

(4)输入项目名spring-demo可以看到存储位置有自动追加spring-demo
IDEA配置Maven](这里写自定义目录标题)在这里插入图片描述
点击完成

2.IDEA配置Maven:
(1)File-Setting打开设置页

(2)搜索maven

(3)修改maven home directory为maven安装路径在这里插入图片描述
(4)勾选“Override”将setting文件位置及仓库位置改为自配置

(5)修改setting文件位置为 maven安装路径\conf\setting.xml

(6)修改仓库位置为 maven安装路径\repository在这里插入图片描述

二.工程搭建;

spring基础包:

spring-core:Core模块主要包含Spring框架基本的核心工具类,Spring的其他组件要都要使用到这个包里的类,Core模块是其他组件的基本核心
spring-beans:包含访问配置文件、创建和管理bean以及进行IOC/DI操作相关的所有类
spring-context:Spring的上下文即IOC容器,通过上下文可以获得容器中的Bean
spring-expression:EL表达式语言用于在运行时查询和操纵对象.

org.springframework spring-core 5.2.13.RELEASE org.springframework spring-beans 5.2.13.RELEASE org.springframework spring-context 5.2.13.RELEASE org.springframework spring-expression 5.2.13.RELEASE

刷新maven等待自动下载

libraries中有了所有导入的包表示依赖引入完成在这里插入图片描述

核心配置文件

框架是一个半成品,已经封装好了很多功能提供我们使用,而我们如何让他们工作呢?

这里需要创建一个配置文件和Spirng框架通信,文件路径为\src\main\resources文件名为applicationContext.xml
在这里插入图片描述
官方给出的配置文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<!-- bean definitions here -->

把他复制到我们的配置文件后左上角会提示“Application context not configured for this file”,点击“Configure application context”,点击OK
在这里插入图片描述

三.编写代码测试

接口类

新建接口类在这里插入图片描述
编写接口类

package services;

public interface UserService {
public void saveUser();
}
在这里插入图片描述
测试类

新建测试类在这里插入图片描述
编写测试类main方法

public class DemoTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”);
UserService service = (UserService) context.getBean(“userService”);
service.saveUser();
}
}
在这里插入图片描述

四.执行过程分析

BeanFactory

BeanFactory是基础类型的IOC容器,是管理bean容器的根接口,并提供了完整的IOC服务支持

简单来说BeanFactory就是一个管理Bean的工厂,它主要负责初始化各种Bean、调用生命周期等方法

ApplicationContext

ApplicationContext被称为应用上下文,是BeanFactory接口的子接口,在其基础上提供了其他的附加功能,扩展了BeanFactory接口

ClassPathXmlApplicationContext

ClassPathXmlApplicationContext是ApplicationContext的实现类,也在其基础上加了许多附加功能

该类从类路径ClassPath中寻找指定的XML配置文件,找到并完成对象实例化工作

其构造器源码如下:

public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[] {configLocation}, true, null);
}

public ClassPathXmlApplicationContext(
String[] configLocations,
boolean refresh,
@Nullable ApplicationContext parent)
throws BeansException {
super(parent);
// 加载项目中的Spring配置文件
setConfigLocations(configLocations);
if (refresh) {
// 刷新容器
refresh();
}
}
构造器的作用:

调用setConfigLocations方法加载项目中的Spring配置文件
调用refresh方法刷新容器(bean的实例化就在这个方法中)
refresh方法源码如下public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 准备容器刷新
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 准备bean工厂对象
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
// 加载配置文件中的所有bean标签
postProcessBeanFactory(beanFactory);


// Instantiate all remaining (non-lazy-init) singletons.
// 完成此上下文的bean工厂初始化,初始化所有剩余的单例bean
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
// 完成容器刷新
finishRefresh();
} catch (BeansException ex) {

} finally {

}
}
}
refresh方法的作用:

准备容器刷新
准备bean工厂对象
加载配置文件中的所有bean标签
完成bean工厂实例化
完成容器刷新
context.getBean()

context.getBean()方法是通过配置文件中声明的bean标签id属性获取容器内的实例

根据老师所教步骤,进行个人笔记撰写。可能有些错误或者内容不完全,望包涵!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值