Question

Just following Spring Guides http://spring.io/guides#gs I took gs-rest-service and gs-accessing-data-jpa. Now I want to combine them in one application, and that is where like more understanding of new org.springframework.boot.SpringApplication is needed.

In gs-rest-service config looks emazing, that is almost absent

@ComponentScan
@EnableAutoConfiguration
public class Application {

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

gs-accessing-data-jpa is more like Spring XML JavaConfig based app.

@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);

        //...
    }

}

How to combine them?

Does it mean that I need to re-write SpringApplication.run now on more detailed level ?

Was it helpful?

Solution

In the Spring Boot application simply add the dependencies from the JPA sample (the ones which you don't already have.

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")
}

or instead of this you could also use the spring boot starter project for Spring Data JPA. In that case the dependencies would look like the following.

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")
}

This will pull in all needed dependencies.

Next copy the CustomerRepository to the Spring Boot application. That basically should be everything you need. Spring Boot auto-configure now detects Spring Data JPA and JPA and will bootstrap hibernate, spring data for you.

@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        ApplicationContext context= SpringApplication.run(Application.class, args);
        CustomerRepository repository = context.getBean(CustomerRepository.class);
        //...
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top