view spring-boot-playground/src/main/java/de/comline/spring/application/Application.java @ 2:856e646efa17

Weg von der one-size-fits-all @EnableAutoConfiugration hin zu einer bewussten Auswahl von auto config Klassen
author Dirk Olmes <dirk.olmes@codedo.de>
date Thu, 13 Aug 2020 14:16:13 +0200
parents 60afb461bf6c
children b4221c1389af
line wrap: on
line source

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.CustomLogicController;
import de.comline.spring.controller.HelloController;
import de.comline.spring.service.CustomLogicService;

//@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,
			CustomLogicController.class,
			CustomLogicService.class
	})
	// @formatter:on
	static class CustomConfig {
		// no custom methods
	}
}