Spring是一个开源框架,是一个基于IOC和AOP来架构多层的JavaEE 架构,默认是单例模式
Spring模块分为以下几个部分
1.Core container(核心容器)
含有Core,Beans,Context,和Expression Language
2.Data Access
3.Web
4.AOP
4.Test
IOC
Inversion of Control
public class Girl {
//boy是在应用内部创建及维护的。 private Boy boy =new Boy(); public void kiss() { System.out.println(boy.getBoyObject()); } }
IOC(控制反转):所谓控制反转就是应用本身不负责依赖对象的创建及维护,依赖对象的创建及维护是由外部容器负责的。这样控制权就由应用转移到了外部容器,控制权的转移就是所谓反转,目的是为了获得更好的扩展性和良好的可维护性。
<bean id="boy" class="cn.itcast.a_ioc.Boy"></bean>
DI
(依赖注入):把依赖的对象交给外部容器负责创建,就是在运行期间,有外部容器动态的将依赖对象注入到组件中。
public class Girl { private Boy boy = null; public Boy getBoy() { return boy; } //由spring容器注入Boy对象 public void setBoy(Boy boy) { this.boy = boy; } public Girl(){ System.out.println("这是Girl的构造方法"); } public void kiss(){ System.out.println("Girl的kiss()方法"); boy.displany(); } }
//把依赖的对象boy 交有外部容器动态创建并注入
<bean id="boy" class="cn.itcast.b_di.Boy"></bean> <bean id="girl" class="cn.itcast.b_di.Girl"> <!-- property:用来指定Girl对象中的属性 * name:表示Girl类中属性的名称 * ref:将外部的Boy对象通过set方法注入给Girl对象 --> <property name="boy" ref="boy"></property> </bean>
当spring容器启动后,因为spring容器可以管理bean对象的创建,销毁等生命周期,所以我们只需从容器直接获取Bean对象就行,而不用编写一句代码来创建bean对象。
<context:annotation-config/>
这个配置隐式注册了多个对注解进行解析处理的处理器
AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,
PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
核心实现类
DefaultListableBeanFactory
DefaultListableBeanFactory是Spring注册及加载Bean的默认实现,主要是对bean注册后的处理
XmlBeanDefinitionReader
InputStreamSource
//通过Resource完成了对配置文件的封装
Resource resource = new ClassPathResource("beanFactoryTest.xml"); public InputStream getInputStream() throws IOException { InputStream is; if (this.clazz != null) { is = this.clazz.getResourceAsStream(this.path); } else if (this.classLoader != null) { is = this.classLoader.getResourceAsStream(this.path); } else { is = ClassLoader.getSystemResourceAsStream(this.path); } //省略部分代码 }
接口 ApplicationContextInitializer
实现接口的initialize 方法初始化 配置 如reids apollo
XmlBeanFactory
XmlBeanFactory public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException { super(parentBeanFactory); this.reader = new XmlBeanDefinitionReader(this); this.reader.loadBeanDefinitions(resource); } XmlBeanDefinitionReader->loadBeanDefinitions->doLoadBeanDefinitions->registerBeanDefinitions
实例化bean
AbstractApplicationContext->getBean->doGetBean
protected <T> T doGetBean(String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly) throws BeansException { // 提取对应的bean名称 String beanName = this.transformedBeanName(name); //检查缓存中或者实例工厂中是否有对应的实例(因为可能有循环依赖的情况,所有在创建bean是 如有有依赖上一个bean的则直接使用ObjectFactory //直接尝试从缓存中或者SingletonFactories的ObjectFactory中获取 Object sharedInstance = this.getSingleton(beanName); Object bean; if (sharedInstance != null && args == null) { if (this.logger.isDebugEnabled()) { if (this.isSingletonCurrentlyInCreation(beanName)) { this.logger.debug("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference"); } else { this.logger.debug("Returning cached instance of singleton bean '" + beanName + "'"); } } //返回对应的实例,返回实例本身或者指定方法返回的实例 bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, (RootBeanDefinition)null); } else { //检查是否有循环依赖,只有单例才会尝试解决循环依赖,其他直接报错 if (this.isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } BeanFactory parentBeanFactory = this.getParentBeanFactory(); //beanDefinitionMap中(所以已经加载的类中如果不包含从parentBeanFactory中检测 if (parentBeanFactory != null && !this.containsBeanDefinition(beanName)) { String nameToLookup = this.originalBeanName(name); if (parentBeanFactory instanceof AbstractBeanFactory) { return ((AbstractBeanFactory)parentBeanFactory).doGetBean(nameToLookup, requiredType, args, typeCheckOnly); } //递归到BeanFactory中寻找 if (args != null) { return parentBeanFactory.getBean(nameToLookup, args); } return parentBeanFactory.getBean(nameToLookup, requiredType); } //记录不是做类型检查,记录要创建的bean if (!typeCheckOnly) { this.markBeanAsCreated(beanName); } try { RootBeanDefinition mbd = this.getMergedLocalBeanDefinition(beanName); this.checkMergedBeanDefinition(mbd, beanName, args); String[] dependsOn = mbd.getDependsOn(); String[] var11; //若存在依赖则需要递归实例化依赖的 bean if (dependsOn != null) { var11 = dependsOn; int var12 = dependsOn.length; for(int var13 = 0; var13 < var12; ++var13) { String dep = var11[var13]; if (this.isDependent(beanName, dep)) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); } //缓存依赖调用 this.registerDependentBean(dep, beanName); try { this.getBean(dep); } catch (NoSuchBeanDefinitionException var24) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "'" + beanName + "' depends on missing bean '" + dep + "'", var24); } } } // 依赖的bean实例完后,开始实例化mbd本身 if (mbd.isSingleton()) { sharedInstance = this.getSingleton(beanName, () -> { try { return this.createBean(beanName, mbd, args); } catch (BeansException var5) { this.destroySingleton(beanName); throw var5; } }); bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } else if (mbd.isPrototype()) { var11 = null; // prototype 模式的创建(new) Object prototypeInstance; try { this.beforePrototypeCreation(beanName); prototypeInstance = this.createBean(beanName, mbd, args); } finally { this.afterPrototypeCreation(beanName); } bean = this.getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } else { // 指定的 scope上实例化bean(添加@Scope注解的) String scopeName = mbd.getScope(); Scope scope = (Scope)this.scopes.get(scopeName); if (scope == null) { throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'"); } try { Object scopedInstance = scope.get(beanName, () -> { this.beforePrototypeCreation(beanName); Object var4; try { var4 = this.createBean(beanName, mbd, args); } finally { this.afterPrototypeCreation(beanName); } return var4; }); bean = this.getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } catch (IllegalStateException var23) { throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton", var23); } } } catch (BeansException var26) { this.cleanupAfterBeanCreationFailure(beanName); throw var26; } } //检查需要的类型是否符合bean的实际类型 if (requiredType != null && !requiredType.isInstance(bean)) { try { T convertedBean = this.getTypeConverter().convertIfNecessary(bean, requiredType); if (convertedBean == null) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } else { return convertedBean; } } catch (TypeMismatchException var25) { if (this.logger.isDebugEnabled()) { this.logger.debug("Failed to convert bean '" + name + "' to required type '" + ClassUtils.getQualifiedName(requiredType) + "'", var25); } throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } else { return bean; } }
注解
Autowired
//@Autowired @Qualifier("userDao1")
private IUserDao userDao;
Autowired标注在字段上
如果Autowired注解标注在字段上,会按照IUserDao的类型找,找不到抛出异常。
Autowired @Qualifier("userDao")标注在字段上
如果@Autowired @Qualifier("userDao")标注在字段上,按照名称userDao找,如果没有找到,此时会抛出异常。
Resource
Resource注解标注在字段上
如果@Resource注解标注在字段上, 此时先按照属性的名称找,找不到在根据属性类型找,都没找到就抛异常。
@Resource(name="userDao")标注在字段上,根据name找,找不到就抛异常。