• Java学习笔记(十六)


    Spring


    什么是 Spring 框架?Spring 框架有哪些主要模块?
    Spring 是一种开源轻量级框架,是为了解决企业应用程序开发复杂性而创建的

    ​ Spring 框架本身亦是按照设计模式精心打造,这使得我们可以在开发环境中安心的集成 Spring 框
    架,不必担心 Spring 是如何在后台进行工作的。

    ​ Spring 框架至今已集成了20 多个模块。这些模块主要被分如下图所示的核心容器、数据访问/集
    成,、Web、AOP(面向切面编程)、工具、消息和测试模块。

    Spring容器中Bean默认为单例,通过@Scope方法指定 (bean的作用域

    prototype:多实例,IOC容器启动的时候,并不会去调用方法创建对象,而是每次获取的时候才会调用方法去创建

    singleton:单实例,IOC容器容器启动的时候就会调用方法创建对象放入到IOC容器中,以后每次获取直接从容器中拿同一个bean(大Map.get()拿)

    request:主要针对web应用,递交一次请求,创建一个对象

    session:同一个session创建一个实例
     

    1. //Scope属性指定多例,缺省默认单例
    2. @Scope("prototype")
    3. @Bean
    4. public Person person(){
    5. return new Person("aaa", 20);
    6. }
    • @Lazy 懒加载

    主要针对单实例bean,单实例bean默认在容器启动时创建,加上@Lazy注解表示容器启动时不创建对向,仅当第一次获取时才创建初始化 

    1. //懒加载机制
    2. @Lazy
    3. @Bean
    4. public Person person(){
    5. return new Person("aaa", 20);
    6. }
    • useDefaultFilters = true注解
    1. @Configuration
    2. //此Filters注解表示扫包时,默认只包含@Controller的注解的bean,但useDefaultFilters = true时,将其他的注解如@Service也包含进去了,因为@Controller与@Service等相关注解都属于@Component子注解
    3. @ComponentScan(value = "com.shadow", includeFilters = {
    4. @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})},
    5. useDefaultFilters = true
    6. )
    7. public class AppConfig {
    8. @Bean
    9. public Person person(){
    10. return new Person("aaa", 20);
    11. }
    12. }
    13. //Controller也属于Component
    14. @Target({ElementType.TYPE})
    15. @Retention(RetentionPolicy.RUNTIME)
    16. @Documented
    17. @Component
    18. public @interface Controller {
    19. }
    20. //ClassPathBeanDefinitionScanner类 165行
    21. //源码部分,如果useDefaultFilters为true,则调用registerDefaultFilters方法添加Component信息
    22. public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry,
    23. boolean useDefaultFilters,
    24. Environment environment, @Nullable ResourceLoader resourceLoader) {
    25. this.registry = registry;
    26. if (useDefaultFilters) {
    27. registerDefaultFilters();
    28. }
    29. }
    30. protected void registerDefaultFilters() {
    31. this.includeFilters.add(new AnnotationTypeFilter(Component.class));
    32. }
    • 什么是控制反转(IOC)?什么是依赖注入?

    IOC:把bean的创建、初始化、销毁交给 spring 来管理,而不是由开发者控制,实现控制反转。

    依然注入有以下三种实现方式: 1. 构造器注入 2. Setter 方法注入 3. 接口注入

    • @Conditional条件注册bean

    1. //Conditional注解条件注册bean,该注解value值必须为Condition类型
    2. @Conditional(WindCondition.class)
    3. @Bean
    4. public Person person(){
    5. return new Person("aaa", 20);
    6. }
    7. //上述Conditional注解配置的条件类
    8. public class WindCondition implements Condition {
    9. /**
    10. * 测试条件注册bean,如果当前操作系统为windows则注入bean
    11. */
    12. @Override
    13. public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    14. //获取ioc容器的beanFactory
    15. ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    16. //获取当前环境信息
    17. Environment environment = context.getEnvironment();
    18. //environment获取当前操作系统
    19. String property = environment.getProperty("os.name");
    20. if(property.contains("Windows")){
    21. return true; //返回true则注入bean
    22. }
    23. return false;
    24. }
    25. }
    • @Import注册bean
    1. @Configuration
    2. @ComponentScan(value = "com.shadow")
    3. @Controller
    4. //用Import注解注册bean,可放入多个class,指定的class需要提供无参构造方法,bean的id默认为全类名
    5. @Import({Person.class})
    6. public class AppConfig {
    7. }
    8. //还可以通过ImportBeanDefinitionRegistrar自定义注册,向容器中注册bean;
    9. //新建一个类CustomImportBeanDefinitionRegistrar实现该接口,通过Import注解导入
    10. @Import({Person.class, CustomImportBeanDefinitionRegistrar.class})
    11. public class AppConfig {
    12. }
    13. public class CustomImportBeanDefinitionRegistrar
    14. implements ImportBeanDefinitionRegistrar {
    15. @Override
    16. public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    17. //在容器中注册自定义bean
    18. RootBeanDefinition beanDefinition = new RootBeanDefinition(Order.class);
    19. registry.registerBeanDefinition("order", beanDefinition);
    20. }
    21. }

     

  • 相关阅读:
    【HDFS】记一次由JN性能瓶颈导致的NN频繁宕机异常
    记录一次成功反混淆脱壳及抓包激活app全过程
    大疆2022秋招笔试 —— 最小时间差、数组的最小偏移量
    【日记】文章更新计划
    【ElasticSearch系列-05】SpringBoot整合elasticSearch
    机器学习算法系列————决策树(二)
    Linux nohup 命令
    WiFi网络分析工具Airtool for Mac
    One class learning(SVDD)
    [ 云计算 华为云 ] 华为云开天 aPaaS:构建高效的企业数字化平台(下)
  • 原文地址:https://blog.csdn.net/Rick_rui/article/details/126109277