×

简述spring加载过程

简述spring加载过程(spring怎么实现类加载的)

admin admin 发表于2023-11-19 22:59:30 浏览32 评论0

抢沙发发表评论

本文目录

spring怎么实现类加载的

以spring框架注入bean说明接口TestService 有2个实现类 TestServiceImp1 @Service(“TestService1“) ,TestServiceImp2 @Service(“TestService2“)在controller里注入server的bean时使用注释@Qualifier指明使用的实现如@Autowired@Qualifier(“TestService1“)TestService testService则调用具体代码时testService.test();//此时使用的是TestServiceImp1实现类里的test方法,但是用eclipse查看源码时会显示2个实现类要你选择

java web的 怎么加载spring

spring在web环境中,java代码里需要得到ApplicationContext; 根据前期配置的不同,有两种方式:方式一,spring加载放到web.xml中配置: view plain copy print? init() throws ServletException { BeanManager bm = new BeanManager(); } 在java代码中使用 BeanManager.getBean(String beanId); 来获得bean实例。

如何在spring初始化过程中加载数据库中的数据

有两种方法

  1. 在你写的类里面实现InitializingBean接口,在afterPropertiesSet方法里面写初始化方法

  2. 实现ApplicationListener接口,在spring初始化bean完了后初始化数据

springBoot的类加载顺序是怎样的呢

Tomcat的class加载的优先顺序一览 1.最先是$JAVA_HOME/jre/lib/ext/下的jar文件。 2.环境变量CLASSPATH中的jar和class文件。 3.$CATALINA_HOME/common/classes下的class文件。 4.$CATALINA_HOME/commons/endorsed下的jar文件。 5.$CATALINA_HOME/commons/i18n下的jar文件。 6.$CATALINA_HOME/common/lib 下的jar文件。

spring bean什么时候加载

一个applicationContext.xml配置文件,这个不可少一个bean,这里我没用接口,直接用一个普通的类做为Spring的bean一个Junit测试类applicationContext.xml《?xml version=“1.0“ encoding=“UTF-8“?》《!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN 2.0//EN“ “但Spring经常把简单的逻辑写的非常“啰嗦”,仔细分析代码,发现他完成了几个事情:a. 对bean对象的校验b. 检查beanFactory中是否已经有同名的bean,如果有,进行相应处理c. 把bean对象放到beanDefinitionMap中(这就是最终所谓的注册)d. 清除整个过程缓存的对象数据以上便是Spring对bean解析注册的全过程,总结一下大致步骤:1. 加载XML文件,封装成Resource对象2. 调用Reader对象方法读取XML文件内容,并将相关属性放到BeanDefinition实例3. 将BeanDefinition对象放到BeanFactory对象

Spring 标签加载bean的原理是怎样的

Spring管理bean:

1.从web.xml里面配置的ContextLoaderListener开始。

2.ContextLoaderListener继承ContextLoader

3.执行ContextLoaderListener的contextInitialized方法,获得servletContext.

4.把servletContext传入ContextLoader的initWebApplicationContext(ServletContext servletContext)方法并执行。

5.通过ContextLoader.createWebApplicationContext(servletContext)获得this.context(WebApplicationContext)实例。

默认情况下这个context就是XmlWebApplicationContext。

这个XmlWebApplicationContext继承一个AbstractApplicationContext类。

再执行ContextLoader.configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc)方法,

执行AbstractApplicationContext的.refresh();方法

 view plain copy

  • @Override  

  • public void refresh() throws BeansException, IllegalStateException {  

  • synchronized (this.startupShutdownMonitor) {  

  • // Prepare this context for refreshing.  

  • prepareRefresh();  

  • // Tell the subclass to refresh the internal bean factory.  

  • ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();  

  • // Prepare the bean factory for use in this context.  

  • prepareBeanFactory(beanFactory);  

  • try {  

  • // Allows post-processing of the bean factory in context subclasses.  

  • postProcessBeanFactory(beanFactory);  

  • // Invoke factory processors registered as beans in the context.  

  • invokeBeanFactoryPostProcessors(beanFactory);  

  • // Register bean processors that intercept bean creation.  

  • registerBeanPostProcessors(beanFactory);  

  • // Initialize message source for this context.  

  • initMessageSource();  

  • // Initialize event multicaster for this context.  

  • initApplicationEventMulticaster();  

  • // Initialize other special beans in specific context subclasses.  

  • onRefresh();  

  • // Check for listener beans and register them.  

  • registerListeners();  

  • // Instantiate all remaining (non-lazy-init) singletons.  

  • finishBeanFactoryInitialization(beanFactory);  

  • // Last step: publish corresponding event.  

  • finishRefresh();  

  • }  

  • catch (BeansException ex) {  

  • logger.warn(“Exception encountered during context initialization - cancelling refresh attempt“, ex);  

  • // Destroy already created singletons to avoid dangling resources.  

  • destroyBeans();  

  • // Reset ’active’ flag.  

  • cancelRefresh(ex);  

  • // Propagate exception to caller.  

  • throw ex;  

  • }  

  • }  

  • 这个方法完成了WebApplicationContext里面的beanfactory的初始化和bean载入,beanfactorypostprocessor的调用,beanpostprocessor的注册,ApplicationEvent的监听和注册,non-lazy-init的bean的初始化。
  • 换言之,已经把该准备的都准备好了,只需要有请求来获取bean,就根据情况或返回已经初始化的bean或进行bean的Instantiation 和 Initialization。

Spring载入多个配置文件加载顺序是怎么样的

这个顺序不需要关心吧?Spring是先加载配置文件,然后更加配置文件再初始化相应的类比如你在B配置文件中声明了一个BeanA 在A配置文件中用到了这个BeanA,Spring 不会由于配置文件加载顺序的问题而造成找不到BeanA的错误的。

SpringBoot的配置加载顺序是什么

1. 开启 DevTools 时, ~/.spring-boot-devtools.properties2. 测试类上的 @TestPropertySource 注解3. @SpringBootTest#properties 属性4. **==命令?参数(--server.port=9000 )==**5. SPRING_APPLICATION_JSON 中的属性 6. ServletConfig 初始化参数7. ServletContext 初始化参数8. java:comp/env 中的 JNDI 属性9. System.getProperties()10. **操作系统环境变量**11. random.* 涉及到的 RandomValuePropertySource12. **jar 包外部的 application-{profile}.properties 或 .yml**13. **jar 包内部的 application-{profile}.properties 或 .yml**14. **jar 包外部的 application.properties 或 .yml**15. **jar 包内部的 application.properties 或 .yml** 16. **@Configuration 类上的 @PropertySource**17. SpringApplication.setDefaultProperties() 设置的默认属性看到这么多,是不是懵逼了,感觉完全记不住,就去黑马程序员官网视频库看免费视频。