Skip to content

Instantly share code, notes, and snippets.

@joshlong
Created September 20, 2022 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshlong/2c5c66b4dac9a0fdb81744cb7dcd91e1 to your computer and use it in GitHub Desktop.
Save joshlong/2c5c66b4dac9a0fdb81744cb7dcd91e1 to your computer and use it in GitHub Desktop.
This is a trivial Spring Boot 3 AOT application doing all sorts of stuff
/*
add schema.sql
create table customer
(
id serial primary key,
name varchar(255) not null
);
add spring boot starters as usual, then add:
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
<metadataRepository>
<enabled>true</enabled>
<version>0.1.2</version>
</metadataRepository>
</configuration>
</plugin>
*/
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.boot.ApplicationRunner;
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.data.annotation.Id;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;
import java.util.stream.Stream;
import static org.springframework.web.servlet.function.RequestPredicates.GET;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
ApplicationRunner applicationRunner(CustomerRepository customerRepository) {
return args -> {
Stream.of("Bruce,Betty,Clark,Lana,Lois,Lex,Peter,Barry,Diana".split(","))
.forEach(n -> customerRepository.save(new Customer(null, n)));
customerRepository.findAll().forEach(System.out::println);
};
}
@Bean
RouterFunction<ServerResponse> routes(CustomerRepository repository) {
return RouterFunctions.route()
.route(GET("/customers"), r -> ServerResponse.ok().body(repository.findAll()))
.build();
}
}
interface CustomerRepository extends CrudRepository<Customer, Integer> {
}
record Customer(@Id Integer id, String name) {
}
@Configuration
@EnableBatchProcessing
class JobConfiguration {
@Bean
Job job(JobRepository repository, Step step) {
return new JobBuilder("job", repository)
.start(step)
.build();
}
@Bean
Step step(JobRepository repository, PlatformTransactionManager transactionManager) {
return new StepBuilder("step", repository)
.tasklet((contribution, chunkContext) -> {
System.out.println("veni vidi batch!");
return null;
}, transactionManager)
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment