java autoconfig_SpringBoot之AutoConfig自动配置

1. @XxxxAuto

在SpringBoot中有很多以XxxxAutoConfiguration注解,其实他的作用就是,自动配置当前模块要依赖的类

例如:

@EnableAutoConfiguration

就告诉SpringBoot需要加载那些类,spring-boot-1.5.1.RELEASE.jar/META-INF/spring.factories 在该文件中

2. @Enable

@SpringBootApplication其实也是有以下三个注解组成的

@EnableAutoConfiguration 自动依赖当前所有模块的配置类org/springframework/boot/spring-boot-autoconfigure/1.5.2.RELEASE/spring-boot-autoconfigure-1.5.2.RELEASE.jar!/META-INF/spring.factories

@ComponentScan 扫描class

@Configuration 配置

当我们不在启动类添加@EnableAutoConfiguration时候,我们要自定义要依赖的模块,就要使用

@EnableAsync

@EnableScheduling

@EnableWebMVC

@EnableConfigurationProperties

@EnableJpaRepositories

@EnableTransactionManagement

@EnableCaching

其实@EnableAutoConfiguration这个注解,都是从自己的模块中查询spring.factories文件,

所以当应用启动就加载spring-boot-autoconfigure中的Spring.factories

代码中是

1

2

3

4

5

6

7

8

9

10类:AutoConfigurationImportSelector

方法:public String[] selectImports(AnnotationMetadata annotationMetadata)

AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader)

public static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader) {

return loadMetadata(classLoader, "META-INF/spring-autoconfigure-metadata.properties");

}

具体的模块会导入不同的EnableConfigurationPropertiesImportSelector,然后复写selectImports方法,从当前类名中拿到包名+中依赖的信息,然后加载

1

2

3

4

5

6

7

8

9

10

11Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Import(AsyncConfigurationSelector.class)

public @interface EnableAsync {

Class extends Annotation> annotation() default Annotation.class;

boolean proxyTargetClass() default false;

AdviceMode mode() default AdviceMode.PROXY;

int order() default Ordered.LOWEST_PRECEDENCE;

}

生注册Bean或者是添加配置时候我们可以更加细化@ConditionalOnClass : classpath中存在该类时起效

@ConditionalOnMissingClass : classpath中不存在该类时起效

@ConditionalOnBean : DI容器中存在该类型Bean时起效

@ConditionalOnMissingBean : DI容器中不存在该类型Bean时起效

@ConditionalOnSingleCandidate : DI容器中该类型Bean只有一个或@Primary的只有一个时起效

@ConditionalOnExpression : SpEL表达式结果为true时

@ConditionalOnProperty : 参数设置或者值一致时起效

@ConditionalOnResource : 指定的文件存在时起效

@ConditionalOnJndi : 指定的JNDI存在时起效

@ConditionalOnJava : 指定的Java版本存在时起效

@ConditionalOnWebApplication : Web应用环境下起效

@ConditionalOnNotWebApplication : 非Web应用环境下起效

执行顺序@AutoConfigureAfter:在指定的配置类初始化后再加载

@AutoConfigureBefore:在指定的配置类初始化前加载

@AutoConfigureOrder:数越小越先初始化

自定义Conditional约束类1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25public class ConditionalOtoSaasApplication extends SpringBootCondition{

@Override

public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {

Object name = annotatedTypeMetadata.getAnnotationAttributes(ConditionalOnMyProperties.class.getName()).get("name");

conditionContext.getEnvironment();

if (((String) name).equalsIgnoreCase("test")) {

return new ConditionOutcome(true, "get name properties");

}

return new ConditionOutcome(false, "no get name properties");

}

}

@Target({ElementType.TYPE, ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Conditional(ConditionalOtoSaasApplication.class)

public @interface ConditionalOnMyProperties {

String name();

}

@Configuration

@ConditionalOnMyProperties(name = "test")

public class BlmConfig{

private String url;

private String name;

}

如果您觉得文章对你有用,可以赏我一杯咖啡

d367ccebea330889099c3300608b6a70.png

分享

新浪微博

微信

QQ空间

QQ好友

豆瓣

有道云笔记

取消

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,确保已经添加了 `redisson-spring-boot-starter` 依赖。 然后,在 Spring Boot 3.1 中,你可以使用 `@ConfigurationPropertiesScan` 来扫描自定义的配置类。因此,我们可以创建一个 `RedissonProperties` 配置类,并使用 `@ConfigurationProperties` 注解来指定属性前缀,如下所示: ```java @ConfigurationProperties(prefix = "redisson") public class RedissonProperties { private String address; private String password; // 其他属性... // getter 和 setter 方法... } ``` 接下来,我们可以创建一个配置类 `RedissonAutoConfiguration`,并使用 `@ConditionalOnClass` 注解来确保 Redisson 相关的类在类路径中存在。然后,我们可以使用 `@EnableConfigurationProperties` 注解来启用 `RedissonProperties` 配置属性,并使用 `@Bean` 注解来创建 `RedissonClient` 对象,如下所示: ```java @Configuration @ConditionalOnClass(Redisson.class) @EnableConfigurationProperties(RedissonProperties.class) public class RedissonAutoConfiguration { @Bean(destroyMethod = "shutdown") RedissonClient redissonClient(RedissonProperties redissonProperties) { Config config = new Config(); config.useSingleServer() .setAddress(redissonProperties.getAddress()) .setPassword(redissonProperties.getPassword()); // 其他配置... return Redisson.create(config); } } ``` 最后,我们需要在 `application.properties`(或 `application.yml`)文件中配置 Redisson 相关的属性,如下所示: ```properties redisson.address=redis://localhost:6379 redisson.password=password # 其他属性... ``` 现在,我们就可以通过 `@Autowired` 注解来注入 `RedissonClient` 对象,并使用 Redisson 来操作 Redis 了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值