LOADING

加载过慢请开启缓存 浏览器默认开启

BeanFactory、ApplicationContext简介

2023/4/7 Java 框架

BeanFactory创建Bean实例对象

示例代码:

xml配置文件

1
2
3
4
5
6
<?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="userService" class="com.zy.service.impl.UserServiceImpl"></bean>
</beans>

userService接口

1
2
3
4
package com.zy.service;

public interface UserService {
}

userServiceImp

1
2
3
4
5
6
7
8
9
10
11
12
package com.zy.service.impl;

import com.zy.service.UserService;

/**
* @author Administrator
* @version 1.0
* @description TODO
* @date 2023/4/7 8:56
*/
public class UserServiceImpl implements UserService {
}

核心方法代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.zy.test;

import com.zy.service.UserService;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

/**
* @author Administrator
* @version 1.0
* @description TODO
* @date 2023/4/7 9:04
*/
public class BeanFactoryTest {
public static void main(String[] args) {
//创建工厂
DefaultListableBeanFactory beanFactory=new DefaultListableBeanFactory();
//创建一个读取器(xml)
XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(beanFactory);
//读取配置文件给工厂
reader.loadBeanDefinitions("beans.xml");
//根据id获取Bean实例对象
UserService userService = (UserService) beanFactory.getBean("userService");
System.out.println(userService);
}
}

ApplicationCOntext快速入门

ApplicationContext称为Spring容器,内部封装了BeanFactory,比BeanFactory功能更强大更丰富,使用ApplicationContext进行开发时,xml配置文件的名称习惯写成applicationContext.xml

xml配置文件代码:

1
2
3
4
5
6
<?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="userService" class="com.zy.service.impl.UserServiceImpl"></bean>
</beans>

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.zy.test;

import com.zy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author Administrator
* @version 1.0
* @description TODO
* @date 2023/4/7 16:18
*/
public class ApplicationContextTest {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
System.out.println(userService);
}
}

- BeanFactory与ApplicationContext的关系

  1. BeanFactory是Spring的早期接口,称为Spring的Bean工厂,ApplicationEontext是后期更高级接口,称之为Spring容器;

2)ApplicationContext在BeanFactory基础上对功能进行了扩展,例如:监听功能、国际化功能等。BeanFactory的API更偏向底层,ApplicationContext的API大多数是对这些底层API的封装;

3)Bean创建的主要逻辑和功能都被封装在BeanFactory中,ApplicationContext不仅继承了BeanFactory,而且ApplicationContext内部还维护着BeanFactory的引用,所以,ApplicationContext与BeanFactory既有继承关系,又有融合关系。

  1. Bean的初始化时机不同,原始BeanFactory是在首次调用getBean时才进行Bean的创建,而ApplicationContext则是配置文件加载,容器一创建就将Bean都实例化并初始化好。