WIP
authorKai Moritz <kai@juplo.de>
Fri, 20 Nov 2020 21:19:22 +0000 (22:19 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 20 Nov 2020 22:01:11 +0000 (23:01 +0100)
pom.xml
src/main/java/de/juplo/demos/multiplebeans/MultipleBeansApplication.java
src/main/java/de/juplo/demos/multiplebeans/MultipleBeansBeanFactoryPostProcessor.java
src/main/java/de/juplo/demos/multiplebeans/MultipleBeansProperties.java
src/main/resources/application.yml
src/main/resources/templates/site.html [new file with mode: 0644]
src/main/resources/templates/view.html [deleted file]

diff --git a/pom.xml b/pom.xml
index c5a9621..0c9e4dc 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                        <artifactId>lombok</artifactId>
                        <optional>true</optional>
                </dependency>
+               <dependency>
+                       <groupId>org.webjars</groupId>
+                       <artifactId>bootstrap</artifactId>
+                       <version>4.5.2</version>
+                       <scope>runtime</scope>
+               </dependency>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-test</artifactId>
index 97cfb2a..cafcacf 100644 (file)
@@ -3,11 +3,16 @@ package de.juplo.demos.multiplebeans;
 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);
+       }
 
        public static void main(String[] args) {
                SpringApplication.run(MultipleBeansApplication.class, args);
index 0a89bcc..50bbbc7 100644 (file)
@@ -8,7 +8,6 @@ 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 {
@@ -16,7 +15,7 @@ public class MultipleBeansBeanFactoryPostProcessor implements BeanFactoryPostPro
         MultipleBeansProperties properties = factory.getBean(MultipleBeansProperties.class);
         BeanDefinitionRegistry registry = (BeanDefinitionRegistry)factory;
 
-        for (MultipleBeansProperties.Site site : properties.site) {
+        for (MultipleBeansProperties.Site site : properties.sites) {
             BeanDefinition beanDefinition =
                     BeanDefinitionBuilder
                             .genericBeanDefinition(SiteController.class)
index f8384ff..5d1e2fd 100644 (file)
@@ -4,12 +4,13 @@ import lombok.Getter;
 import lombok.Setter;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 
-@ConfigurationProperties("de.juplo")
+@ConfigurationProperties("juplo")
 @Getter
 @Setter
 public class MultipleBeansProperties {
 
-    Site[] site;
+    String project;
+    Site[] sites;
 
 
     @Getter
index 1f49f99..04397c1 100644 (file)
@@ -1,10 +1,6 @@
-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!
+juplo:
+  project: multiple-beans
+  sites:
+    - name: peter
+      description: This is the Home-Page of PETER
       
diff --git a/src/main/resources/templates/site.html b/src/main/resources/templates/site.html
new file mode 100644 (file)
index 0000000..2b66bb5
--- /dev/null
@@ -0,0 +1,22 @@
+<!DOCTYPE HTML>
+<html xmlns:th="http://www.thymeleaf.org" th:replace="~{layout :: pagelayout(~{:: title}, ~{:: h1}, ~{:: div.card-text})}">
+  <head>
+    <title th:text="${name}">SITE-TEMPLATE</title>
+    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+  </head>
+  <body>
+    <h1 th:text="${name}">SITE</h1>
+    <div class="card-text">
+      <p th:text="${description}">DESCRIPTION</p>
+    </div>
+    <div class="card-text">
+      <p>
+        <em>
+          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.
+        </em>
+      </p>
+    </div>
+  </body>
+</html>
diff --git a/src/main/resources/templates/view.html b/src/main/resources/templates/view.html
deleted file mode 100644 (file)
index 2b66bb5..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-<!DOCTYPE HTML>
-<html xmlns:th="http://www.thymeleaf.org" th:replace="~{layout :: pagelayout(~{:: title}, ~{:: h1}, ~{:: div.card-text})}">
-  <head>
-    <title th:text="${name}">SITE-TEMPLATE</title>
-    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
-    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-  </head>
-  <body>
-    <h1 th:text="${name}">SITE</h1>
-    <div class="card-text">
-      <p th:text="${description}">DESCRIPTION</p>
-    </div>
-    <div class="card-text">
-      <p>
-        <em>
-          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.
-        </em>
-      </p>
-    </div>
-  </body>
-</html>