# HG changeset patch # User Dirk Olmes # Date 1597325498 -7200 # Node ID 92d52e4ac567855f79404d410198552dd21d7542 # Parent b4221c1389af7786aa4e7d70621796422dcbf94c JPA hinzugefuegt. diff -r b4221c1389af -r 92d52e4ac567 spring-boot-playground/pom.xml --- a/spring-boot-playground/pom.xml Thu Aug 13 14:31:40 2020 +0200 +++ b/spring-boot-playground/pom.xml Thu Aug 13 15:31:38 2020 +0200 @@ -42,7 +42,15 @@ org.springframework.boot spring-boot-starter-web - + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + org.springframework.boot spring-boot-starter-test diff -r b4221c1389af -r 92d52e4ac567 spring-boot-playground/src/main/java/de/comline/spring/application/Application.java --- a/spring-boot-playground/src/main/java/de/comline/spring/application/Application.java Thu Aug 13 14:31:40 2020 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,66 +0,0 @@ -package de.comline.spring.application; - -import java.util.Arrays; - -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; -import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; -import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; -import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; -import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; - -import de.comline.spring.application.Application.CustomConfig; -import de.comline.spring.controller.HelloController; - -//@SpringBootApplication - -//@ComponentScan(basePackages = "de.comline.spring.controller,de.comline.spring.service") -//@EnableAutoConfiguration - -@Import(CustomConfig.class) -public class Application { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } - - @Bean - public CommandLineRunner commandLineRunner(ApplicationContext ctx) { - return args -> { - System.out.println("Let's inspect the beans provided by Spring Boot:"); - - String[] beanNames = ctx.getBeanDefinitionNames(); - Arrays.sort(beanNames); - for (String beanName : beanNames) { - System.out.println(beanName); - } - }; - } - - @Configuration - // scheint hier nicht zu funktionieren: - // @ComponentScan(basePackages = "de.comline.spring.controller,de.comline.spring.service") - - // ServletWebServerFactoryAutoConfiguration for bringing up the embedded tomcat - // DispatcherServletAutoConfiguration for detecting the REST controllers - // WebMvcAutoConfiguration enabling Jackson response serialization - // @formatter:off - @Import({ - ServletWebServerFactoryAutoConfiguration.class, - DispatcherServletAutoConfiguration.class, - JacksonAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - WebMvcAutoConfiguration.class, - - HelloController.class - // Note how we do not list the CustomLogicController here, hence the controller is not bound - }) - // @formatter:on - static class CustomConfig { - // no custom methods - } -} diff -r b4221c1389af -r 92d52e4ac567 spring-boot-playground/src/main/java/de/comline/spring/application/AutoConfiguredApplication.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-boot-playground/src/main/java/de/comline/spring/application/AutoConfiguredApplication.java Thu Aug 13 15:31:38 2020 +0200 @@ -0,0 +1,38 @@ +package de.comline.spring.application; + +import java.util.Arrays; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +@ComponentScan(basePackages = { + "de.comline.spring.controller", + "de.comline.spring.service" +}) +@EnableAutoConfiguration +@EnableJpaRepositories("de.comline.spring.repository") +@EntityScan("de.comline.spring.entity") +public class AutoConfiguredApplication { + public static void main(String[] args) { + SpringApplication.run(AutoConfiguredApplication.class, args); + } + + @Bean + public CommandLineRunner commandLineRunner(ApplicationContext ctx) { + return args -> { + System.out.println("Let's inspect the beans provided by Spring Boot:"); + + String[] beanNames = ctx.getBeanDefinitionNames(); + Arrays.sort(beanNames); + for (String beanName : beanNames) { + System.out.println(beanName); + } + }; + } +} diff -r b4221c1389af -r 92d52e4ac567 spring-boot-playground/src/main/java/de/comline/spring/application/ExplicitlyConfiguredApplication.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-boot-playground/src/main/java/de/comline/spring/application/ExplicitlyConfiguredApplication.java Thu Aug 13 15:31:38 2020 +0200 @@ -0,0 +1,61 @@ +package de.comline.spring.application; + +import java.util.Arrays; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; +import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; +import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; +import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +import de.comline.spring.application.ExplicitlyConfiguredApplication.CustomConfig; +import de.comline.spring.controller.HelloController; + +@Import(CustomConfig.class) +public class ExplicitlyConfiguredApplication { + public static void main(String[] args) { + SpringApplication.run(ExplicitlyConfiguredApplication.class, args); + } + + @Bean + public CommandLineRunner commandLineRunner(ApplicationContext ctx) { + return args -> { + System.out.println("Let's inspect the beans provided by Spring Boot:"); + + String[] beanNames = ctx.getBeanDefinitionNames(); + Arrays.sort(beanNames); + for (String beanName : beanNames) { + System.out.println(beanName); + } + }; + } + + @Configuration + // scheint hier nicht zu funktionieren: + // @ComponentScan(basePackages = "de.comline.spring.controller,de.comline.spring.service") + + // ServletWebServerFactoryAutoConfiguration for bringing up the embedded tomcat + // DispatcherServletAutoConfiguration for detecting the REST controllers + // WebMvcAutoConfiguration enabling Jackson response serialization + // @formatter:off + @Import({ + ServletWebServerFactoryAutoConfiguration.class, + DispatcherServletAutoConfiguration.class, + JacksonAutoConfiguration.class, + HttpMessageConvertersAutoConfiguration.class, + WebMvcAutoConfiguration.class, + + HelloController.class + // Note how we do not list the CustomLogicController here, hence the controller is not bound + }) + // @formatter:on + static class CustomConfig { + // no custom methods + } +} diff -r b4221c1389af -r 92d52e4ac567 spring-boot-playground/src/main/java/de/comline/spring/controller/MovieController.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-boot-playground/src/main/java/de/comline/spring/controller/MovieController.java Thu Aug 13 15:31:38 2020 +0200 @@ -0,0 +1,20 @@ +package de.comline.spring.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import de.comline.spring.service.MovieService; + +@RestController +public class MovieController { + @Autowired + private MovieService service; + + @PostMapping("/movie/{title}") + public String index(@PathVariable(name = "title") String title) { + service.createMovie(title); + return "Successfully created movie " + title + ".\n"; + } +} diff -r b4221c1389af -r 92d52e4ac567 spring-boot-playground/src/main/java/de/comline/spring/entity/Movie.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-boot-playground/src/main/java/de/comline/spring/entity/Movie.java Thu Aug 13 15:31:38 2020 +0200 @@ -0,0 +1,36 @@ +package de.comline.spring.entity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "MOVIE") +public class Movie { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "ID") + private long id; + + @Column(name = "TITLE") + private String title; + + /** + * This default constructor is required for hibernate but should not be used from client code. + */ + @Deprecated + protected Movie() { + super(); + } + + public Movie(String title) { + this.title = title; + } + + public String getTitle() { + return title; + } +} diff -r b4221c1389af -r 92d52e4ac567 spring-boot-playground/src/main/java/de/comline/spring/repository/MovieRepository.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-boot-playground/src/main/java/de/comline/spring/repository/MovieRepository.java Thu Aug 13 15:31:38 2020 +0200 @@ -0,0 +1,8 @@ +package de.comline.spring.repository; + +import org.springframework.data.repository.CrudRepository; + +import de.comline.spring.entity.Movie; + +public interface MovieRepository extends CrudRepository { +} diff -r b4221c1389af -r 92d52e4ac567 spring-boot-playground/src/main/java/de/comline/spring/service/MovieService.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-boot-playground/src/main/java/de/comline/spring/service/MovieService.java Thu Aug 13 15:31:38 2020 +0200 @@ -0,0 +1,22 @@ +package de.comline.spring.service; + +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import de.comline.spring.entity.Movie; +import de.comline.spring.repository.MovieRepository; + +@Service +public class MovieService { + @Autowired + private MovieRepository repository; + + @Transactional + public Movie createMovie(String title) { + Movie movie = new Movie(title); + movie = repository.save(movie); + return movie; + } +}