简体   繁体   English

如何在 bean 运行之前延迟 Spring JPA 自动配置?

[英]How do I delay Spring JPA auto-configuration until a bean has run?

I have a Spring application that's trying to connect to a database.我有一个试图连接到数据库的 Spring 应用程序。 In order to connect, an SSH tunnel must first be established (using Jsch).为了连接,必须首先建立一个 SSH 隧道(使用 Jsch)。 How do I delay HibernateJpaAutoConfiguration until after the bean that's establishing the Jsch SSH session has returned?我如何延迟HibernateJpaAutoConfiguration直到建立 Jsch SSH 会话的 bean 返回之后? Currently the application is failing to start because the tunnel hasn't been opened yet.当前应用程序无法启动,因为隧道尚未打开。 When I try to exclude this autoconfiguration class, and then instantiate it explicitly predicated on the session bean already having been created, I get the following error:当我尝试排除这个自动配置类,然后根据已经创建的会话 bean 显式实例化它时,我收到以下错误:

Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!

I don't understand why I suddenly have to provide this myself when, if I rely on the auto-configuration, I don't have to provide it.我不明白为什么我突然必须自己提供这个,如果我依靠自动配置,我不必提供它。 If someone can show me a way to achieve this, that would be great.如果有人可以向我展示实现这一目标的方法,那就太好了。

POM:聚甲醛:

        ...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- Spring data JPA, default tomcat pool, exclude it -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        ...

Main application:主要应用:

@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
public class Application {
    public static void main(String... args) {
        SpringApplication.run(Application.class, args);
    }

    @Configuration
    static class SshTunnelConfiguration {
        @Bean
        public com.jcraft.jsch.Session sshTunnel() {
            ...
        }
    }

    @Configuration
    @ConditionalOnBean(com.jcraft.jsch.Session.class)
    static class DelayedJpaConfiguration extends HibernateJpaAutoConfiguration {
        public JpaConfiguration(DataSource dataSource, JpaProperties jpaProperties, ObjectProvider<JtaTransactionManager> jtaTransactionManager, ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
            super(dataSource, jpaProperties, jtaTransactionManager, transactionManagerCustomizers);
        }
    }
}

You would have to implement your own data source (by extending the one you use) and implementing the InitializingBean interface and in the 'afterPropertiesSet' method initialize your jsch tunnel.您必须实现自己的数据源(通过扩展您使用的数据源)并实现 InitializingBean 接口,并在“afterPropertiesSet”方法中初始化您的 jsch 隧道。

Please refer to this: Spring Data JPA with ssh tunnel to a remote MySQL server请参考: Spring Data JPA with ssh tunnel to a remote MySQL server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM