簡體   English   中英

WebMvcAutoConfiguration與多個外部EntityManager

[英]WebMvcAutoConfiguration with multiple external EntityManager

在將現有的Spring Web應用程序遷移到Spring Boot 1.5.13的過程中出現問題。 我已經處理了幾乎所有內容,但無法將良好的持久性單元注入WebMvcAutoConfiguration。 我實際上有3個從類路徑導入的不同的實體管理器工廠(由3個不同的內部庫作為.xml文件提供,我無法更改它們)。 他們每個人都以這種方式分裂:

<sqe-db:jpa-emf database-definition-name="db-name" embedded-datasource="false"/>

<bean id="transaction-mnanager-name" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="jpa-lib-name" />
    </bean>

數據源配置目前由tomcat提供,但是我將把它們外部化為dev環境的application.properties。

遷移之后,我找不到任何方法為WebMvcAutoConfiguration指定正確的bean,這導致以下錯誤:

Method requestMappingHandlerMapping in org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration required a single bean, but 3 were found:
    - jpa-lib1-name1: defined in null
    - jpa-lib1-name2: defined in null
    - jpa-lib1-name3: defined in null

我現在的目標是采用以下配置:

  • 使應用程序既可以作為SpringApplication運行,也可以作為WAR部署(我想我已經正確地做到了)
  • 甚至可以在類路徑中使用多個實體工廠的情況下,對Web應用程序使用Spring Boot自動配置的方法
  • 將我自己的數據源用於“開發”配置文件,但將tomcat一個留給其他環境。

我的要求或多或少是:-最小化對現有應用程序所需的更改-繼續使用庫提供的.xml

一個簡單的選擇是使用更現代的注釋驅動的配置來重寫配置,但是我寧願堅持使用現有的實現,因此在發布新庫時不必更改它。

在不觸摸現有xml文件的情況下聲明@Primary bean的任何方法?

任何想法或不同的方法都非常歡迎

經過一整夜的睡眠,我找到了最簡單的解決方案。

我將其添加到配置類中:

@Autowired
@Qualifier("name-of-default-factory")
private EntityManagerFactory entityManagerFactory;

@Bean
@Primary
public EntityManagerFactory getEntityManagerFactory() {
    return entityManagerFactory;
}

現在它將正確地注入了主bean。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM