Spring Core Technologies 官方文档学习

Spring Core Technologies 官方文档 :
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#spring-core

1. IoC Container

1.1. Introduction to Spring IoC Container and Beans

  1. IoC 又叫 DI (Denpendency Injection)。实例化一个对象所需要依赖比如其他对象,属性,包括实例化的过程都由 Spring 容器负责提供,控制,而不在有这个对象本身去控制。

1.2 Container Overview

org.springframework.context.ApplicationContext 这个接口代表了 Spring 容器,同时负责实例,配置,组装 要实例化的对象。

1.2.1. Configuration Metadata
  1. 三种类型的配置元数据:xml 形式的,annnotation 形式, java 代码形式的。
1.2.2 Instantiating a Container
  1.     ApplicationContext context = new ClassPathXmlApplicationContext("dubbo/consumer.xml");
    

1.3 Bean Overview

1.3.2 Instantiating Beans
  1. 三种实例化方式

  2. 构造函数的方式实例化。

  3. 静态工厂方法实例化。
    public class ClientService {
    private static ClientService clientService = new ClientService();
    private ClientService() {}

    public static ClientService createInstance() {
    return clientService;
    }
    }

  4. 通过实例工厂方法
    public class DefaultServiceLocator {

    private static ClientService clientService = new ClientServiceImpl();
    
    public ClientService createClientServiceInstance() {
        return clientService;
    }}
    
<bean id="clientService"
factory-bean="serviceLocator"
factory-method="createClientServiceInstance"/>

<bean id="accountService"
factory-bean="serviceLocator"
factory-method="createAccountServiceInstance"/>

1.4 Dependency

1.4.1 Dependency Injection
1.4.1 Dependency Resolution Process
  1. Spring 容器 ApplicationContext 创建和初始化的时,使用配置元数据(Configuration MetaData)。
  2. 所谓的依赖就是就是Bean 的属性,构造所需要的参数。这些依赖会在Bean 被创建的时候由Spring 容器提供。
  3. 一个Bean 的创建 BeanScope 决定的,可能在容器创建的时候创建,也可能在这个Bean 被请求使用的时候创建。
1.4.2 Dependencies and Configuration in Detail
  1. 如何定义 InnerBean, 如何定义集合,集合的合并,Null 和 空的表示。
1.4.3 使用 Depends-on

使用Dependon-on 强制一个或多个依赖在使用前初始化。

1.4.4 懒加载

默认不会懒加载,default-lazy-init = “true”。

1.5 Bean Scope

  1. 单例 Singletion: 一个Spring 容器只有一个Bean。
  2. Prototype : 容器每次使用getBean() 方法时(bean 的初始化的时候)会创建一个新的Bean。Prototype 通常使用在有状态的Bean 的情况下,无状态的Bean 通常使用 Singletion.
  3. custom scope : 自定义scope。

1.6 Customizing the Nature of a Bean

1.6.1 LifeCycleCallbacks
  1. InitializingBean — afterPropertiesSet() — @postConstruct — init-method 容器把Bean 的所有属性设置好了以后调用此方法。
  2. DisposableBean — destory() — PreDestory — destory-method 当对象被销毁时调用这个方法。
  3. LifeCycle接口定义了一些基本的方法用于任意对象的生命周期的管理。接口包括 start() stop() isRunning() 方法。
  4. Phased 接口的 getPhased() 方法定义了对象启动,关闭涉及到的回调函数的调用顺序。默认的每个生命周期方法的timeout是30秒。
1.6.2 ApplicationContextAware and BeanNameAware
  1. 如果对象实现了 ApplicationContextAware 接口, 实例在创建啊是就会获取 ApplicationContext 的引用。一种用法是获取其他的 Bean,一种用法是访问 Resource, 发布时间消息Event
    public interface ApplicationContextAware {public interface ApplicationContextAware {
    public interface ApplicationContextAware {
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
    }
  2. 如果对象实现的 BeanNameAware, 对象会获取一个bean 名称的引用。这个回调方法在 @postContruct 之前被调用。

1.8. Container Extension Points

1.8.1 Customizing Beans by Using a BeanPostProcessor
  1. BeanPostProcessor 包含两个接口,一个在容器初始化方法(比如 InitializingBean.afterPropertiesSet )之前,一个在Bean 的所有初始化回调方法之后。
1.8.2 Customizing Configuration Metadata with a BeanFactoryPostProcessor
  1. BeanFactoryPostProcessor 对 配置文件(configuration metadata)进行操作。在Spring 容器实例化beans 之前,让 BeanFactoryPostProcessor 读取配置文件(configuration metadata),改变配置文件。例如:PropertyPlaceholderConfigurer
1.8.3 Customizing Instantiation Logic with a FactoryBean
  1. Object getObject() : 返回是的这个factory 创建的object。
  2. boolean isSingleton()
  3. Class getObjectType():返回 这个对象的object type。

1.9 Annotation-based Container Configuration

  1. context:annotation-config/
    这个标签只会找在这个标签所在的应用上下文中(application context)。例如:如果你把这个标签放在 WebApplicationContext 中,这个标签只会探测到controller 中的@Autowire bean。
  2. @Required
    属性必须在bean 的配置时(configuration time)就赋值。
  3. @Autowired
    setter, constructor 方法 上使用, 也可以在任意有参数的方法上使用。也可以在属性上使用, Map<String, Object> 也可以自动绑定, key 是 beans 的名称, values 是 beans。
  4. @Primary
    在一个 @Autowire 有多个参与者时,使用 @primary 指定谁是优先使用的。
  5. @Qualifier bean 的名称作为 默认的qualifier 值。
  6. 基于 autowiring with qualifier 自定义annnotation。
  7. @Resource: @Resource 注解有个name 的属性。Spring 容器默认使用这个属性注入对象。没有指定的name 的话,使用属性名或者 setter 方法对应的参数名。
  8. @PostConstruct and @PreDestory

1.10 Classpath Scanning and Managed Components

  1. @Component 是一个元注解,泛型的注解,而 @Repository, @Service, @Controller 是特定含义的注解。@Repository 是会对异常做特定转译的。
  2. 自动检测
    注解 @ComponentScan(basePackages = “org.example”)
    <context:component-scan base-package=“org.example”/>
  3. 使用过滤器去定制化扫描。
  4. 使用自定义的bean name 生成器 @ComponentScan(basePackages = “org.example”, nameGenerator = MyNameGenerator.class)
  5. 生成 beans 的索引。加入依赖 spring-context-indexer。

1.12 Java-based Container Configuration

  1. 通过AnnotationConfigApplicationContext 实例化spring 容器。 ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
  2. 通过**@ComponentScan**(basePackages = “com.acme”) 启动bean 自动扫描加载。
  3. 在web 应用中使用javaConfigurationContext。配置代码参考:
    https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-java-instantiating-container-web
  4. 使用 @Bean 注解 定义一个 Bean。
  5. 使用 @Import Annotation 加载多个java-config 类:配置代码参考:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-java-configuration-annotation
  6. 混合使用 java-config 和 xml 配置
    配置代码参考:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-java-combining

1.13. Environment Abstraction

3. Validation, Data Binding, and Type Conversion

3.1. Validation by Using Spring’s Validator Interface

(1)实现 org.springframework.validation.Validator 接口 实现校验细节。如果校验失败,会把失败信息传给 Error Object。
(2) 可以在一个 Validator 中嵌入另外一个Validator。

3.2 Resolving Codes to Error Messages

3.3 Bean Manipulation and the BeanWrapper

3.4 Spring Type Conversion

(1)
public interface Converter<S, T> {
T convert(S source);
}

3.5 Spring Field Formatting

(1)根据不同的格式要求,字段内容显示不同。
实现 AnnotationFormatterFactory<?>
@NumberFormat(style=Style.CURRENCY)
private BigDecimal decimal;
(2)spring 内部的格式解析器:
DateFormatter
NumberFormatter etc.

3.7. Spring Validation

(1) 使用 JSR-303 注解。@NotNull @Size
(2)配置一个 Bean Validation Provider
(3)Spring MVC 3 Validation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值