Mercurial > hg > SpringPlayground
changeset 4:92d52e4ac567
JPA hinzugefuegt.
author | Dirk Olmes <dirk.olmes@codedo.de> |
---|---|
date | Thu, 13 Aug 2020 15:31:38 +0200 |
parents | b4221c1389af |
children | 227f3105fedd |
files | spring-boot-playground/pom.xml spring-boot-playground/src/main/java/de/comline/spring/application/Application.java spring-boot-playground/src/main/java/de/comline/spring/application/AutoConfiguredApplication.java spring-boot-playground/src/main/java/de/comline/spring/application/ExplicitlyConfiguredApplication.java spring-boot-playground/src/main/java/de/comline/spring/controller/MovieController.java spring-boot-playground/src/main/java/de/comline/spring/entity/Movie.java spring-boot-playground/src/main/java/de/comline/spring/repository/MovieRepository.java spring-boot-playground/src/main/java/de/comline/spring/service/MovieService.java |
diffstat | 8 files changed, 194 insertions(+), 67 deletions(-) [+] |
line wrap: on
line diff
--- 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 @@ <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> - + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-jpa</artifactId> + </dependency> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + </dependency> + <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId>
--- 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 - } -}
--- /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); + } + }; + } +}
--- /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 + } +}
--- /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"; + } +}
--- /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; + } +}
--- /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<Movie, Long> { +}
--- /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; + } +}