From 254c003d101a442c879117316272ca36a756eced Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 20 Nov 2020 21:40:28 +0100 Subject: [PATCH] WIP --- .../MultipleBeansApplication.java | 4 +- ...MultipleBeansBeanFactoryPostProcessor.java | 30 +++++++++++ .../MultipleBeansProperties.java | 22 ++++++++ .../demos/multiplebeans/SiteController.java | 31 +++++++++++ src/main/resources/400.html | 15 ++++++ src/main/resources/application.properties | 1 - src/main/resources/application.yml | 10 ++++ src/main/resources/error.html | 51 +++++++++++++++++++ src/main/resources/layout.html | 20 ++++++++ src/main/resources/view.html | 22 ++++++++ 10 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/juplo/demos/multiplebeans/MultipleBeansBeanFactoryPostProcessor.java create mode 100644 src/main/java/de/juplo/demos/multiplebeans/MultipleBeansProperties.java create mode 100644 src/main/java/de/juplo/demos/multiplebeans/SiteController.java create mode 100644 src/main/resources/400.html delete mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/application.yml create mode 100644 src/main/resources/error.html create mode 100644 src/main/resources/layout.html create mode 100644 src/main/resources/view.html diff --git a/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplication.java b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplication.java index 618ba8e..97cfb2a 100644 --- a/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplication.java +++ b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplication.java @@ -2,12 +2,14 @@ package de.juplo.demos.multiplebeans; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication +@EnableConfigurationProperties(MultipleBeansProperties.class) public class MultipleBeansApplication { + public static void main(String[] args) { SpringApplication.run(MultipleBeansApplication.class, args); } - } diff --git a/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansBeanFactoryPostProcessor.java b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansBeanFactoryPostProcessor.java new file mode 100644 index 0000000..0a89bcc --- /dev/null +++ b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansBeanFactoryPostProcessor.java @@ -0,0 +1,30 @@ +package de.juplo.demos.multiplebeans; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.stereotype.Component; + +@Component +public class MultipleBeansBeanFactoryPostProcessor implements BeanFactoryPostProcessor { + + public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException { + + MultipleBeansProperties properties = factory.getBean(MultipleBeansProperties.class); + BeanDefinitionRegistry registry = (BeanDefinitionRegistry)factory; + + for (MultipleBeansProperties.Site site : properties.site) { + BeanDefinition beanDefinition = + BeanDefinitionBuilder + .genericBeanDefinition(SiteController.class) + .addConstructorArgValue(site.name) + .addConstructorArgValue(site.description) + .getBeanDefinition(); + + registry.registerBeanDefinition("name", beanDefinition); + } + } +} diff --git a/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansProperties.java b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansProperties.java new file mode 100644 index 0000000..f8384ff --- /dev/null +++ b/src/main/java/de/juplo/demos/multiplebeans/MultipleBeansProperties.java @@ -0,0 +1,22 @@ +package de.juplo.demos.multiplebeans; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("de.juplo") +@Getter +@Setter +public class MultipleBeansProperties { + + Site[] site; + + + @Getter + @Setter + public static class Site { + + String name; + String description; + } +} diff --git a/src/main/java/de/juplo/demos/multiplebeans/SiteController.java b/src/main/java/de/juplo/demos/multiplebeans/SiteController.java new file mode 100644 index 0000000..dd6290b --- /dev/null +++ b/src/main/java/de/juplo/demos/multiplebeans/SiteController.java @@ -0,0 +1,31 @@ +package de.juplo.demos.multiplebeans; + +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.Controller; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +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 { + + ModelAndView mav = new ModelAndView("site"); + mav.addObject("name", name); + mav.addObject("description", description); + return mav; + } +} diff --git a/src/main/resources/400.html b/src/main/resources/400.html new file mode 100644 index 0000000..36ba0c0 --- /dev/null +++ b/src/main/resources/400.html @@ -0,0 +1,15 @@ + + + + Testing Exception-Handling - Template for 400 + + + + +

Template for 400

+
+

EXCEPTION

+

Back to HOME

+
+ + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 8b13789..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..1f49f99 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,10 @@ +de: + juplo: + site: + - name: peter + description: This is the Home-Page of PETER + - name: ute + description: My name is Ute. I like cats! + - name: franz + description: Franz was here! + diff --git a/src/main/resources/error.html b/src/main/resources/error.html new file mode 100644 index 0000000..a5e8149 --- /dev/null +++ b/src/main/resources/error.html @@ -0,0 +1,51 @@ + + + + XXX: ERROR + + + + +

XXX: ERROR

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
StatusERROR.STATUS
ErrorERROR.ERROR
MessageERROR.MESSAGE
Request-IDERROR.REQUEST_ID
TimstampERROR.TIMESTAMP
PathERROR.PATH
Trace +
+
ERROR.TRACE
+
+
+

Back to HOME + + + diff --git a/src/main/resources/layout.html b/src/main/resources/layout.html new file mode 100644 index 0000000..3ad68ed --- /dev/null +++ b/src/main/resources/layout.html @@ -0,0 +1,20 @@ + + + + + + TITLE + + + +

+
+
+

HEADER

+
BODY
+
+
+ + diff --git a/src/main/resources/view.html b/src/main/resources/view.html new file mode 100644 index 0000000..2b66bb5 --- /dev/null +++ b/src/main/resources/view.html @@ -0,0 +1,22 @@ + + + + SITE-TEMPLATE + + + + +

SITE

+
+

DESCRIPTION

+
+
+

+ + Each site is defined in the configuration with name and description inside an array. + This demo shows, how to create multiple beans of the same type based on the configuration. + +

+
+ + -- 2.20.1