file-type

实验探索:Spring Bean的加载顺序控制

下载需积分: 50 | 5KB | 更新于2025-02-11 | 186 浏览量 | 7 下载量 举报 1 收藏
download 立即下载
标题中的“Spring Bean 加载顺序”指的是在Spring框架中,通过依赖注入管理的Bean对象在容器启动时被加载到内存中的顺序问题。Spring框架提供了强大的依赖注入(DI)功能,允许开发者通过配置文件、注解或者Java配置类来声明Bean对象,并通过容器的管理机制来初始化和注入依赖。 描述中提到的“Spring Bean 加载顺序 实验小例子”可能指的是一种实践操作,通过编写一个简单的示例程序,来展示和测试不同的Bean在Spring容器中的加载顺序,尤其是当Bean之间存在依赖关系时。这样的实验可以帮助开发者更好地理解和掌握Spring框架的工作原理,以及如何控制Bean的生命周期。 从给出的信息来看,我们没有具体的代码和详细的配置信息,但是可以基于Spring框架的一些关键知识点来详细说明Spring Bean的加载顺序涉及的知识点。 知识点1: Spring Bean的生命周期 Spring Bean的生命周期涵盖了从创建到销毁的整个过程,包括实例化Bean、设置对象属性、调用Bean的初始化方法以及销毁Bean前的清理工作。Spring容器对Bean的生命周期进行管理,开发者可以通过配置Bean的作用域(scope)、初始化方法(init-method)和销毁方法(destroy-method)等来影响Bean的生命周期。 知识点2: Spring Bean的作用域 Spring容器为Bean定义了多种作用域,包括单例(singleton)、原型(prototype)、会话(session)、请求(request)等。在单例作用域下,无论需要多少次引用Bean,容器始终返回Bean的同一个实例。而在原型作用域下,每次请求该Bean时,容器都会创建一个新的实例。了解不同作用域下Bean的行为对于掌握加载顺序至关重要。 知识点3: Bean之间的依赖关系 在Spring配置中,Bean可以有依赖关系,即一个Bean的创建可能依赖于另一个Bean的存在。Spring容器会根据这些依赖关系来确定加载Bean的顺序。当容器启动时,它首先创建无依赖关系的Bean,然后是依赖于之前创建的Bean的那些Bean,直到所有的Bean都初始化完成。 知识点4: 使用@DependsOn注解控制加载顺序 在某些情况下,Spring的自动依赖分析不能满足特定的加载顺序需求时,可以使用@DependsOn注解来明确指定某个Bean的加载顺序。这个注解可以应用在Bean声明上,用来指定当前Bean依赖于哪些其他Bean。 知识点5: BeanFactoryPostProcessor和BeanPostProcessor 在Spring中,BeanFactoryPostProcessor允许我们在Bean定义加载之后,Bean实例化之前修改Bean的定义属性。BeanPostProcessor则允许我们对Bean的实例进行自定义操作,比如在Bean初始化前后执行自定义的逻辑。这两种接口的实现可以在Bean的加载顺序和生命周期中进行干预和控制。 知识点6: XML配置中的`depends-on`属性 在使用XML配置文件定义Bean时,可以使用`depends-on`属性来控制Bean的加载顺序。通过指定`depends-on`属性,可以明确告诉Spring容器一个Bean的创建依赖于哪些其他的Bean。 知识点7: Java配置中的@Order注解或Ordered接口 在使用Java配置类时,可以通过实现Ordered接口或者使用@Order注解来为配置类或Bean指定加载顺序。这在多个配置类或多个Bean有特定的加载顺序要求时非常有用。 通过这些知识点,开发者可以更好地理解和控制Spring Bean的加载顺序,以及如何根据应用的需要对Spring容器进行适当的配置和调整。在实际开发中,通常会根据具体的应用场景和需求来决定是否需要精确控制Bean的加载顺序,以及如何有效地利用Spring提供的各种机制来实现这一目标。

相关推荐

filetype

设计模式14:45:58.381 [restartedMain] WARN o.s.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'FIXED_CONFIG.SysIsOpenSensitive' defined in file [E:\project\dangan_system\office-common\target\classes\com\nfrc\modules\core\fixed\config\SysIsOpenSensitive.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError 14:45:58.381 [restartedMain] INFO com.alibaba.druid.pool.DruidDataSource - {dataSource-0} closing ... 14:45:58.412 [restartedMain] ERROR org.springframework.boot.SpringApplication - Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'FIXED_CONFIG.SysIsOpenSensitive' defined in file [E:\project\dangan_system\office-common\target\classes\com\nfrc\modules\core\fixed\config\SysIsOpenSensitive.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1334) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1232) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java

filetype

2025.03.17 10:09:33|INFO |org.springframework.boot.SpringApplication|logStartupProfileInfo()|655|The following profiles are active: dev 2025.03.17 10:09:35|INFO |org.springframework.data.repository.config.RepositoryConfigurationDelegate|multipleStoresDetected()|249|Multiple Spring Data modules found, entering strict repository configuration mode! 2025.03.17 10:09:35|INFO |org.springframework.data.repository.config.RepositoryConfigurationDelegate|registerRepositoriesIn()|127|Bootstrapping Spring Data Redis repositories in DEFAULT mode. 2025.03.17 10:09:35|INFO |org.springframework.data.repository.config.RepositoryConfigurationDelegate|registerRepositoriesIn()|187|Finished Spring Data repository scanning in 148ms. Found 0 Redis repository interfaces. 2025.03.17 10:09:35|INFO |org.springframework.cloud.context.scope.GenericScope|setSerializationId()|295|BeanFactory id=583b7bc4-7db1-3dd1-be46-5c61142142a7 2025.03.17 10:09:35|INFO |org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker|postProcessAfterInitialization()|330|Bean 'com.arpa.wms.api.EnzomesaServerAPI' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025.03.17 10:09:35|INFO |org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker|postProcessAfterInitialization()|330|Bean 'threadConfig' of type [com.arpa.wms.config.ThreadConfig$$EnhancerBySpringCGLIB$$bcdc659] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025.03.17 10:09:35|INFO |org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker|postProcessAfterInitialization()|330|Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025.03.17 10:09:36|INFO |org.springframework.boot.web.embedded.tomcat.TomcatWebServer|initialize()|92|Tomcat initialized with port(s): 8080 (http) 2025.03.17 10:09:36|INFO |org.apache.juli.logging.DirectJDKLog|log()|173|Initializing ProtocolHandler ["http-nio-8080"] 2025.03.17 10:09:36|INFO |org.apache.juli.logging.DirectJDKLog|log()|173|Starting service [Tomcat]

filetype

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logExportAspect': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ccdAdminApi': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplateDTO' defined in class path resource [com/iflytek/cdc/edr/configuration/RestTemplateConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.iflytek.cdc.edr.utils.RestTemplateDTO]: Factory method 'restTemplateDTO' threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "com.iflytek.cdc.edr.configuration.RestTemplateCommonConfig$OutInvoke.toString()" because the return value of "com.iflytek.cdc.edr.configuration.RestTemplateCommonConfig.getOutInvoke()" is null at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:325) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1404) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) 解析