java - 结合2个Spring boot应用

标签 java spring spring-boot spring-java-config

只需遵循 Spring 指南 http://spring.io/guides#gs我采用了 gs-rest-service 和 gs-accessing-data-jpa。现在我想将它们组合在一个应用程序中,这就是需要更多地了解新的 org.springframework.boot.SpringApplication 的地方。

gs-rest-service配置中看起来令人惊叹,但几乎不存在

@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

gs-accessing-data-jpa 更像是基于 Spring XML JavaConfig 的应用程序。

@Configuration
@EnableJpaRepositories
public class CopyOfApplication {

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder().setType(H2).build();
    }

   // 2 other beans...

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }


    public static void main(String[] args) {
        AbstractApplicationContext context = new AnnotationConfigApplicationContext(CopyOfApplication.class);
        CustomerRepository repository = context.getBean(CustomerRepository.class);

        //...
    }

}

如何组合它们?

这是否意味着我现在需要在更详细的级别上重新编写 SpringApplication.run

最佳答案

在 Spring Boot 应用程序中,只需添加 JPA 示例中的依赖项(您还没有的依赖项。

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M7")
    compile("org.springframework:spring-orm:4.0.0.RC1")
    compile("org.springframework.data:spring-data-jpa:1.4.1.RELEASE")
    compile("org.hibernate:hibernate-entitymanager:4.2.1.Final")
    compile("com.h2database:h2:1.3.172")
    testCompile("junit:junit:4.11")
}

或者您也可以使用 spring boot starter project 来代替这个适用于 Spring Data JPA。在这种情况下,依赖关系将如下所示。

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M7")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:0.5.0.M7")
    compile("com.h2database:h2:1.3.172")
    testCompile("junit:junit:4.11")
}

这将引入所有需要的依赖项。

接下来将 CustomerRepository 复制到 Spring Boot 应用程序。这基本上应该是你需要的一切。 Spring Boot 自动配置现在可以检测 Spring Data JPA 和 JPA,并将为您引导 hibernate、spring data。

@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        ApplicationContext context= SpringApplication.run(Application.class, args);
        CustomerRepository repository = context.getBean(CustomerRepository.class);
        //...
    }
}

关于java - 结合2个Spring boot应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21041126/

相关文章:

java - 将变量合并到 <Applet> 标记中以在 CMS (Joomla) 中使用

java - 在Eclipse中设置Apache Squoop 1.4.4开发环境

java - 我可以在 mvn 中指定自定义生命周期方法吗

java - Spring ,JPA, hibernate ,Tomcat : Unable to find persistence unit when loading Spring application context

java 11 和 spring boot 错误 : this_class should be module-info

java - SpringMVC、c3p0、hibernate、JPA 应用程序泄漏连接导致 Too Many Connections 错误

java - 如何在java中多次调用launch()

java - 访问 Spring @RequestBody 中动态添加的属性

java - 502 错误网关和 "Please try again in 30 seconds"消息

apache - Apache 背后的 Tomcat 使用 ajp 进行 Spring Boot 应用程序