一、基础概念
Spring框架最基本的接口就是BeanFactory,负责创建、管理、配置Bean;
确切来说,BeanFactory就相当于Spring的心脏,而ApplicationContext就相当于整个身躯,由BeanFactory派生而来,负责实现各种功能。
二。案例
先创建一个实体类
@Data
public class Entity{
private String id;
private String name;
private Integer age;
}
接着创建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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- singleton-->
<bean id="EntitySingleton" class="com.xiaoniuma.demo.po.Entity" scope="singleton">
<property name="id" value="1"/>
<property name="name" value="Java小牛马"/>
<property name="age" value="22"/>
</bean>
<!-- prototype-->
<bean id="UserPrototype" class="com.xiaoniuma.demo.po.Entity" scope="prototype" >
<property name="id" value="2"/>
<property name="name" value="Java小马牛"/>
<property name="age" value="23"/>
</bean>
</beans>
接着创建一个测试类:
import com.chenxi.demo.po.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author: Java小牛马
* @create: 2023-11-24 13:21
*/
public class ApplicationContextDemo {
//打印日志
private static final Logger logger = LoggerFactory.getLogger(ApplicationContextDemo.class);
public static void main(String[] args) {
/**
*
* ApplicationContext体系结构
* 主要实现类:
* ClassPathXmlApplicationContext:默认从类路径加载配置文件
* FileSystemXmlApplicationContext:默认从文件系统中装载配置文件
*/
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
* Singleton:单实例(在容器启动完成之前就已经创建好了,保存在容器中了,任何时候获取都是获取之前创建好的那个对象)
*/
Entity entity = (Entity) applicationContext.getBean("EntitySingleton");
Entity entity1 = (Entity) applicationContext.getBean("EntitySingleton");
Entity entity2 = applicationContext.getBean("EntitySingleton",User.class);
logger.info("entity.hashCode()是:{}",entity.hashCode());
logger.info("entity1.hashCode()是:{}",entity1.hashCode());
logger.info("entity是:{}",entity);
logger.info("entity1是:{}",entity1);
logger.info("entity == entity1 :{}",entity == entity1);
/**
* Prototype:多实例(容器启动默认不会创建多实例bean对象,只有在获取的时候才创建,每次获取都会创建一个新的实例对象)
*/
Entity entity3 = (Entity) applicationContext.getBean("EntityPrototype");
Entity entity4 = (Entity) applicationContext.getBean("EntityPrototype");
logger.info("entity3.hashCode()是:{}",entity3.hashCode());
logger.info("entity4.hashCode()是:{}",entity4.hashCode());
logger.info("entity3是:{}",entity3);
logger.info("entity4是:{}",entity4);
logger.info("entity3 == entity4 :{}",entity3 == entity4);
}
}
运行,查看控制台日志:
13:27:32.921 [main] INFO com.xiaoniuma.demo.utils.ApplicationContextDemo - entity.hashCode()是:1446002
13:27:32.922 [main] INFO com.xiaoniuma.demo.utils.ApplicationContextDemo - entity1.hashCode()是:1446002
13:27:32.922 [main] INFO com.xiaoniuma.demo.utils.ApplicationContextDemo - entity是:User(id=1, name=Java小牛马, age=22)
13:27:32.922 [main] INFO com.xiaoniuma.demo.utils.ApplicationContextDemo - entity1是:User(id=1, name=Java小牛马, age=22)
13:27:32.922 [main] INFO com.xiaoniuma.demo.utils.ApplicationContextDemo - entity == entity1 :true
22:00:32.923 [main] INFO com.xiaoniuma.demo.utils.ApplicationContextDemo - entity3.hashCode()是:266875004
13:27:32.923 [main] INFO com.xiaoniuma.demo.utils.ApplicationContextDemo - entity4.hashCode()是:266875004
13:27:32.923 [main] INFO com.xiaoniuma.demo.utils.ApplicationContextDemo - entity3是:Entity(id=2, name=Java小牛马, age=23)
13:27:32.923 [main] INFO com.xiaoniuma.demo.utils.ApplicationContextDemo - entity4是:Entity(id=2, name=Java小牛马, age=23)
13:27:32.923 [main] INFO com.xiaoniuma.demo.utils.ApplicationContextDemo - entity3 == entity4 :false
可以看到,整一个的代码逻辑是先创建Spring的工厂类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
再解析xml文件获取Bean实例(相当于从容器直接拿出来用)
Entity entity = (Entity) applicationContext.getBean("EntityPrototype");
通过这个案例可以了解到,Context被称为Spring上下文环境的原因,其实称ApplicationContext为应用的容器更为接近一些。
三、总结
以下是ApplicationContext一些特性和功能:
Bean管理:ApplicationContext负责管理应用程序中的所有Bean,包括实例化、配置、装配和生命周期管理等。
依赖注入:通过ApplicationContext可以实现依赖注入(DI),即自动将依赖关系注入到相应的Bean中,避免硬编码依赖关系,提高代码的灵活性和可维护性。
AOP支持:ApplicationContext提供了对面向切面编程(AOP)的支持,可以通过配置切面和通知来实现横切关注点的模块化和重用。
事件传播:ApplicationContext支持事件传播机制,可以在容器中发布和监听事件,实现模块之间的解耦和通信。
国际化支持:ApplicationContext提供了对国际化和本地化的支持,可以方便地处理多语言和不同地区的资源。
资源加载:ApplicationContext可以加载和管理各种类型的资源,包括文件、类路径资源、URL等,方便应用程序进行资源访问和管理。
生命周期管理:ApplicationContext管理Bean的生命周期,包括初始化、销毁等过程,可以通过配置来控制Bean的生命周期行为。
容器扩展:ApplicationContext是一个可扩展的容器,可以通过自定义扩展点来增加和定制容器的功能和行为。
以上便是今天的分享啦,感谢观看!!!