WIP
authorKai Moritz <kai@juplo.de>
Fri, 20 Nov 2020 23:44:00 +0000 (00:44 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 20 Nov 2020 23:44:00 +0000 (00:44 +0100)
src/main/java/de/juplo/demos/multiplebeans/HomeController.java
src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplication.java
src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplicationContextInitializer.java
src/main/java/de/juplo/demos/multiplebeans/MultipleBeansEnvironmentPostProcessor.java
src/main/java/de/juplo/demos/multiplebeans/SiteController.java

index 2645129..5a488cf 100644 (file)
@@ -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;
+    }
 }
index cafcacf..3d97bdd 100644 (file)
@@ -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);
        }
index 02f42a5..7e1d867 100644 (file)
@@ -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<ConfigurableApplicationContext> {
 
+    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);
         }
     }
 }
index 132f6ee..e1313de 100644 (file)
@@ -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
index dd6290b..c3b9b9c 100644 (file)
@@ -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 {