From 465911e902d8151204bddf0afe35902485cb90c9 Mon Sep 17 00:00:00 2001 From: petrelevich Date: Sat, 17 Feb 2024 02:07:53 +0300 Subject: [PATCH] 05-springBoot --- 2024-01/spring-05-springBoot/.gitignore | 24 +++++++ 2024-01/spring-05-springBoot/pom.xml | 68 +++++++++++++++++++ .../src/main/java/ru/otus/SpringBootDemo.java | 12 ++++ .../commandlinerunners/PreparationDev.java | 32 +++++++++ .../main/java/ru/otus/configs/AppProps.java | 39 +++++++++++ .../ru/otus/configs/ApplicationConfig.java | 17 +++++ .../ru/otus/configs/ApplicationMessage.java | 4 ++ .../java/ru/otus/service/GreetingService.java | 7 ++ .../service/GreetingServiceServiceImpl.java | 34 ++++++++++ .../main/java/ru/otus/service/LocalHello.java | 29 ++++++++ .../src/main/resources/application.yml | 15 ++++ .../src/main/resources/banner.txt | 1 + .../resources/i18n/appmessages.properties | 0 .../resources/i18n/appmessages_en.properties | 1 + .../i18n/appmessages_ru_RU.properties | 1 + .../src/main/resources/logback.xml | 13 ++++ 16 files changed, 297 insertions(+) create mode 100644 2024-01/spring-05-springBoot/.gitignore create mode 100644 2024-01/spring-05-springBoot/pom.xml create mode 100644 2024-01/spring-05-springBoot/src/main/java/ru/otus/SpringBootDemo.java create mode 100644 2024-01/spring-05-springBoot/src/main/java/ru/otus/commandlinerunners/PreparationDev.java create mode 100644 2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/AppProps.java create mode 100644 2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationConfig.java create mode 100644 2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationMessage.java create mode 100644 2024-01/spring-05-springBoot/src/main/java/ru/otus/service/GreetingService.java create mode 100644 2024-01/spring-05-springBoot/src/main/java/ru/otus/service/GreetingServiceServiceImpl.java create mode 100644 2024-01/spring-05-springBoot/src/main/java/ru/otus/service/LocalHello.java create mode 100644 2024-01/spring-05-springBoot/src/main/resources/application.yml create mode 100644 2024-01/spring-05-springBoot/src/main/resources/banner.txt create mode 100644 2024-01/spring-05-springBoot/src/main/resources/i18n/appmessages.properties create mode 100644 2024-01/spring-05-springBoot/src/main/resources/i18n/appmessages_en.properties create mode 100644 2024-01/spring-05-springBoot/src/main/resources/i18n/appmessages_ru_RU.properties create mode 100644 2024-01/spring-05-springBoot/src/main/resources/logback.xml diff --git a/2024-01/spring-05-springBoot/.gitignore b/2024-01/spring-05-springBoot/.gitignore new file mode 100644 index 00000000..4ea52072 --- /dev/null +++ b/2024-01/spring-05-springBoot/.gitignore @@ -0,0 +1,24 @@ +target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ diff --git a/2024-01/spring-05-springBoot/pom.xml b/2024-01/spring-05-springBoot/pom.xml new file mode 100644 index 00000000..ae219052 --- /dev/null +++ b/2024-01/spring-05-springBoot/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + ru.otus + spring-05-springBoot + 1.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.2.0 + + + UTF-8 + 17 + 17 + + 3.1.0 + 3.3.9 + + + + + + + org.springframework.boot + spring-boot-starter + + + + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + ${maven-enforcer-plugin.version} + + + enforce-maven + + enforce + + + + + + ${minimal.maven.version} + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + \ No newline at end of file diff --git a/2024-01/spring-05-springBoot/src/main/java/ru/otus/SpringBootDemo.java b/2024-01/spring-05-springBoot/src/main/java/ru/otus/SpringBootDemo.java new file mode 100644 index 00000000..63da3f54 --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/java/ru/otus/SpringBootDemo.java @@ -0,0 +1,12 @@ +package ru.otus; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootDemo { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemo.class, args); + } +} diff --git a/2024-01/spring-05-springBoot/src/main/java/ru/otus/commandlinerunners/PreparationDev.java b/2024-01/spring-05-springBoot/src/main/java/ru/otus/commandlinerunners/PreparationDev.java new file mode 100644 index 00000000..c4810bbe --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/java/ru/otus/commandlinerunners/PreparationDev.java @@ -0,0 +1,32 @@ +package ru.otus.commandlinerunners; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import ru.otus.service.GreetingService; + + +@Component +public class PreparationDev implements CommandLineRunner { + private static final Logger logger = LoggerFactory.getLogger(PreparationDev.class); + + private final GreetingService greetingService; + + public PreparationDev(GreetingService greetingService) { + this.greetingService = greetingService; + } + + @Override + public void run(String... args) { + //args парметры, котрые могут быть переданы в Main + var applArgs = Arrays.toString(args); + logger.info("DEV mode!!! Что-то настравиваем и подготавливаем, параметры: {} ", applArgs); + + var reply = greetingService.sayHello("Ivan"); + logger.info("reply:{}", reply); + } + +} diff --git a/2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/AppProps.java b/2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/AppProps.java new file mode 100644 index 00000000..4cc87f0a --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/AppProps.java @@ -0,0 +1,39 @@ +package ru.otus.configs; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import org.springframework.boot.context.properties.bind.ConstructorBinding; + +@ConfigurationProperties(prefix = "application") +public class AppProps { + private final String message; + private final Locale locale; + private final List data; + private final Map mapa; + + @ConstructorBinding + public AppProps(String message, Locale locale, List data, Map mapa) { + this.message = message; + this.locale = locale; + this.data = data; + this.mapa = mapa; + } + public String getMessage() { + return message; + } + public Locale getLocale() { + return locale; + } + + public List getData() { + return data; + } + + public Map getMapa() { + return mapa; + } +} diff --git a/2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationConfig.java b/2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationConfig.java new file mode 100644 index 00000000..7baf016b --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationConfig.java @@ -0,0 +1,17 @@ +package ru.otus.configs; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableConfigurationProperties(AppProps.class) +public class ApplicationConfig { + + + @Bean + public ApplicationMessage applicationMessage(@Value("${application.message}") String message) { + return new ApplicationMessage(message); + } +} diff --git a/2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationMessage.java b/2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationMessage.java new file mode 100644 index 00000000..ffc16c43 --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationMessage.java @@ -0,0 +1,4 @@ +package ru.otus.configs; + +public record ApplicationMessage(String message) { +} diff --git a/2024-01/spring-05-springBoot/src/main/java/ru/otus/service/GreetingService.java b/2024-01/spring-05-springBoot/src/main/java/ru/otus/service/GreetingService.java new file mode 100644 index 00000000..c0b48c82 --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/java/ru/otus/service/GreetingService.java @@ -0,0 +1,7 @@ +package ru.otus.service; + +import java.util.Map; + +public interface GreetingService { + Map sayHello(String name); +} diff --git a/2024-01/spring-05-springBoot/src/main/java/ru/otus/service/GreetingServiceServiceImpl.java b/2024-01/spring-05-springBoot/src/main/java/ru/otus/service/GreetingServiceServiceImpl.java new file mode 100644 index 00000000..92ab84c0 --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/java/ru/otus/service/GreetingServiceServiceImpl.java @@ -0,0 +1,34 @@ +package ru.otus.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.MessageSource; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; +import ru.otus.configs.AppProps; + +@Service +public class GreetingServiceServiceImpl implements GreetingService { + private static final Logger logger = LoggerFactory.getLogger(GreetingServiceServiceImpl.class); + private final MessageSource messageSource; + private final AppProps props; + + public GreetingServiceServiceImpl(MessageSource messageSource, AppProps props) { + this.messageSource = messageSource; + this.props = props; + logger.info("props.data:{}", props.getData()); + logger.info("props.mapa:{}", props.getMapa()); + } + + @Override + public Map sayHello(String name) { + var messageLocalized = messageSource.getMessage("hello.user", new String[]{"Ivan"}, props.getLocale()); + logger.info("Localization:{}", messageLocalized); + + Map map = new HashMap<>(); + map.put(name, messageLocalized); + return map; + } +} diff --git a/2024-01/spring-05-springBoot/src/main/java/ru/otus/service/LocalHello.java b/2024-01/spring-05-springBoot/src/main/java/ru/otus/service/LocalHello.java new file mode 100644 index 00000000..01d813ba --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/java/ru/otus/service/LocalHello.java @@ -0,0 +1,29 @@ +package ru.otus.service; + +import jakarta.annotation.PostConstruct; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import ru.otus.configs.AppProps; +import ru.otus.configs.ApplicationMessage; + + +@Component +public class LocalHello { + private static final Logger logger = LoggerFactory.getLogger(LocalHello.class); + + private final AppProps props; + private final String message; + + public LocalHello(AppProps props, ApplicationMessage applicationMessage) { + this.props = props; + this.message = applicationMessage.message(); + } + + //!!! Вообще, PostConstruct - это плохая практика !!! + @PostConstruct + public void printHello() { + logger.info("message from props:{}", props.getMessage()); + logger.info("message:{}", message); + } +} diff --git a/2024-01/spring-05-springBoot/src/main/resources/application.yml b/2024-01/spring-05-springBoot/src/main/resources/application.yml new file mode 100644 index 00000000..c9187e14 --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/resources/application.yml @@ -0,0 +1,15 @@ +spring: + messages: + basename: "i18n/appmessages" + encoding: "UTF-8" + +application: + message: "Test Props msg" + locale: ru_RU + data: + - "da1" + - "da2" + - "da3" + mapa: + key1: 12 + key2: 34 diff --git a/2024-01/spring-05-springBoot/src/main/resources/banner.txt b/2024-01/spring-05-springBoot/src/main/resources/banner.txt new file mode 100644 index 00000000..fb2d3fac --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/resources/banner.txt @@ -0,0 +1 @@ +Custom banner... \ No newline at end of file diff --git a/2024-01/spring-05-springBoot/src/main/resources/i18n/appmessages.properties b/2024-01/spring-05-springBoot/src/main/resources/i18n/appmessages.properties new file mode 100644 index 00000000..e69de29b diff --git a/2024-01/spring-05-springBoot/src/main/resources/i18n/appmessages_en.properties b/2024-01/spring-05-springBoot/src/main/resources/i18n/appmessages_en.properties new file mode 100644 index 00000000..e3bada4f --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/resources/i18n/appmessages_en.properties @@ -0,0 +1 @@ +hello.user="Hello, {0}"! \ No newline at end of file diff --git a/2024-01/spring-05-springBoot/src/main/resources/i18n/appmessages_ru_RU.properties b/2024-01/spring-05-springBoot/src/main/resources/i18n/appmessages_ru_RU.properties new file mode 100644 index 00000000..07b8dad3 --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/resources/i18n/appmessages_ru_RU.properties @@ -0,0 +1 @@ +hello.user="Привет, {0}"! \ No newline at end of file diff --git a/2024-01/spring-05-springBoot/src/main/resources/logback.xml b/2024-01/spring-05-springBoot/src/main/resources/logback.xml new file mode 100644 index 00000000..6fb07ce3 --- /dev/null +++ b/2024-01/spring-05-springBoot/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + +