搭建配置Spring
引入依赖
maven仓库查询网址:MavenRepository
spring基础包:
- spring-core:Core模块主要包含Spring框架基本的核心工具类,Spring的其他组件要都要使用到这个包里的类,Core模块是其他组件的基本核心
- spring-beans:包含访问配置文件、创建和管理bean以及进行IOC/DI操作相关的所有类
- spring-context:Spring的上下文即IOC容器,通过上下文可以获得容器中的Bean
- spring-expression:EL表达式语言用于在运行时查询和操纵对象
<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>
</dependencies>
刷新maven等待自动下载
libraries中有了所有导入的包表示依赖引入完成
核心配置文件
框架是一个半成品,已经封装好了很多功能提供我们使用,而我们如何让他们工作呢?
这里需要创建一个配置文件和Spirng框架通信,文件路径为\src\main\resources文件名为applicationContext.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"
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
">
<!-- bean definitions here -->
</beans>
把他复制到我们的配置文件后左上角会提示“Application context not configured for this file”,点击“Configure application context”,点击OK
编写代码测试
接口类
新建接口类
编写接口类
package services;
public interface UserService {
public void saveUser();
}
实现类
新建实现类
实现接口并使用快捷键添加接口方法实现
编写实现方法
package services.impl;
import services.UserService;
public class UserServiceImpl implements UserService {
public void saveUser() {
System.out.println("service的save方法执行了");
}
}
补充配置文件
将我们自定义的实现类交给Spring的容器管理
<!-- 此标签的作用是利用反射机制将UserServiceImpl类的实例交给Spring容器 -->
<bean id="userService" class="services.impl.UserServiceImpl"/>
测试类
新建测试类
编写测试类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();
}
}
Alt+Enter键导包
测试结果
可以看到控制台打印输出 证明确实从容器中获取到了userService实例
执行过程分析
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属性获取容器内的实例