mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
05-springBoot
This commit is contained in:
@@ -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/
|
||||
@@ -0,0 +1,68 @@
|
||||
<?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-05-springBoot</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.0</version>
|
||||
</parent>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
|
||||
<maven-enforcer-plugin.version>3.1.0</maven-enforcer-plugin.version>
|
||||
<minimal.maven.version>3.3.9</minimal.maven.version>
|
||||
</properties>
|
||||
|
||||
|
||||
<!-- Стартеры -->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<!-- собираем "жирный" jar-ник-->
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-enforcer-plugin</artifactId>
|
||||
<version>${maven-enforcer-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>enforce-maven</id>
|
||||
<goals>
|
||||
<goal>enforce</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<rules>
|
||||
<dependencyConvergence/>
|
||||
<requireMavenVersion>
|
||||
<version>${minimal.maven.version}</version>
|
||||
</requireMavenVersion>
|
||||
</rules>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<executable>true</executable>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+32
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> data;
|
||||
private final Map<String, Integer> mapa;
|
||||
|
||||
@ConstructorBinding
|
||||
public AppProps(String message, Locale locale, List<String> data, Map<String, Integer> 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<String> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public Map<String, Integer> getMapa() {
|
||||
return mapa;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package ru.otus.configs;
|
||||
|
||||
public record ApplicationMessage(String message) {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package ru.otus.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface GreetingService {
|
||||
Map<String, String> sayHello(String name);
|
||||
}
|
||||
+34
@@ -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<String, String> sayHello(String name) {
|
||||
var messageLocalized = messageSource.getMessage("hello.user", new String[]{"Ivan"}, props.getLocale());
|
||||
logger.info("Localization:{}", messageLocalized);
|
||||
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put(name, messageLocalized);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
Custom banner...
|
||||
@@ -0,0 +1 @@
|
||||
hello.user="Hello, {0}"!
|
||||
@@ -0,0 +1 @@
|
||||
hello.user="Привет, {0}"!
|
||||
@@ -0,0 +1,13 @@
|
||||
<configuration scan="true" scanPeriod="10 seconds">
|
||||
<jmxConfigurator />
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user