【问题标题】:Create Bean and Name using @Bean annotation and Spring EL Expressions使用 @Bean 注解和 Spring EL 表达式创建 Bean 和 Name
【发布时间】:2017-04-24 14:35:15
【问题描述】:

我尝试使用@Bean(value="") 属性创建一个Bean 及其名称。 value 应设置为Spring EL Expression

@Configuration
public class TestClass{

   @Autowired
   TestProperty testProperty

   @Bean(value="#{testProperty.name}")
   public MyBean myBean(){
       MyBean b = new MyBean();
       return b;
   }
}

我的testProperty 是正确注入的,并拥有一个值,例如testProperty.name = "Bean1"

目前我创建的 Bean 只是将 EL 表达式作为其名称:{"bean":"#{testProperty.name}"

如何将 MyBean 的名称从 testProperty.name 命名?

【问题讨论】:

  • 我真的怀疑你是否可以使用 SpEL 或 bean 名称。你能详细说明你的用例吗?
  • @luboskrnac 我的用例是我必须在属性文件中创建外部配置的石英作业。所以它可以是一份或多份工作。我有关于这个问题的another open question。也许这可以给你上下文。
  • 我已经回答了你的一个非常相似的问题:stackoverflow.com/questions/40929161/…

标签: java spring spring-boot spring-el


【解决方案1】:

您可以为@Scheduled 注释的cron 属性使用属性值:

@Scheduled(cron = "${testProperty.name}")

所以根本不需要SpEL。

【讨论】:

    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 2020-03-21
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多