我使用的是Spring Boot3.2,并且我正在try 编写一个注释来导入一些Spring配置.这是我所拥有的:
@Configuration
public class CustomAutoConfiguration {
@Bean
public CustomBean customBean() {
return new CustomBean();
}
}
然后我有这样的注解:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(CustomAutoConfiguration.class)
public @interface EnableCustomAutoConfiguration {
}
然后我可以像这样启用:
@SpringBootApplication
@EnableCustomAutoConfiguration
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
但我需要CustomBean
包含@EnableCustomAutoConfiguration
批注中指定的一些值.例如,如果我这样修改EnableCustomAutoConfiguration
:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(CustomAutoConfiguration.class)
public @interface EnableCustomAutoConfiguration {
String someString();
}
然后,我希望在CustomAutoConfiguration
中可以访问someString
:
@Configuration
public class CustomAutoConfiguration {
@Bean
public CustomBean customBean() {
String someString = ?? // How can I get the value of "someString" defined in the annotation?
return new CustomBean(someString);
}
}
我怎样才能做到这一点呢?