From: Kai Moritz Date: Fri, 20 Nov 2020 23:44:00 +0000 (+0100) Subject: WIP X-Git-Tag: wip-implementation~1 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=d6e70a1c578c1cfa5a08f1d19bd309d6e7e55b43;p=demos%2Fmultiple-beans WIP --- diff --git a/src/main/java/de/juplo/demos/multiplebeans/HomeController.java b/src/main/java/de/juplo/demos/multiplebeans/HomeController.java index 2645129..5a488cf 100644 --- a/src/main/java/de/juplo/demos/multiplebeans/HomeController.java +++ b/src/main/java/de/juplo/demos/multiplebeans/HomeController.java @@ -1,2 +1,23 @@ -package de.juplo.demos.multiplebeans;public class HomeController { +package de.juplo.demos.multiplebeans; + +import lombok.RequiredArgsConstructor; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.Controller; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@RequiredArgsConstructor +public class HomeController implements Controller { + + private final String[] sites; + + + @Override + public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { + + ModelAndView mav = new ModelAndView("home"); + mav.addObject("sites", sites); + return mav; + } } diff --git a/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplication.java b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplication.java index cafcacf..3d97bdd 100644 --- a/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplication.java +++ b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplication.java @@ -1,19 +1,21 @@ package de.juplo.demos.multiplebeans; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; @SpringBootApplication -@EnableConfigurationProperties(MultipleBeansProperties.class) public class MultipleBeansApplication { - @Bean(name = "/peter") - public SiteController controller(MultipleBeansProperties properties) { - return new SiteController(properties.sites[0].name, properties.sites[0].description); + @Bean("/") + public HomeController homeController(@Value("${juplo.sites}")String[] sites) { + + return new HomeController(sites); } + public static void main(String[] args) { SpringApplication.run(MultipleBeansApplication.class, args); } diff --git a/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplicationContextInitializer.java b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplicationContextInitializer.java index 02f42a5..7e1d867 100644 --- a/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplicationContextInitializer.java +++ b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplicationContextInitializer.java @@ -1,22 +1,25 @@ package de.juplo.demos.multiplebeans; +import lombok.AllArgsConstructor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.stereotype.Component; +@AllArgsConstructor public class MultipleBeansApplicationContextInitializer implements ApplicationContextInitializer { + private final String[] sites; + + @Override public void initialize(ConfigurableApplicationContext context) { - context.refresh(); - MultipleBeansProperties properties = context.getBean(MultipleBeansProperties.class); ConfigurableListableBeanFactory factory = context.getBeanFactory(); - for (MultipleBeansProperties.Site site : properties.sites) { - SiteController controller = new SiteController(site.name, site.description); - factory.registerSingleton("/" + site.name, controller); + for (String site : sites) { + SiteController controller = new SiteController(site, "Descrition of site " + site); + factory.registerSingleton("/" + site, controller); } } } diff --git a/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansEnvironmentPostProcessor.java b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansEnvironmentPostProcessor.java index 132f6ee..e1313de 100644 --- a/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansEnvironmentPostProcessor.java +++ b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansEnvironmentPostProcessor.java @@ -1,20 +1,31 @@ package de.juplo.demos.multiplebeans; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import lombok.AllArgsConstructor; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertySource; -import java.util.regex.Pattern; +import java.util.Arrays; +@AllArgsConstructor public class MultipleBeansEnvironmentPostProcessor implements EnvironmentPostProcessor { - public final static Pattern PATTERN = Pattern.compile("juplo\\.sites\\[([0-9]+)\\]\\.(.+)"); @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + String sites = null; for (PropertySource source : environment.getPropertySources()) { - System.out.println(source.getName()); + Object found = source.getProperty("juplo.sites"); + if (found != null) + sites = found.toString(); } + + if (sites == null) + throw new IllegalArgumentException("Parameter juplo.sites is not set!"); + + application.addInitializers(new MultipleBeansApplicationContextInitializer( + Arrays.stream(sites.split(",")) + .map(site -> site.trim()) + .toArray(size -> new String[size]))); } -} +} \ No newline at end of file diff --git a/src/main/java/de/juplo/demos/multiplebeans/SiteController.java b/src/main/java/de/juplo/demos/multiplebeans/SiteController.java index dd6290b..c3b9b9c 100644 --- a/src/main/java/de/juplo/demos/multiplebeans/SiteController.java +++ b/src/main/java/de/juplo/demos/multiplebeans/SiteController.java @@ -1,24 +1,21 @@ package de.juplo.demos.multiplebeans; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +@RequiredArgsConstructor public class SiteController implements Controller { private final String name; private final String description; - public SiteController(String name, String description) { - - this.name = name; - this.description = description; - } - - public ModelAndView handleRequest( HttpServletRequest request, HttpServletResponse response) throws Exception {