我正在使用Sping Boot 3.2,我正在尝试编写一个annotation来导入一些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();
}
型
然后,我希望someString
可以在CustomAutoConfiguration
中访问:
@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);
}
}
型
我如何才能做到这一点?
2条答案
按热度按时间zmeyuzjn1#
您可以通过
ApplicationContext
找到带注解的bean例如:
字符串
vc6uscn92#
如果你使用@Import,就没有必要再使用@Configuration了,它们都是将类注入到ioc容器中,如果我理解正确的话,你可以这样使用它们
字符串