一個Singleton的Bean中Autowired了一個prototype的Bean,每次調用SingletonBean.doSomething()時打印的對象是不是同一個呢?
~~~java
@Service
public class SingletonBean{
@Autowired
private PrototypeBean prototypeBean;
public void doSomething(){
System.out.println(prototypeBean.toString());
}
}
@Component
@Scope(value="prototype")
public class PrototypeBean{
}
~~~
因為Singleton是單例的,所以在項目啟動時就會初始化,prototypeBean本質上只是它的一個Property,那么ApplicationContex中只存在一個SingletonBean和一個初始化SingletonBean時創建的一個prototype類型的PrototypeBean。
那么每次調用SingletonBean.doSomething()時,Spring會從ApplicationContex中獲取SingletonBean,每次獲取的SingletonBean是同一個,所以即便PrototypeBean是prototype的,但PrototypeBean仍然是同一個。每次打印出來的內存地址肯定是同一個
- 概述
- Spring的使命
- 環境變量
- spring架構
- Spring各版本特性
- Spring3.1新特性
- spring頂級項目
- spring基礎
- spring環境變量
- 依賴注入
- spring獲取bean方法
- BeanFactory vs FactoryBean
- JavaBean裝配
- XML顯式配置
- 基于JAVA配置
- 自動化裝配bean
- SpringBean的作用域
- Spring應用上下文實現
- springbean的生命周期
- 自定義Bean的創建與銷毀
- Spring容器啟動過程
- spring加載xsd文件的流程
- spring擴展接口
- Spring主要類功能說明
- spring事務管理
- 事務特性
- 數據庫事務隔離級別
- 事務隔離性問題
- spring事務隔離級別
- 事務傳播行為
- @Transactional
- 循環依賴
- 構造器注入
- 循環依賴原理
- spring循環依賴原理
- spring三級緩存
- Spring注解
- @Component
- @ComponentScan
- @Autowired
- @Import
- @ImportResource
- @Profile
- @Conditional
- @Qualifier
- @Scope
- @PropertySource
- @Value
- @EnableScheduling
- SpEL-Spring表達式
- Spring-AOP
- SpringAOP五種通知類型
- AOP術語
- SpringMVC
- MVC原理圖
- SpringMVC工作原理
- springboot
- @SpringCloudApplication
- springboot tomcat配置
- Spring Boot Starter POMs
- Spring Boot technical starters
- spring boot事件類型
- Springboot日志
- SpringCloud
- springcloud微服務解決方案
- 服務組件
- 注冊中心
- Eureka
- Spring Cloud Zookeeper
- nacos
- Hystrix熔斷原理
- Hystrix應用
- Spring Cloud Config
- 服務網關
- Zuul
- Spring Cloud Gateway
- 服務調用及負載
- Ribbon
- Feign&OpenFeign
- Turbine
- actuator
- springboot & springcloud
- springcloud vs dubbo
- 常見面試題
- BeanFactory和FactoryBean
- @Autowired/@Resource和@Inject的區別
- Singleton bean注入prototype bean
- 附錄