Giter Site home page Giter Site logo

xushuang-spring's Introduction

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); Person p = applicationContext.getBean("p1", Person.class);

#applicationContext.getBean("p1", Person.class); #调用getBean()方法 #org.springframework.context.support.AbstractApplicationContext @Override public T getBean(String name, Class requiredType) throws BeansException { assertBeanFactoryActive(); return getBeanFactory().getBean(name, requiredType); }

		org.springframework.beans.factory.support.AbstractBeanFactory
		public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory 
		
			@Override
			public Object getBean(String name, Object... args) throws BeansException {
				return doGetBean(name, null, args, false);
			}
			
			doGetBean()方法
				#Object sharedInstance = getSingleton(beanName);//单例模式
					#org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
					#protected Object getSingleton(String beanName, boolean allowEarlyReference)
					
					public Object getSingleton(String beanName) {
						return getSingleton(beanName, true);
					}
					
					#--》
					protected Object getSingleton(String beanName, boolean allowEarlyReference) {
						#private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
						Object singletonObject = this.singletonObjects.get(beanName);
						if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
							synchronized (this.singletonObjects) {
								singletonObject = this.earlySingletonObjects.get(beanName);
								if (singletonObject == null && allowEarlyReference) {
									ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
									if (singletonFactory != null) {
										singletonObject = singletonFactory.getObject();
										this.earlySingletonObjects.put(beanName, singletonObject);
										this.singletonFactories.remove(beanName);
									}
								}
							}
						}
						return singletonObject;
					}
				
				
				--代码
				if (sharedInstance != null && args == null) {
					if (logger.isTraceEnabled()) {
						if (isSingletonCurrentlyInCreation(beanName)) {
							logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
									"' that is not fully initialized yet - a consequence of a circular reference");
						}
						else {
							logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
						}
					}
					#这一行ctrl可以进入xml配置文件里
					bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
				}
				
				protected Object getObjectForBeanInstance(Object beanInstance, String name, String beanName,
						 @Nullable RootBeanDefinition mbd) {
		
				// Don't let calling code try to dereference the factory if the bean isn't a factory.
				if (BeanFactoryUtils.isFactoryDereference(name)) {
					if (beanInstance instanceof NullBean) {
						return beanInstance;
					}
					if (!(beanInstance instanceof FactoryBean)) {
						throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass());
					}
					if (mbd != null) {
						mbd.isFactoryBean = true;
					}
					return beanInstance;
				}
			
			
			protected Object getObjectForBeanInstance(--》if (BeanFactoryUtils.isFactoryDereference(name)) {

xushuang-spring's People

Watchers

xushuanglu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.