Mercurial > hg > SpringPlayground
annotate spring-boot-playground/src/main/java/de/comline/spring/application/Application.java @ 3:b4221c1389af
Nur die REST Endpunkte, die in der config explizit angezogen werden, sind nachher auch registriert
author | Dirk Olmes <dirk.olmes@codedo.de> |
---|---|
date | Thu, 13 Aug 2020 14:31:40 +0200 |
parents | 856e646efa17 |
children |
rev | line source |
---|---|
1
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
1 package de.comline.spring.application; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
2 |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
3 import java.util.Arrays; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
4 |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
5 import org.springframework.boot.CommandLineRunner; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
6 import org.springframework.boot.SpringApplication; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
7 import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
8 import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
9 import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
10 import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; |
2
856e646efa17
Weg von der one-size-fits-all @EnableAutoConfiugration hin zu einer bewussten Auswahl von auto config Klassen
Dirk Olmes <dirk.olmes@codedo.de>
parents:
1
diff
changeset
|
11 import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; |
1
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
12 import org.springframework.context.ApplicationContext; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
13 import org.springframework.context.annotation.Bean; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
14 import org.springframework.context.annotation.Configuration; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
15 import org.springframework.context.annotation.Import; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
16 |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
17 import de.comline.spring.application.Application.CustomConfig; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
18 import de.comline.spring.controller.HelloController; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
19 |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
20 //@SpringBootApplication |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
21 |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
22 //@ComponentScan(basePackages = "de.comline.spring.controller,de.comline.spring.service") |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
23 //@EnableAutoConfiguration |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
24 |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
25 @Import(CustomConfig.class) |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
26 public class Application { |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
27 public static void main(String[] args) { |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
28 SpringApplication.run(Application.class, args); |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
29 } |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
30 |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
31 @Bean |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
32 public CommandLineRunner commandLineRunner(ApplicationContext ctx) { |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
33 return args -> { |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
34 System.out.println("Let's inspect the beans provided by Spring Boot:"); |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
35 |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
36 String[] beanNames = ctx.getBeanDefinitionNames(); |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
37 Arrays.sort(beanNames); |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
38 for (String beanName : beanNames) { |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
39 System.out.println(beanName); |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
40 } |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
41 }; |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
42 } |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
43 |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
44 @Configuration |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
45 // scheint hier nicht zu funktionieren: |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
46 // @ComponentScan(basePackages = "de.comline.spring.controller,de.comline.spring.service") |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
47 |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
48 // ServletWebServerFactoryAutoConfiguration for bringing up the embedded tomcat |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
49 // DispatcherServletAutoConfiguration for detecting the REST controllers |
2
856e646efa17
Weg von der one-size-fits-all @EnableAutoConfiugration hin zu einer bewussten Auswahl von auto config Klassen
Dirk Olmes <dirk.olmes@codedo.de>
parents:
1
diff
changeset
|
50 // WebMvcAutoConfiguration enabling Jackson response serialization |
1
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
51 // @formatter:off |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
52 @Import({ |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
53 ServletWebServerFactoryAutoConfiguration.class, |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
54 DispatcherServletAutoConfiguration.class, |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
55 JacksonAutoConfiguration.class, |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
56 HttpMessageConvertersAutoConfiguration.class, |
2
856e646efa17
Weg von der one-size-fits-all @EnableAutoConfiugration hin zu einer bewussten Auswahl von auto config Klassen
Dirk Olmes <dirk.olmes@codedo.de>
parents:
1
diff
changeset
|
57 WebMvcAutoConfiguration.class, |
1
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
58 |
3
b4221c1389af
Nur die REST Endpunkte, die in der config explizit angezogen werden, sind nachher auch registriert
Dirk Olmes <dirk.olmes@codedo.de>
parents:
2
diff
changeset
|
59 HelloController.class |
b4221c1389af
Nur die REST Endpunkte, die in der config explizit angezogen werden, sind nachher auch registriert
Dirk Olmes <dirk.olmes@codedo.de>
parents:
2
diff
changeset
|
60 // Note how we do not list the CustomLogicController here, hence the controller is not bound |
1
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
61 }) |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
62 // @formatter:on |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
63 static class CustomConfig { |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
64 // no custom methods |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
65 } |
60afb461bf6c
Import des Spring boot playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
66 } |