java - 此位置不允许使用 Spring 注释 @Profile

标签 java spring annotations

我有一个配置文件,我想根据配置文件创建不同的 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 中引入的。

  • @Profile在版本 3.2.9.RELEASE 中仅允许将此注释放置在类型上。

    @Target(value=TYPE)

  • @Profile在版本 4.0.0.RELEASE 中允许将此注释放置在类型和方法上。

    @Target(value={TYPE,METHOD})

关于java - 此位置不允许使用 Spring 注释 @Profile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33822585/

相关文章:

java - tomcat - HTTP 连接器 maxThreads/acceptCount 与 JDBC 池 maxActive 之间的比率

java - 使用 String bean 作为 SPEL 中的键从映射中获取值

java - 身份验证请求失败 : Bad credentials [LDAP: error code 49 - data 52e, v1db1]

java - 如何在带有注释的mybatis和oracle中插入时返回ID

java - Java 中的 gem 棋游戏 - 在 while 循环中使用 int 数组

java - 在对数函数中使用递归幂函数,无需 Math.functions

java - java中的Scanner如何向hasNext()返回true?

java - RESTEasy-Spring 集成 web 应用程序抛出神秘错误 : NoResourceFoundFailure

java - 如果存在注释,我该如何执行某些功能?

java - FetchType.LAZY 位于父级的 @OneToMany 上,FetchType.EAGER 位于子级的 @ManyToOne 上