2026-01 spring-05-springBoot

This commit is contained in:
stvort
2026-02-21 11:21:58 +04:00
parent dbe479921c
commit 68a6704283
22 changed files with 475 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
.idea/
*.iml
target/
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.otus</groupId>
<artifactId>greeting-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>greeting-app</name>
<description>Spring Boot project demo</description>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<io.starter.version>0.0.1-SNAPSHOT</io.starter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>ru.otus</groupId>
<artifactId>io-starter</artifactId>
<version>${io.starter.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>${lombok.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,17 @@
package ru.otus.greetingapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import ru.otus.greetingapp.config.OtusIOConfig;
//@EnableConfigurationProperties(OtusIOConfig.class)
@SpringBootApplication
public class GreetingAppApplication {
public static void main(String[] args) {
SpringApplication.run(GreetingAppApplication.class, args);
}
}
@@ -0,0 +1,29 @@
package ru.otus.greetingapp.config;
import lombok.Getter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.ConstructorBinding;
import ru.otus.io.config.LocaleProvider;
import java.util.Locale;
@ToString
@ConfigurationProperties(prefix = "otus.custom")
public class OtusIOConfig implements LocaleProvider {
@Getter
private final boolean ioEnabled;
private final Locale locale;
@ConstructorBinding
public OtusIOConfig(boolean ioEnabled, String locale) {
this.ioEnabled = ioEnabled;
this.locale = Locale.forLanguageTag(locale);
}
@Override
public Locale getCurrent() {
return locale;
}
}
@@ -0,0 +1,47 @@
package ru.otus.greetingapp.runner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import ru.otus.greetingapp.config.OtusIOConfig;
import ru.otus.io.services.IOService;
import ru.otus.io.services.LocalizationService;
import static java.util.Objects.isNull;
@Component
public class AppRunner implements ApplicationRunner {
private final OtusIOConfig ioConfig;
private final IOService ioService;
private final LocalizationService localizationService;
public AppRunner(@Autowired(required = false) OtusIOConfig ioConfig,
@Autowired(required = false) IOService ioService,
@Autowired(required = false) LocalizationService localizationService) {
this.ioConfig = ioConfig;
this.ioService = ioService;
this.localizationService = localizationService;
}
@Override
public void run(ApplicationArguments args) {
if (isNull(ioService)) {
System.err.println("Отсутствует сервис ввода-вывода. Включите настройку \"otus.custom.io.enabled\"");
return;
}
ioService.outputString("---------------------------------------------");
ioService.outputAsString(ioConfig);
ioService.outputString("---------------------------------------------");
var greetingTarget = localizationService.getMessage("greeting.target");
var greeting = localizationService.getMessage("greeting", greetingTarget);
ioService.outputString(greeting);
ioService.outputString("---------------------------------------------");
}
}
@@ -0,0 +1,9 @@
#spring:
#messages:
#basename: messages
#fallback-to-system-locale: false
#otus:
#custom:
#io-enabled: true
#locale: ru-RU
@@ -0,0 +1,9 @@
o o o o o
<|> <|> <|> <|> <|>
/ > / \ / \ / \ < \
\o__ __o o__ __o \o/ \o/ o__ __o o o o__ __o \o__ __o \o/ o__ __o/
| v\ /v |> | | /v v\ <|> <|> /v v\ | |> | /v |
/ \ <\ /> // / \ / \ /> <\ < > < > /> <\ / \ < > / \ /> / \
\o/ o/ \o o/ \o/ \o/ \ / \o o/\o o/ \ / \o/ \o/ \ \o/
| <| v\ /v __o | | o o v\ /v v\ /v o o | | o |
/ \ / \ <\/> __/> / \ / \ <\__ __/> <\/> <\/> <\__ __/> / \ / \ <\__ / \
@@ -0,0 +1,2 @@
greeting=Hello {0}!!!
greeting.target=World
@@ -0,0 +1,2 @@
greeting=Hello {0}!!!
greeting.target=World
@@ -0,0 +1,2 @@
greeting=\u041F\u0440\u0438\u0432\u0435\u0442 {0}!!!
greeting.target=\u041C\u0438\u0440
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ru.otus</groupId>
<artifactId>io-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>io-starter</name>
<description>Spring Boot starter demo</description>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.boot.version>3.5.7</spring.boot.version>
<snakeyaml.version>2.0</snakeyaml.version>
<lombok.version>1.18.28</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring.boot.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring.boot.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,46 @@
package ru.otus.io;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.otus.io.config.DefaultLocaleProvider;
import ru.otus.io.config.LocaleProvider;
import ru.otus.io.services.IOService;
import ru.otus.io.services.LocalizationService;
import ru.otus.io.services.LocalizationServiceImpl;
import ru.otus.io.services.StreamsIOService;
@ConditionalOnProperty(value = "otus.custom.io-enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnClass(MessageSource.class)
@Configuration
public class IOAutoconfiguration {
@ConditionalOnMissingBean(IOService.class)
@Bean
public IOService ioService() {
return new StreamsIOService(System.out, System.in);
}
@ConditionalOnMissingBean(LocaleProvider.class)
@Bean
public LocaleProvider localeProvider(@Value("${otus.custom.locale:en-US}") String localeTag) {
return new DefaultLocaleProvider(localeTag);
}
@ConditionalOnMissingBean(LocalizationService.class)
@ConditionalOnBean(MessageSource.class)
@Bean
public LocalizationService localizationService(LocaleProvider localeProvider,
MessageSource messageSource) {
return new LocalizationServiceImpl(localeProvider, messageSource);
}
}
@@ -0,0 +1,17 @@
package ru.otus.io.config;
import java.util.Locale;
public class DefaultLocaleProvider implements LocaleProvider {
private final Locale locale;
public DefaultLocaleProvider(String localeTag) {
this.locale = Locale.forLanguageTag(localeTag);
}
@Override
public Locale getCurrent() {
return locale;
}
}
@@ -0,0 +1,7 @@
package ru.otus.io.config;
import java.util.Locale;
public interface LocaleProvider {
Locale getCurrent();
}
@@ -0,0 +1,8 @@
package ru.otus.io.services;
public interface IOService {
void outputString(String s);
void outputAsString(Object o);
String readString();
}
@@ -0,0 +1,5 @@
package ru.otus.io.services;
public interface LocalizationService {
String getMessage(String key, Object ...args);
}
@@ -0,0 +1,20 @@
package ru.otus.io.services;
import org.springframework.context.MessageSource;
import ru.otus.io.config.LocaleProvider;
public class LocalizationServiceImpl implements LocalizationService {
private final LocaleProvider localeProvider;
private final MessageSource messageSource;
public LocalizationServiceImpl(LocaleProvider localeProvider, MessageSource messageSource) {
this.localeProvider = localeProvider;
this.messageSource = messageSource;
}
@Override
public String getMessage(String key, Object... args) {
return messageSource.getMessage(key, args, localeProvider.getCurrent());
}
}
@@ -0,0 +1,31 @@
package ru.otus.io.services;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class StreamsIOService implements IOService {
private final PrintStream ps;
private final Scanner sc;
public StreamsIOService(PrintStream ps, InputStream is) {
this.ps = ps;
this.sc = new Scanner(is);
}
@Override
public void outputString(String s) {
ps.println(s);
}
@Override
public void outputAsString(Object o) {
ps.println(o);
}
@Override
public String readString() {
return sc.nextLine();
}
}
+17
View File
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ru.otus</groupId>
<artifactId>spring-boot-intro</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>greeting-app</module>
<module>io-starter</module>
</modules>
</project>