成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

聊一聊Spring框架中的約定優于配置設計

開發 前端
Spring從2.5版本引入注解驅動開發后,組件掃描成為核心特性之一。通過@ComponentScan注解,Spring會自動掃描指定包及其子包下的組件。

一、什么是約定優于配置?

約定優于配置(Convention over Configuration, CoC)是一種軟件設計范式,它主張通過預定義合理的默認約定來減少開發人員需要做出的決策數量。在Spring框架中,這一理念貫穿始終,使得開發者能夠專注于業務邏輯而非繁瑣的配置。

二、Spring中的默認行為

2.1 組件掃描

Spring從2.5版本引入注解驅動開發后,組件掃描成為核心特性之一。通過@ComponentScan注解,Spring會自動掃描指定包及其子包下的組件。

@Configuration
@ComponentScan("com.example.demo")
public class AppConfig {
    // 不需要顯式定義所有bean
}

分析:

  • 默認掃描與配置類相同的包及其子包
  • 自動檢測帶有@Component及其派生注解(@Service, @Repository, @Controller)的類
  • 默認bean名稱生成策略:類名首字母小寫(如UserService變為userService)

2.2 @Autowired自動裝配

Spring的自動裝配(@Autowired)遵循一系列合理的默認規則:

  1. 類型優先:首先按類型匹配,當有多個同類型bean時才按名稱
  2. 構造器注入:當類只有一個構造器時,@Autowired可省略
  3. 名稱派生:當需要按名稱裝配時,參數名/屬性名作為默認限定符

三、SpringBoot中配置理念

Spring Boot將約定優于配置的理念發揮到極致,通過自動配置和啟動器(starter)大大簡化了開發。

3.1 自動配置機制

Spring Boot的@SpringBootApplication實際上組合了三個核心注解:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration // 啟用自動配置
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
    // ...
}
  • Spring Boot啟動時加載META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件
  • 根據classpath存在的類來決定哪些配置生效(通過@Conditional系列注解)
  • 應用合理的默認配置

3.2 屬性綁定的默認約定

# application.properties
app.database.url=jdbc:mysql://localhost:3306/mydb
app.database.username=admin
@ConfigurationProperties("app.database")
public class DatabaseProperties {
    private String url;
    private String username;
    // getters and setters
}
  • 屬性文件中的kebab-case(短橫線分隔)會自動匹配到Java類的camelCase
  • 也支持PascalCase、snake_case等多種格式

四、自定義約定配置

4.1 創建自定義starter

1)定義配置類:

@AutoConfiguration
@ConditionalOnClass(MyService.class)
@EnableConfigurationProperties(MyProperties.class)
public class MyAutoConfiguration {


    @Bean
    @ConditionalOnMissingBean
    public MyService myService(MyProperties properties) {
        return new MyService(properties);
    }
}

2)在META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports中注冊:

com.example.MyAutoConfiguration

4.2 自定義條件注解

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Conditional(OnProductionEnvironmentCondition.class)
public @interface ConditionalOnProduction {
}


public class OnProductionEnvironmentCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String env = context.getEnvironment().getProperty("app.env");
        return "prod".equalsIgnoreCase(env);
    }
}

4.3 自定義掃描規則

@Configuration
@ComponentScan(
    basePackages = "com.example",
    includeFilters = @Filter(type = FilterType.ANNOTATION, classes = CustomAnnotation.class),
    nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class
)
public class CustomScannerConfig {
    // 使用完全限定名作為bean名稱
}

五、源碼分析

5.1 默認bean名稱生成器

AnnotationBeanNameGenerator實現了默認的bean命名策略:

// org.springframework.context.annotation.AnnotationBeanNameGenerator
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
    if (definition instanceof AnnotatedBeanDefinition) {
        String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition);
        if (StringUtils.hasText(beanName)) {
            return beanName;
        }
    }
    // 默認實現:首字母小寫的類名
    return buildDefaultBeanName(definition, registry);
}


protected String buildDefaultBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
    String beanClassName = definition.getBeanClassName();
    String shortClassName = ClassUtils.getShortName(beanClassName);
    return Introspector.decapitalize(shortClassName);
}

5.2 條件注解

Spring Boot的條件注解(@Conditional)是自動配置的核心:

// org.springframework.boot.autoconfigure.condition.SpringBootCondition
public final boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String classOrMethodName = getClassOrMethodName(metadata);
    try {
        ConditionOutcome outcome = getMatchOutcome(context, metadata);
        // 記錄日志...
        return outcome.isMatch();
    }
    catch (NoClassDefFoundError ex) {
        throw ex;
    }
    catch (Throwable ex) {
        throw ex;
    }
}

以@ConditionalOnClass為例,其匹配邏輯在OnClassCondition中實現:

// org.springframework.boot.autoconfigure.condition.OnClassCondition
protected ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    MultiValueMap<String, Object> onClasses = getAllAnnotationAttributes(
            metadata, ConditionalOnClass.class.getName());
    if (onClasses != null) {
        List<String> missing = filter(onClasses.get("value"), context.getClassLoader(), false);
        if (!missing.isEmpty()) {
            return ConditionOutcome.noMatch(ConditionMessage.forCondition(
                    ConditionalOnClass.class).didNotFind("required class", "required classes")
                    .items(Style.QUOTE, missing));
        }
    }
    // 類似處理@ConditionalOnMissingClass
    return ConditionOutcome.match();
}


責任編輯:武曉燕 來源: 全棧程序員老馬
相關推薦

2025-06-05 01:45:00

Spring框架適配器

2024-10-16 15:11:58

消息隊列系統設計

2020-07-16 14:40:23

大數據計算框架

2023-12-28 09:59:37

Spring容器XML

2020-12-11 11:11:44

原子類JavaCAS

2019-12-02 16:23:03

Python編程語言“垃圾”回收

2022-08-30 07:39:57

C++namespace隔離

2021-01-04 08:09:07

Linux內核Watchdog

2024-09-09 08:29:25

2023-07-06 13:56:14

微軟Skype

2022-03-06 20:35:41

并發串行CAP

2021-06-30 07:19:35

微服務業務MySQL

2019-12-12 14:52:10

數據庫腳本

2022-11-09 08:05:15

JavaScriptsuper()

2020-09-08 06:54:29

Java Gradle語言

2021-01-28 22:31:33

分組密碼算法

2023-09-22 17:36:37

2020-05-22 08:16:07

PONGPONXG-PON

2020-06-02 15:06:13

Tomcat配置頁面

2018-07-23 15:28:29

HTTPCookieHeader
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日本久久精品视频 | 国产一区二区三区色淫影院 | 午夜视频在线免费观看 | 久久久成人免费一区二区 | 亚洲一区中文字幕在线观看 | 久久一区视频 | 日韩在线国产 | 亚洲欧美国产精品久久 | 特级做a爱片免费69 精品国产鲁一鲁一区二区张丽 | 久久久久久久久久久丰满 | 国产精品99999999 | 国产激情91久久精品导航 | 精品国产乱码一区二区三区 | 羞羞视频在线网站观看 | 日本三级网址 | 国产三级精品三级在线观看四季网 | 亚洲成人精品一区二区 | 久久精品99国产精品 | 日韩精品一区二区三区中文在线 | 欧美在线国产精品 | 精品久久久久久久久久久久久 | 91中文字幕 | 国产精品永久免费视频 | 91精品入口蜜桃 | 久久三区 | 欧美性生活视频 | 亚州毛片 | 美女露尿口视频 | 国产午夜精品久久久 | 午夜精品久久久久久久久久久久久 | 韩国毛片一区二区三区 | 中文字幕视频在线 | 色综合视频在线 | 精品欧美一区免费观看α√ | 国产永久免费 | 亚洲国产欧美91 | 国产精品毛片一区二区三区 | 九色在线 | 精品视频在线观看 | 精品不卡 | 一区二区三区av |