美文网首页
spring初识

spring初识

作者: HC_小后端 | 来源:发表于2018-12-26 22:49 被阅读0次

spring初识


在阅读一些框架源码时发现很多关于spring很有意思的使用方式,希望能系统的了解下spring的功能,我大体想了解以下几点:

  • bean的生命周期管理和bean容器
  • aop切面

bean容器

  • beanFactory,容器的简单实现,其他容器的基础;
  • 上下文容器,如ApplicationContext。

1. 上下文容器

  • AnnotationConfigApplicationContext java配置类
  • AnnotationConfigWebApplicationContext java配置类加载Springweb
  • ClassPathXmlApplicationContext 类路径下XML配置
  • FileSystemApplicationContext 文件系统XMlpei
  • XmlWebApplicationContext 从web获取XMl配置

2. bean生命周期

bean生命周期是由beanFactory要求实现的初始化工厂需要的方法及顺序。

BeanFactory注释

Bean factory实现应该尽可能支持标准bean生命周期接口,完整的初始化方法及其标准顺序是:

    1. BeanNameAware.setBeanName
    1. BeanClassLoaderAware.setBeanClassLoader
    1. BeanFactoryAware.setBeanFactory
    1. EnvironmentAware.setEnvironment
    1. EmbeddedValueResolverAware.setEmbeddedValueResolver
    1. ResourceLoaderAware.setResourceLoader
      only applicable when running in an application context)
    1. ApplicationEventPublisherAware.setApplicationEventPublisher
      only applicable when running in an application context)
    1. MessageSourceAware.setMessageSource
      //only applicable when running in an application context)
    1. ApplicationContextAware.setApplicationContext
      only applicable when running in an application context)
    1. ServletContextAware.setServletContext
      only applicable when running in a web application context)
    1. BeanPostProcessors.postProcessBeforeInitialization
    1. InitializingBean.afterPropertiesSet
    1. a custom init-method definition
    1. BeanPostProcessors.postProcessAfterInitialization

bean关闭时会调用的方法:

    1. DestructionAwareBeanPostProcessors.postProcessBeforeDestruction
    1. DisposableBean.destroy
    1. a custom destroy-method definition

3. 上下文配置方案

  • XML
  • java类
  • 隐式发现机制,自动装配

2.装配Bean


1.自动化装配

标注Bean的注解:

- @Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
- @Named Component 替代方案
- @Service 通常作用在业务层,但是目前该功能与 @Component 相同。
- @Constroller 通常作用在控制层,但是目前该功能与 @Component 相同。
- @Repository 通常作用在DAO层,但是目前该功能与 @Component 相同。

标注Bean扫描路径方式:

- @ComponentScan 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
-  <context:component-scan base-package = "net.xxx"/> 

注入方式:

- @Resource 默认通过按照名称byName自动注入,也可通过type或name属性指定。
- @Autowired 按照类型(byType)装配依赖对象
- @Inject 同@Autowired

2.第三方jar包类配置Bean

    1. 配置类
@Configuration 表明该类是配置类

@Bean 表明该方法返回值需要注册为bean,方法名是生成的bean名称
带有注解的方法多次调用时返回值是单例。
@Bean
private A a(){,例如b,c方法中a()返回值是一样的。
return new  A();
}

@Bean
private B b(){
return new B(a());
}
@Bean
private C c(){
return new C(a());
}
    1. XML配置

相关文章

  • 第二代微服务网关组件 - Spring Cloud Gatewa

    [TOC] 初识Spring Cloud Gateway 简介: Spring Cloud Gateway是Spr...

  • Spring+Spring MVC+MyBatis入门概念

    什么是SSM 什么是Spring Spring核心容器 Spring JDBC 和 Dao. 初识MyBatis【...

  • 【吐血奉献】Spring框架 层层递进轻松入门 (IOC和DI)

    【万字长文】Spring框架 层层递进轻松入门 (IOC和DI) (一) 初识 Spring Spring框架是 ...

  • Spring初识

    Spring初识 Spring(由Rod Johnson创建的一个开源框架)。Spring是一个开放源代码...

  • 初识spring

    一、什么是spring Spring是一个开源框架,Spring是于2003年兴起的一个轻量级的Java开发框架,...

  • Spring初识

    一、Spring基础 核心容器:Spring Core 应用上下文:Spring Context AOP模块:Sp...

  • spring初识

    spring初识 在阅读一些框架源码时发现很多关于spring很有意思的使用方式,希望能系统的了解下spring的...

  • Spring -- 初识

    使用Spring也有很长一段时间了,但还没有深入理解过其设计的思想和实现方式。在此记录学习Spring的一些笔记。...

  • 初识Spring

    Spring写第一个helloworld程序 先new->spring Bean Configuration ->...

  • 初识Spring

    一、概要流程 二、详细大致流程 三、关于接口及一些名词解释

网友评论

      本文标题:spring初识

      本文链接:https://www.haomeiwen.com/subject/ldrklqtx.html