-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;
+ }
}
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);
}
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);
}
}
}
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
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 {