2017-03-08 82 views
1

有這樣一個bean:指定默認@Bean不使用@Primary春季啓動

@Bean 
TemplateEngine myTemplateEngine() {...} 

但春天的引導也已經定義了TemplateEngine一個bean:

組織/ springframework的的/ boot /自動配置/ thymeleaf/ThymeleafAutoConfiguration $ ThymeleafDefaultConfiguration.class

當我自動裝配TemplateEngine我得到

「字段需要一個bean,但2被發現」

我想春天啓動bean是默認的一個且僅當明確地指定使用我的豆腐,所以我不能使用@主要註釋,因爲我沒有訪問該bean定義。

另外,我想避免使用xml配置。

+0

你需要使用onconditional註釋,以確保您的bean被採摘春天靴子默認失敗 – rajadilipkolli

回答

2

嘗試定義你的bean作爲

@Bean 
@ConditionalOnMissingBean(TemplateEngine .class) 
TemplateEngine myTemplateEngine() {...}