使用@Bean注释和 Spring EL 表达式创建 Bean 和名称



我尝试使用@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}"

我怎样才能从testProperty.nameMyBean起名字?

您可以将属性值用于@Scheduled注释的cron属性:

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

所以根本不需要SpEL。

最新更新