文章目录【辰兮要努力】:hello你好我是辰兮,很高兴你能来阅读,昵称是希望自己能不断精进,向着优秀程序员前行!
博客来源于项目以及编程中遇到的问题总结,偶尔会有读书分享,我会陆续更新Java前端、后台、数据库、项目案例等相关知识点总结,感谢你的阅读和关注,希望我的博客能帮助到更多的人,分享获取新知,大家一起进步!
吾等采石之人,应怀大教堂之心,愿大家奔赴在各自的热爱里…
- 一、基础概念
- 二、案例学习
一、基础概念
本期重点分享ApplicationContext基础概念以及应用场景
Spring容器最基本的接口就是BeanFactory。BeanFactory负责配置、创建、管理Bean;
ApplicationContext由BeanFactory派生而来;BeanFactory的许多功能需要编程实现,而在ApplicationContext中则可以通过配置的方式实现;
ApplicationContext因此也称之为Spring上下文。Spring容器负责管理Bean与Bean之间的依赖关系。
二、案例学习
本篇同步会复习一下Spring作用域相关知识singleton + prototype
直接上案例看看ApplicationContext到底可以帮助我们做什么?
创建一个user对象
@Data
public class User {
private String id;
private String name;
private Integer age;
}
创建一个applicationContext.xml的配置文件
创建一个测试类帮助我们学习ApplicationContext
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;
public class ApplicationContextDemo {
//打印日志
private static final Logger logger = LoggerFactory.getLogger(ApplicationContextDemo.class);
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) applicationContext.getBean("UserSingleton");
User user1 = (User) applicationContext.getBean("UserSingleton");
User user2 = applicationContext.getBean("UserSingleton",User.class);
logger.info("user.hashCode()是:{}",user.hashCode());
logger.info("user1.hashCode()是:{}",user1.hashCode());
logger.info("user是:{}",user);
logger.info("user1是:{}",user1);
logger.info("user == user1 :{}",user == user1);
User user3 = (User) applicationContext.getBean("UserPrototype");
User user4 = (User) applicationContext.getBean("UserPrototype");
logger.info("user3.hashCode()是:{}",user3.hashCode());
logger.info("user4.hashCode()是:{}",user4.hashCode());
logger.info("user3是:{}",user3);
logger.info("user4是:{}",user4);
logger.info("user3 == user4 :{}",user3 == user4);
}
}
执行代码:控制台日志
22:00:32.921 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user.hashCode()是:1446002 22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user1.hashCode()是:1446002 22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user是:User(id=1, name=辰兮, age=22) 22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user1是:User(id=1, name=辰兮, age=22) 22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user == user1 :true 22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user3.hashCode()是:266875004 22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user4.hashCode()是:266875004 22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user3是:User(id=2, name=辰兮要努力, age=23) 22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user4是:User(id=2, name=辰兮要努力, age=23) 22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user3 == user4 :false
代码逻辑
创建Spring的工厂类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
通过工厂解析XML获取Bean实例
User user = (User) applicationContext.getBean("UserSingleton");
拓展补充
1、对象相等则hashCode一定相等;
2、hashCode相等对象未必相等
如上是我为什么会将两个对象用 == 来比较,刚好可以复习一下spring作用域相关知识点
ApplicationContext总结
如果说BeanFactory是Spring的心脏,那么ApplicationContext就是完整的身躯了。
ApplicationContext由BeanFactory派生而来,提供了更多面向实际应用的功能。
Context我们通常解释为上下文环境,用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”
非常感谢你阅读到这里,如果这篇文章对你有帮助,希望能留下你的点赞 关注❤️ 分享 留言thanks!!!
2021年11月10日22:16:39 愿你们奔赴在自己的热爱里!



