spring 如何在Sping Boot 中创建一个可以将值传递给配置的“Enable”注解?

rqmkfv5c  于 2024-01-05  发布在  Spring
关注(0)|答案(2)|浏览(140)

我正在使用Sping Boot 3.2,我正在尝试编写一个annotation来导入一些spring配置。这是我所拥有的:

  1. @Configuration
  2. public class CustomAutoConfiguration {
  3. @Bean
  4. public CustomBean customBean() {
  5. return new CustomBean();
  6. }
  7. }

字符串
然后我有这样的注解:

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Import(CustomAutoConfiguration.class)
  4. public @interface EnableCustomAutoConfiguration {
  5. }


然后我可以像这样启用:

  1. @SpringBootApplication
  2. @EnableCustomAutoConfiguration
  3. public class MySpringBootApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(MySpringBootApplication.class, args);
  6. }
  7. }


但是我需要CustomBean包含一些在@EnableCustomAutoConfiguration注解中指定的值。例如,如果我像这样修改EnableCustomAutoConfiguration

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Import(CustomAutoConfiguration.class)
  4. public @interface EnableCustomAutoConfiguration {
  5. String someString();
  6. }


然后,我希望someString可以在CustomAutoConfiguration中访问:

  1. @Configuration
  2. public class CustomAutoConfiguration {
  3. @Bean
  4. public CustomBean customBean() {
  5. String someString = ?? // How can I get the value of "someString" defined in the annotation?
  6. return new CustomBean(someString);
  7. }
  8. }


我如何才能做到这一点?

zmeyuzjn

zmeyuzjn1#

您可以通过ApplicationContext找到带注解的bean
例如:

  1. @Bean
  2. public CustomBean customBean(ApplicationContext applicationContext) {
  3. // get the annotated bean name
  4. Optional<String> customAutoConfigurationBeanName =
  5. applicationContext.getBeansWithAnnotation(EnableCustomAutoConfiguration.class)
  6. .keySet().stream().findFirst();
  7. if (customAutoConfigurationBeanName.isEmpty()) return null;
  8. // get the EnableCustomAutoConfiguration annotation
  9. EnableCustomAutoConfiguration enableCustomAutoConfiguration =
  10. applicationContext.findAnnotationOnBean(customAutoConfigurationBeanName.get(),
  11. EnableCustomAutoConfiguration.class);
  12. return new CustomBean(enableCustomAutoConfiguration.someString());
  13. }

字符串

展开查看全部
vc6uscn9

vc6uscn92#

如果你使用@Import,就没有必要再使用@Configuration了,它们都是将类注入到ioc容器中,如果我理解正确的话,你可以这样使用它们

  1. import lombok.extern.slf4j.Slf4j;
  2. import org.springframework.context.annotation.DeferredImportSelector;
  3. import org.springframework.core.annotation.AnnotationAttributes;
  4. import org.springframework.core.type.AnnotationMetadata;
  5. @Slf4j
  6. public class CustomAutoConfiguration implements DeferredImportSelector {
  7. @Override
  8. public String[] selectImports(AnnotationMetadata metadata) {
  9. AnnotationAttributes attributes = AnnotationAttributes
  10. .fromMap(metadata.getAnnotationAttributes(EnableCustomAutoConfiguration.class.getName(), true));
  11. if (attributes == null) {
  12. log.info("@EnableCustomAutoConfiguration is not present on importing class");
  13. return new String[0];
  14. }
  15. // this "someString" is the value of the property under the annotation you defined
  16. String someString = attributes.getString("someString");
  17. //Depending on the value of the property,
  18. //determine which configuration class you need to enable to load into the ioc container
  19. return new String[]{YourConfig.class.getName()};
  20. }
  21. }

字符串

展开查看全部

相关问题