`
xussen
  • 浏览: 30979 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring factory.getBean(name)过程

阅读更多
Eagerly check singleton cache for manually registered singletons
check if bean definition exists in this factory
Not fond ->check parent factory
Guarantee initialization of beans that the current bean depends on.//保证当前bean所依赖的bean必须初始化完成
getBean(dependsOnBean)
if the current bean is singleton ,create bean instance and register it
else if the merged bean is prototype, create new bean directly without registering
else create bean by another custom scope, out of box the web springframework  supports exactly five scopes.
etc. singleton,prototype,request,session,globalSession,application,and a CustomScopeProcessor Class of SimpleThreadScope
 
about "create bean"
give BeanPostProcessor a chance to return a proxy instead of the target bean instance
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(beanClass, beanName)//比如:抑制目标对象的实例化,返回自己的一个对象。
Create a new instance using a appropriate instantiation strategy:factory method, autowire constructor, constructor, or simple instantiation. return BeanWrapper//作用是属性的访问,属性设置,属性类型转换。比如ResourceEditor可以将<property name="resource" value="/message.properties"/>转化为Resource resource。可以通过CustomEditorConfigurer将自己定制的转换器设置进去
Allow post-process to modify the merged bean definitions // MergedBeanDefinitionPostProcessor  收集bean类型里的Annotation,将其缓存在该processor中,在后面会用它来执行注入
Eagerly cache singletons to be able to resolve circular references // singletonsCurrentlyInCreation.add(A)  A->B   B->A  
populate the bean Instance in the given BeanWrapper with the property values  //填充属性值到bean实例。分别执行:InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation 和 InstantiationAwareBeanPostProcessor.postProcessPropertyValues (比如:将@Autoware,@Required,@Resource属性设置进实例)
initialize the bean instance : invoke aware method(往bean注入beanName, classLoader, beanFactory),BeanPostProcessor.postProcessorBeforeInitialization (最主要的一个例子就是ApplicationContextAwareProcessor,它提供了ResourceLoader,EventPublisher,ApplicationContext等)
invoke afterPropertiesSet and init method
BeanPostProcessor.postProcessorAfterInitialization(BeanNameAutoProxyCreator根据BeanNames判断是否代理)
 
分享到:
评论

相关推荐

    spring中通过ApplicationContext getBean获取注入对象的方法实例

    在Spring框架中,`...&lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;!-- 配置属性文件的位置和别名 --&gt; &lt;!-- 3. 定义JedisPool Bean --&gt; ...

    struts2驱动包

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested ...

    spring AOP

    import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import com.icss.dao.StudentDao; import ...

    几种spring获取bean的方法.txt

    import org.springframework.beans.factory.BeanFactoryAware; public class ServiceLocator implements BeanFactoryAware { private static BeanFactory beanFactory = null; public void setBeanFactory...

    JSF和Spring集成.doc

    - `findBean(String beanName)` 方法用于从 Spring 中查找 bean,该方法通过当前 `FacesContext` 获取 `ServletContext`,进而获取 `ApplicationContext`,最终调用 `getBean` 方法获取指定名称的 bean。...

    flex-spring.pdf

    return applicationContext.getBean("beanName"); } catch (NoSuchBeanDefinitionException e) { throw new ServiceException(e.getMessage(), e); } catch (BeansException e) { throw new ServiceException(e...

    spring-jms使用queue发送消息简单例子

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Component; @Component public class MessageSender ...

    基于Maven的Spring与ActiveMQ整合的简单实例

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Component; @Component public class ...

    JMS与Spring之一(用JmsTemplate同步收发消息)

    JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate"); jmsTemplate.convertAndSend("queue1", "Hello, JMS!"); 使用JMS Template可以简化JMS编程,减少开发者的精力投入。JmsTemplate可以自动...

    获得spring里注册Bean的四种方法

    protected final Object getBean(String beanName) { return ctx.getBean(beanName); } public void setServlet(ActionServlet servlet) { this.servlet = servlet; this.ctx = WebApplicationContextUtils....

    Spring—下载与配置

    本篇将详细介绍Spring框架的下载、安装以及基本配置过程。 首先,我们来了解如何下载Spring框架。Spring框架的官方下载地址是https://spring.io/projects/spring-framework。在该页面,你可以找到最新版本的Spring...

    Spring2.0精简笔记

    - **Spring框架**是一种轻量级Java开发框架,主要用来简化企业级应用的开发过程。它通过一系列的设计模式(如依赖注入DI、面向切面编程AOP等)实现了对业务逻辑与底层基础设施的解耦。 2. **Spring MVC框架:** ...

    java中获得spring中的BEAN

    本文将深入探讨如何在Java中获取Spring容器中的Bean,并通过具体的示例来讲解这一过程。 ### Spring框架概述 Spring框架是由Rod Johnson创建的一个开源项目,旨在简化Java企业级应用程序的开发。Spring的核心特性...

    知识共享-Spring注入属性值案例(雷惊风).

    &lt;bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"&gt; ``` 在这个XML配置文件中,首先定义了一个`Person` Bean(id为`person`),设置了其`age`属性为30...

    SpringBoot 获取spring bean方式.rar

    MyService myService = factory.getBean(MyService.class); ``` 5. **`@Qualifier`注解** 当有多个相同类型的Bean时,`@Qualifier`可以用来指定具体的Bean。例如: ```java @Autowired @Qualifier(...

    Spring配置文件spring-context.zip

    Spring框架是Java开发中不可或缺的一部分,它以IoC(Inversion of Control,控制反转)和DI(Dependency Injection,依赖注入)为核心理念,极大地简化了企业级应用的开发工作。Spring框架提供了一个全面的编程和...

    flex整合Spring

    在Flex与Spring的整合过程中,主要涉及以下几个关键步骤: 1. **配置Spring容器**:在Web应用启动时加载Spring容器,并指定Spring配置文件的位置。 2. **定义SpringFactory**:自定义一个SpringFactory类,用于创建...

    客户关系管理系统框架搭建(二)

    ISysUserGroupService sysUserGroupService = (ISysUserGroupService) ctx.getBean(ISysUserGroupService.SERVICE_NAME); SysUserGroup sysUserGroup = new SysUserGroup(); sysUserGroup....

    spring rmi 小例子

    &lt;bean id="rmiRegistry" factory-bean="rmiRegistryFactory" factory-method="getRegistry" /&gt; &lt;bean id="rmiServerRegistry" class="org.springframework.remoting.rmi.RmiRegistryServer"&gt; ...

Global site tag (gtag.js) - Google Analytics