我有一个配置文件,我想根据配置文件创建不同的 bean。
出于某种原因,这是有效的:
@Configuration
@Profile("myProfile")
public class myClass {
这不是,在 Eclipse 中给出错误消息:
The annotation
@Profile
is disallowed for this location
@Bean
@Profile("myProfile")
我宁愿使用第二个,但我不确定是否可以。 Spring API 说它应该可以工作:
The
@Profile
annotation may be used in any of the following ways:
- as a type-level annotation on any class directly or indirectly annotated with
@Component
, including@Configuration
classes- as a meta-annotation, for the purpose of composing custom stereotype annotations
- as a method-level annotation on any @Bean method
我使用的是Sping Framework 3.1.0,是否有可能是在之后才引入了@Bean
方法的使用?
最佳答案
是的,它是在 Spring 4 中引入的。
关于java - 此位置不允许使用 Spring 注释 @Profile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33822585/