论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
朋友圈
看朋友圈动态,了解ToB世界。
ToB门户
了解全球最新的ToB事件
博客
Blog
排行榜
Ranklist
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
导读
Guide
相册
Album
记录
Doing
搜索
本版
文章
帖子
ToB圈子
用户
免费入驻
产品入驻
解决方案入驻
公司入驻
案例入驻
登录
·
注册
只需一步,快速开始
账号登录
立即注册
找回密码
用户名
Email
自动登录
找回密码
密码
登录
立即注册
首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
圈子
SAAS
ToB企服应用市场:ToB评测及商务社交产业平台
»
论坛
›
软件与程序人生
›
后端开发
›
Java
›
办理非Spring Bean访问Spring Bean的题目:实用指南 ...
办理非Spring Bean访问Spring Bean的题目:实用指南
曹旭辉
金牌会员
|
2024-7-31 08:22:31
|
显示全部楼层
|
阅读模式
楼主
主题
835
|
帖子
835
|
积分
2505
在非SpringBean类里获取SpringBean,会是什么情况?
case1
下面这段代码中,PlainClass 表示一个平凡Java类:
public class PlainClass {
public void foo1() {
TheOtherBean bean = SpringContextUtils.getBean(TheOtherBean.class);
System.out.println(bean);
}
}
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtils implements ApplicationContextAware {
/**
* Spring容器上下文对象实例
*/
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
}
复制代码
我们在SpringBean里实行 PlainClass#foo1 是没题目的。
而在非SpringBean中实行 PlainClass#foo1,会抛空指针异常,这是因为在这种情况下,SpringContextUtils未被Spring容器管理,无法获取到applicationContext对象。
java.lang.NullPointerException
at jstudy.redislimit.SpringContextUtils.getBean(SpringContextUtils.java:30)
at jstudy.beantest.PlainClass.foo1(PlainClass.java:12)
at jstudy.beantest.BeanTest.test(BeanTest.java:18)
复制代码
case2
接着看下面代码中的 PlainClass :
import org.springframework.beans.factory.annotation.Autowired;
public class PlainClass {
@Autowired
private TheOtherBean theOtherBean;
public void foo2() {
System.out.println(theOtherBean);
}
}
import org.springframework.stereotype.Component;
@Component
public class TheOtherBean {
}
复制代码
此时,在SpringBean 里实行 PlainClass#foo2 , 输出效果是什么?
是null。也就是说 theOtherBean 对象是null。为什么? 因为Spring启动时,程序先加载的是 PlainClass 类(它不受Spring管理),而此时`TheOtherBean`尚未被注入容器里,自动装配失败,注入的`TheOtherBean`为null。
当然, 我们通常在项目中不会写这种烂代码来“博人眼球”。
仔细的同学可以发现,IDE会在 PlainClass 这个
非SpringBean
里使用的@Autowired注解上给出警告,提示"Autowired members must be defined in valid Spring bean (@Component|@Service|...)",提醒开发者符合地界说Bean以避免这类题目。
别的,在非SpringBean 里实行 PlainClass#foo2呢?依然是null。
附上我所使用的测试类代码
package jstudy.beantest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanTest {
@Test
public void test() {
new PlainClass().foo2();
}
}
复制代码
该测试类`BeanTest`在SpringBoot情况下运行。解释掉 @SpringBootTest 和 @RunWith(SpringRunner.class)后,`BeanTest`就是一个
非SpringBean(平凡Java类)
了。
总结
在非SpringBean类中直接获取SpringBean可能会引发题目,例如上面案例里提到的空指针和自动装配失败。为避免这些题目,建议将必要访问Spring Bean的类也注册为Spring Bean,以确保依赖关系得到正确管理。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
本帖子中包含更多资源
您需要
登录
才可以下载或查看,没有账号?
立即注册
x
回复
使用道具
举报
0 个回复
倒序浏览
返回列表
快速回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
or
立即注册
本版积分规则
发表回复
回帖并转播
回帖后跳转到最后一页
发新帖
回复
曹旭辉
金牌会员
这个人很懒什么都没写!
楼主热帖
网络安全应急响应 - 03 - 日志分析与内 ...
Mysql 的Innodb引擎和Myisam数据结构和 ...
【docker系列】docker API管理接口增加 ...
Nmap抓包分析与绕过Windows防火墙 ...
一招教你如何高效批量导入与更新数据 ...
微服务架构演进
聊聊Spring事务控制策略以及@Transacti ...
Redis - 介绍与使用场景
Maven配置私有仓库
C#中的CSV文件读写
标签云
存储
挺好的
服务器
快速回复
返回顶部
返回列表