Spring 一览众山小

一览众山小

简单的理解Spring就是一个大的容器,它管理诸多不同职责的对象,我们把这些对象统称为Bean。说到Spring 有两个重要的概念不得不提,一个是依赖注入(DI),另一个是面向界面编程(AOP)。依赖注入降低我们代码的耦合度,使得对象间或模块间松散耦合,让我们的整个软件结构变的更加灵活、可变。这里我要多提一点,就是在设计Bean的时候一般是面向接口的,这一点对于模块外露的功能类来说至关重要。

名词解释

控制反转(IOC, Inversion of Control),是面向对象编程的一种设计原则,可以降低代码之间的耦合度。在Java中常见的有两种方式,一是依赖注入,另一个是依赖查找。

依赖注入(DI,Dependency Injection ),将依赖通过外部以参数的形式注入。

依赖查找(DL,Dependency Lookup) ,通过配置或其他方式指明接口的实现类。如Java 中的SPI。

面向切面编程(AOP,Aspect Oriented Programming),主要针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某一步骤或阶段,从而降低各部分的耦合性。我们可以通过Spring 对业务代码实现日志记录、安全控制、事务处理等。

父子容器

我们可以给一个容器设置父容器,父容器对于子容器是可见的,这里说的可见的是指子容器可以获取到父容器中的Bean,需要注意的是某些特殊职责的Bean只对本容器起作用,例如实现了BeanPostProcesser、ApplicationListener等接口的Bean,虽然可以在子容器中获取的到但它们的职责只对所在容器起作用。

容器的启动过程

  1. 装载Bean的配置,容器读取Bean的配置并生成一个相应的Bean的定义类。
  2. 实例化Bean

Bean的定义:我们在定义一个Bean时一般做三件事情,1、指定Bean的类 2、指定Bean的依赖 3、指定Bean的职责,我们可以通过接口或者是注解指定这个Bean是用来干什么的,例如我们可以为Bean指定实现BeanPostProcesser接口,那么在Bean创建的过程中会被调用。Spring支持三种方式让我们提供Bean的定义,1)配置文件 2)Java Config 3)在Spring 周期中动态设置ean的定义。

1、配置文件

1    <beans:bean name="loadEntityDataService" class="com.sprite.framework.base.entitydata.LoadEntityDataService">
2         <beans:property name="delegator" ref="delegator"/>
3     </beans:bean>

 

2、Java Config

 1 @Configuration
 2 @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
 3 public class EntityConfig {
 4     private static final String DIALECT = "hibernate.dialect";
 5     private static final String SHOW_SQL = "hibernate.show_sql";
 6     private static final String HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";
 7 
 8     @Bean  
 9     public DataSource dataSource(Environment env) {  
10         BasicDataSource druidDataSource = new BasicDataSource();  
11         druidDataSource.setPassword(env.getProperty("jdbc.password"));  
12         druidDataSource.setUsername(env.getProperty("jdbc.username"));  
13         druidDataSource.setUrl(env.getProperty("jdbc.url"));  
14         druidDataSource.setDriverClassName(env.getProperty("jdbc.driver"));  
15         druidDataSource.setMaxActive(Integer.parseInt(env.getProperty("jdbc.maxActive"))); 
16         druidDataSource.setMaxIdle(Integer.parseInt(env.getProperty("jdbc.maxIdle")));
17         druidDataSource.setMaxWait(Integer.parseInt(env.getProperty("jdbc.maxWait")));
18         return druidDataSource;  
19     } 
20 }

 

3、动态设置

 1 public class AnnotationConfigBeanDefinitionParser implements BeanDefinitionParser {
 2 
 3     @Override
 4     public BeanDefinition parse(Element element, ParserContext parserContext) {
 5         Object source = parserContext.extractSource(element);
 6 
 7         // Obtain bean definitions for all relevant BeanPostProcessors.
 8         Set<BeanDefinitionHolder> processorDefinitions =
 9                 AnnotationConfigUtils.registerAnnotationConfigProcessors(parserContext.getRegistry(), source);
10 
11         // Register component for the surrounding <context:annotation-config> element.
12         CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
13         parserContext.pushContainingComponent(compDefinition);
14 
15         // Nest the concrete beans in the surrounding component.
16         for (BeanDefinitionHolder processorDefinition : processorDefinitions) {
17             parserContext.registerComponent(new BeanComponentDefinition(processorDefinition));
18         }
19 
20         // Finally register the composite component.
21         parserContext.popAndRegisterContainingComponent();
22 
23         return null;
24     }
25 
26 }

 

实例化Bean:容器在实例化Bean时一般分为三部分,1、根据Bean的定义实例化原有对象2、设置对象属性、3、装饰对象(生成代理、切面编程等)。在这个过程中Spring为我们提供了一些接口,可以通过这些接口改对象属性或者生成特定功能的代理对象。

代理

代理可分为两种:动态代理 、静态代理。静态代理是通过编码或工具生成代理,动态代理在编译或运行时生成代理。

动态代理:Spring 动态代理的实现有两种,它们是CGLIB 和JDK动态代理。CGLIB 是一个可以操作字节码的库。

系列文章 <<Spring的里里外外>>

posted @ 2018-08-23 18:04  包子小铺  阅读(384)  评论(0编辑  收藏  举报