From 0a5ccdaac8a76caf423f764bef5b5302aa5d629f Mon Sep 17 00:00:00 2001 From: petrelevich Date: Sun, 9 Apr 2023 23:00:11 +0300 Subject: [PATCH] L05-springBoot --- 2023-03/spring-05-springBoot/.gitignore | 24 +++++++ 2023-03/spring-05-springBoot/pom.xml | 68 +++++++++++++++++++ .../src/main/java/ru/otus/SpringBootDemo.java | 14 ++++ .../commandlinerunners/PreparationDev.java | 32 +++++++++ .../main/java/ru/otus/configs/AppProps.java | 28 ++++++++ .../ru/otus/configs/ApplicationConfig.java | 9 +++ .../java/ru/otus/service/GreetingService.java | 7 ++ .../service/GreetingServiceServiceImpl.java | 17 +++++ .../main/java/ru/otus/service/LocalHello.java | 35 ++++++++++ .../java/wrongPackage/PreparationUnKnown.java | 17 +++++ .../src/main/resources/application.yml | 17 +++++ .../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, 284 insertions(+) create mode 100644 2023-03/spring-05-springBoot/.gitignore create mode 100644 2023-03/spring-05-springBoot/pom.xml create mode 100644 2023-03/spring-05-springBoot/src/main/java/ru/otus/SpringBootDemo.java create mode 100644 2023-03/spring-05-springBoot/src/main/java/ru/otus/commandlinerunners/PreparationDev.java create mode 100644 2023-03/spring-05-springBoot/src/main/java/ru/otus/configs/AppProps.java create mode 100644 2023-03/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationConfig.java create mode 100644 2023-03/spring-05-springBoot/src/main/java/ru/otus/service/GreetingService.java create mode 100644 2023-03/spring-05-springBoot/src/main/java/ru/otus/service/GreetingServiceServiceImpl.java create mode 100644 2023-03/spring-05-springBoot/src/main/java/ru/otus/service/LocalHello.java create mode 100644 2023-03/spring-05-springBoot/src/main/java/wrongPackage/PreparationUnKnown.java create mode 100644 2023-03/spring-05-springBoot/src/main/resources/application.yml create mode 100644 2023-03/spring-05-springBoot/src/main/resources/banner.txt create mode 100644 2023-03/spring-05-springBoot/src/main/resources/i18n/appmessages.properties create mode 100644 2023-03/spring-05-springBoot/src/main/resources/i18n/appmessages_en.properties create mode 100644 2023-03/spring-05-springBoot/src/main/resources/i18n/appmessages_ru_RU.properties create mode 100644 2023-03/spring-05-springBoot/src/main/resources/logback.xml diff --git a/2023-03/spring-05-springBoot/.gitignore b/2023-03/spring-05-springBoot/.gitignore new file mode 100644 index 00000000..4ea52072 --- /dev/null +++ b/2023-03/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/2023-03/spring-05-springBoot/pom.xml b/2023-03/spring-05-springBoot/pom.xml new file mode 100644 index 00000000..d4b32ca5 --- /dev/null +++ b/2023-03/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.0.5 + + + 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/2023-03/spring-05-springBoot/src/main/java/ru/otus/SpringBootDemo.java b/2023-03/spring-05-springBoot/src/main/java/ru/otus/SpringBootDemo.java new file mode 100644 index 00000000..996a8eda --- /dev/null +++ b/2023-03/spring-05-springBoot/src/main/java/ru/otus/SpringBootDemo.java @@ -0,0 +1,14 @@ +package ru.otus; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +//http://localhost:8080/actuator/health + +@SpringBootApplication +public class SpringBootDemo { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemo.class, args); + } +} diff --git a/2023-03/spring-05-springBoot/src/main/java/ru/otus/commandlinerunners/PreparationDev.java b/2023-03/spring-05-springBoot/src/main/java/ru/otus/commandlinerunners/PreparationDev.java new file mode 100644 index 00000000..c4810bbe --- /dev/null +++ b/2023-03/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/2023-03/spring-05-springBoot/src/main/java/ru/otus/configs/AppProps.java b/2023-03/spring-05-springBoot/src/main/java/ru/otus/configs/AppProps.java new file mode 100644 index 00000000..3705d682 --- /dev/null +++ b/2023-03/spring-05-springBoot/src/main/java/ru/otus/configs/AppProps.java @@ -0,0 +1,28 @@ +package ru.otus.configs; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.Locale; + +@ConfigurationProperties(prefix = "application") +public class AppProps { + + private String message; + private Locale locale; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Locale getLocale() { + return locale; + } + + public void setLocale(Locale locale) { + this.locale = locale; + } +} diff --git a/2023-03/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationConfig.java b/2023-03/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationConfig.java new file mode 100644 index 00000000..975eb40d --- /dev/null +++ b/2023-03/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationConfig.java @@ -0,0 +1,9 @@ +package ru.otus.configs; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableConfigurationProperties(AppProps.class) +public class ApplicationConfig { +} diff --git a/2023-03/spring-05-springBoot/src/main/java/ru/otus/service/GreetingService.java b/2023-03/spring-05-springBoot/src/main/java/ru/otus/service/GreetingService.java new file mode 100644 index 00000000..c0b48c82 --- /dev/null +++ b/2023-03/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/2023-03/spring-05-springBoot/src/main/java/ru/otus/service/GreetingServiceServiceImpl.java b/2023-03/spring-05-springBoot/src/main/java/ru/otus/service/GreetingServiceServiceImpl.java new file mode 100644 index 00000000..8501ceae --- /dev/null +++ b/2023-03/spring-05-springBoot/src/main/java/ru/otus/service/GreetingServiceServiceImpl.java @@ -0,0 +1,17 @@ +package ru.otus.service; + +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +@Service +public class GreetingServiceServiceImpl implements GreetingService { + + @Override + public Map sayHello(String name) { + Map map = new HashMap<>(); + map.put(name, "Hello, " + name); + return map; + } +} diff --git a/2023-03/spring-05-springBoot/src/main/java/ru/otus/service/LocalHello.java b/2023-03/spring-05-springBoot/src/main/java/ru/otus/service/LocalHello.java new file mode 100644 index 00000000..6dddf223 --- /dev/null +++ b/2023-03/spring-05-springBoot/src/main/java/ru/otus/service/LocalHello.java @@ -0,0 +1,35 @@ +package ru.otus.service; + +import jakarta.annotation.PostConstruct; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.MessageSource; +import org.springframework.stereotype.Component; +import ru.otus.configs.AppProps; + + +@Component +public class LocalHello { + private static final Logger logger = LoggerFactory.getLogger(LocalHello.class); + + private final MessageSource messageSource; + private final AppProps props; + private final String message; + + public LocalHello(MessageSource messageSource, AppProps props, @Value("${application.message}") String message) { + this.messageSource = messageSource; + this.props = props; + this.message = message; + } + + //!!! Вообще, PostConstruct - это плохая практика !!! + @PostConstruct + public void printHello() { + var messageLocalized = messageSource.getMessage("hello.user", new String[]{"Ivan"}, props.getLocale()); + logger.info("Localization:{}", messageLocalized); + + logger.info("message from props:{}", props.getMessage()); + logger.info("message:{}", message); + } +} diff --git a/2023-03/spring-05-springBoot/src/main/java/wrongPackage/PreparationUnKnown.java b/2023-03/spring-05-springBoot/src/main/java/wrongPackage/PreparationUnKnown.java new file mode 100644 index 00000000..4900dfb6 --- /dev/null +++ b/2023-03/spring-05-springBoot/src/main/java/wrongPackage/PreparationUnKnown.java @@ -0,0 +1,17 @@ +package wrongPackage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +//Этот компонент не будет найден, т.к. нарушена иерархия пакетов +@Component +public class PreparationUnKnown implements CommandLineRunner { + private static final Logger logger = LoggerFactory.getLogger(PreparationUnKnown.class); + + @Override + public void run(String... args) throws Exception { + logger.info("Это не увидим никогда"); + } +} diff --git a/2023-03/spring-05-springBoot/src/main/resources/application.yml b/2023-03/spring-05-springBoot/src/main/resources/application.yml new file mode 100644 index 00000000..425b46b2 --- /dev/null +++ b/2023-03/spring-05-springBoot/src/main/resources/application.yml @@ -0,0 +1,17 @@ +spring: + messages: + basename: "i18n/appmessages" + encoding: "UTF-8" + +management: + endpoints: + web: + exposure: + include: info, health, beans + +server: + port: 8080 + +application: + message: "Test Props msg" + locale: ru_RU diff --git a/2023-03/spring-05-springBoot/src/main/resources/banner.txt b/2023-03/spring-05-springBoot/src/main/resources/banner.txt new file mode 100644 index 00000000..fb2d3fac --- /dev/null +++ b/2023-03/spring-05-springBoot/src/main/resources/banner.txt @@ -0,0 +1 @@ +Custom banner... \ No newline at end of file diff --git a/2023-03/spring-05-springBoot/src/main/resources/i18n/appmessages.properties b/2023-03/spring-05-springBoot/src/main/resources/i18n/appmessages.properties new file mode 100644 index 00000000..e69de29b diff --git a/2023-03/spring-05-springBoot/src/main/resources/i18n/appmessages_en.properties b/2023-03/spring-05-springBoot/src/main/resources/i18n/appmessages_en.properties new file mode 100644 index 00000000..e3bada4f --- /dev/null +++ b/2023-03/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/2023-03/spring-05-springBoot/src/main/resources/i18n/appmessages_ru_RU.properties b/2023-03/spring-05-springBoot/src/main/resources/i18n/appmessages_ru_RU.properties new file mode 100644 index 00000000..07b8dad3 --- /dev/null +++ b/2023-03/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/2023-03/spring-05-springBoot/src/main/resources/logback.xml b/2023-03/spring-05-springBoot/src/main/resources/logback.xml new file mode 100644 index 00000000..6fb07ce3 --- /dev/null +++ b/2023-03/spring-05-springBoot/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + +