Skip to content

Commit

Permalink
#392 - Added example for deferred repository initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Aug 16, 2018
1 parent eb1add7 commit 4fe1ec3
Show file tree
Hide file tree
Showing 6,005 changed files with 100,327 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
210 changes: 210 additions & 0 deletions jpa/deferred/README.adoc

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions jpa/deferred/pom.xml
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>

<artifactId>spring-data-jpa-deferred</artifactId>
<name>Spring Data JPA - Deferred bootstrap modes</name>

<properties>
<spring-data-releasetrain.version>Lovelace-BUILD-SNAPSHOT</spring-data-releasetrain.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
88 changes: 88 additions & 0 deletions jpa/deferred/src/main/java/example/Application.java
@@ -0,0 +1,88 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;

import example.model.Customer;

import javax.sql.DataSource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.repository.config.BootstrapMode;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@SpringBootApplication
public class Application {

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

/**
* Configures a {@link LocalContainerEntityManagerFactoryBean} to use background initialization for the {@code lazy}
* and {@code deferred} profiles.
*
* @author Oliver Gierke
*/
@Configuration
@Profile({ "lazy", "deferred" })
static class LazyJpaConfiguration {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(JpaVendorAdapter jpaVendorAdapter,
DataSource dataSource, Environment environment) {

ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setDaemon(true);
threadPoolTaskExecutor.afterPropertiesSet();

LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setBootstrapExecutor(threadPoolTaskExecutor);
emf.setDataSource(dataSource);
emf.setJpaVendorAdapter(jpaVendorAdapter);
emf.setPackagesToScan(Customer.class.getPackage().getName());

return emf;
}
}

/**
* Bootstraps Spring Data JPA in lazy mode if the {@code lazy} profile is activated.
*
* @author Oliver Gierke
*/
@Profile("lazy")
@Configuration
@EnableJpaRepositories(bootstrapMode = BootstrapMode.LAZY)
static class LazyRepositoryConfiguration {}

/**
* Bootstraps Spring Data JPA in deferred mode if the {@code deferred} profile is activated.
*
* @author Oliver Gierke
*/
@Profile("deferred")
@Configuration
@EnableJpaRepositories(bootstrapMode = BootstrapMode.DEFERRED)
static class DeferredRepositoryConfiguration {}
}
27 changes: 27 additions & 0 deletions jpa/deferred/src/main/java/example/model/Customer.java
@@ -0,0 +1,27 @@
package example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer {

@Id @GeneratedValue(strategy = GenerationType.AUTO) private long id;
private String firstName;
private String lastName;

protected Customer() {}

public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}

}
27 changes: 27 additions & 0 deletions jpa/deferred/src/main/java/example/model/Customer1.java
@@ -0,0 +1,27 @@
package example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer1 {

@Id @GeneratedValue(strategy = GenerationType.AUTO) private long id;
private String firstName;
private String lastName;

protected Customer1() {}

public Customer1(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer1[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}

}
27 changes: 27 additions & 0 deletions jpa/deferred/src/main/java/example/model/Customer10.java
@@ -0,0 +1,27 @@
package example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer10 {

@Id @GeneratedValue(strategy = GenerationType.AUTO) private long id;
private String firstName;
private String lastName;

protected Customer10() {}

public Customer10(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer10[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}

}
27 changes: 27 additions & 0 deletions jpa/deferred/src/main/java/example/model/Customer100.java
@@ -0,0 +1,27 @@
package example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer100 {

@Id @GeneratedValue(strategy = GenerationType.AUTO) private long id;
private String firstName;
private String lastName;

protected Customer100() {}

public Customer100(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer100[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}

}
27 changes: 27 additions & 0 deletions jpa/deferred/src/main/java/example/model/Customer1000.java
@@ -0,0 +1,27 @@
package example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer1000 {

@Id @GeneratedValue(strategy = GenerationType.AUTO) private long id;
private String firstName;
private String lastName;

protected Customer1000() {}

public Customer1000(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer1000[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}

}
27 changes: 27 additions & 0 deletions jpa/deferred/src/main/java/example/model/Customer1001.java
@@ -0,0 +1,27 @@
package example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer1001 {

@Id @GeneratedValue(strategy = GenerationType.AUTO) private long id;
private String firstName;
private String lastName;

protected Customer1001() {}

public Customer1001(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer1001[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}

}
27 changes: 27 additions & 0 deletions jpa/deferred/src/main/java/example/model/Customer1002.java
@@ -0,0 +1,27 @@
package example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer1002 {

@Id @GeneratedValue(strategy = GenerationType.AUTO) private long id;
private String firstName;
private String lastName;

protected Customer1002() {}

public Customer1002(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer1002[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}

}
27 changes: 27 additions & 0 deletions jpa/deferred/src/main/java/example/model/Customer1003.java
@@ -0,0 +1,27 @@
package example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer1003 {

@Id @GeneratedValue(strategy = GenerationType.AUTO) private long id;
private String firstName;
private String lastName;

protected Customer1003() {}

public Customer1003(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer1003[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}

}
27 changes: 27 additions & 0 deletions jpa/deferred/src/main/java/example/model/Customer1004.java
@@ -0,0 +1,27 @@
package example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer1004 {

@Id @GeneratedValue(strategy = GenerationType.AUTO) private long id;
private String firstName;
private String lastName;

protected Customer1004() {}

public Customer1004(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer1004[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}

}
27 changes: 27 additions & 0 deletions jpa/deferred/src/main/java/example/model/Customer1005.java
@@ -0,0 +1,27 @@
package example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer1005 {

@Id @GeneratedValue(strategy = GenerationType.AUTO) private long id;
private String firstName;
private String lastName;

protected Customer1005() {}

public Customer1005(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer1005[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}

}

0 comments on commit 4fe1ec3

Please sign in to comment.