mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
L04 SpringBoot
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?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>application-2020-05</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<!-- Наследуем зависимости от глобального родителя -->
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.3.0.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
|
||||
<maven-enforcer-plugin.version>3.0.0-M3</maven-enforcer-plugin.version>
|
||||
<minimal.maven.version>3.3.9</minimal.maven.version>
|
||||
|
||||
<messager-starter.version>0.0.1-SNAPSHOT</messager-starter.version>
|
||||
</properties>
|
||||
|
||||
|
||||
<!-- Стартеры -->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>messager-starter-2020-05</artifactId>
|
||||
<version>${messager-starter.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- эффект стартера
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
<version>3.12.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver</artifactId>
|
||||
<version>3.12.2</version>
|
||||
</dependency>
|
||||
-->
|
||||
</dependencies>
|
||||
|
||||
<!-- собираем "жирный" jar-ник-->
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<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>
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.mainpackage;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import ru.otus.mainpackage.configs.YamlProps;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties(YamlProps.class)
|
||||
public class Demo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Demo.class, args);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.mainpackage.configs;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
|
||||
|
||||
@Configuration
|
||||
public class Localization {
|
||||
@Bean
|
||||
public MessageSource messageSource() {
|
||||
var ms = new ReloadableResourceBundleMessageSource();
|
||||
ms.setBasename("classpath:/il8n/bundle");
|
||||
ms.setDefaultEncoding("UTF-8");
|
||||
return ms;
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package ru.otus.mainpackage.configs;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@ConfigurationProperties(prefix = "application")
|
||||
public class YamlProps {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.mainpackage.welcome;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface Greeting {
|
||||
Map<String, String> sayHello(String name);
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package ru.otus.mainpackage.welcome;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.otus.mainpackage.configs.YamlProps;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class GreetingController {
|
||||
private static final Logger logger = LoggerFactory.getLogger(GreetingController.class);
|
||||
private final Greeting greeting;
|
||||
|
||||
public GreetingController(Greeting greeting, YamlProps props) {
|
||||
this.greeting = greeting;
|
||||
logger.info("ATTENTION! props.getMessage(): {}", props.getMessage());
|
||||
}
|
||||
|
||||
//http://localhost:8080/hello?name=ddd
|
||||
@GetMapping("/hello")
|
||||
public Map<String, String> sayHello(@RequestParam(name="name") String name) {
|
||||
return this.greeting.sayHello(name);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ru.otus.mainpackage.welcome;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class GreetingControllerRstyle {
|
||||
private static final Logger logger = LoggerFactory.getLogger(GreetingControllerRstyle.class);
|
||||
|
||||
private final Greeting greeting;
|
||||
|
||||
public GreetingControllerRstyle(Greeting greeting) {
|
||||
logger.info("Я НАСТОЯЩИЙ БИН!!!");
|
||||
this.greeting = greeting;
|
||||
}
|
||||
|
||||
//http://localhost:8080/hello/jone
|
||||
@GetMapping(value="/hello/{name}")
|
||||
public Map<String, String> sayHello(@PathVariable("name") String name) {
|
||||
return this.greeting.sayHello(name);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.mainpackage.welcome;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class GreetingService implements Greeting {
|
||||
|
||||
@Override
|
||||
public Map<String, String> sayHello(String name) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put(name, "Hello, " + name);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package ru.otus.mainpackage.welcome;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.mainpackage.configs.YamlProps;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@Component
|
||||
public class LocalHello {
|
||||
private static final Logger logger = LoggerFactory.getLogger(LocalHello.class);
|
||||
|
||||
private final MessageSource messageSource;
|
||||
private final YamlProps props;
|
||||
|
||||
public LocalHello(MessageSource messageSource, YamlProps props) {
|
||||
this.messageSource = messageSource;
|
||||
this.props = props;
|
||||
}
|
||||
|
||||
//!!! Вообще, PostConstruct - это плохая практика !!!
|
||||
@PostConstruct
|
||||
public void printHello() {
|
||||
var message = messageSource.getMessage("hello.user", new String[]{"Ivan"}, props.getLocale());
|
||||
logger.info(message);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package ru.otus.mainpackage.сommandlinerunners;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.Messager;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
@Component
|
||||
public class PreparationDev implements CommandLineRunner {
|
||||
private static final Logger logger = LoggerFactory.getLogger(PreparationDev.class);
|
||||
|
||||
private final Messager messager;
|
||||
|
||||
public PreparationDev(Messager messager) {
|
||||
this.messager = messager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
logger.info("DEV mode!!! Что-то настравиваем и подготавливаем, параметры: {} ", Arrays.toString(args));
|
||||
logger.info("message from Messager:{}", messager.sayMessage());
|
||||
//args парметры, котрые могут быть переданы в Main
|
||||
}
|
||||
}
|
||||
+17
@@ -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("Это не увидим никогда");
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
server:
|
||||
port: 8080
|
||||
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: info, health, beans
|
||||
|
||||
application:
|
||||
message: "Test Props msg"
|
||||
locale: ru_RU
|
||||
|
||||
messager:
|
||||
message: "Hello from Messager"
|
||||
+1
@@ -0,0 +1 @@
|
||||
hello.user="Hello, {0}"!
|
||||
+1
@@ -0,0 +1 @@
|
||||
hello.user="Привет, {0}"!
|
||||
+13
@@ -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>
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package ru.otus.mainpackage.welcome;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
|
||||
|
||||
@WebMvcTest({GreetingController.class, GreetingControllerRstyle.class})
|
||||
public class GreetingControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private Greeting greeting;
|
||||
|
||||
@Test
|
||||
public void sayHelloTest() throws Exception {
|
||||
String name = "Mr.Test";
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put(name, "Hello, " + name);
|
||||
String expectedResult = "{\"Mr.Test\":\"Hello, Mr.Test\"}";
|
||||
|
||||
when(greeting.sayHello(name)).thenReturn(map);
|
||||
MockHttpServletResponse result = mockMvc.perform(get(String.format("/hello?name=%s", name)))
|
||||
.andReturn()
|
||||
.getResponse();
|
||||
|
||||
verify(greeting, times(1)).sayHello(name);
|
||||
System.out.println();
|
||||
assertThat(result.getContentAsString()).isEqualTo(expectedResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sayHelloTestMock() throws Exception {
|
||||
String name = "Mr.Test";
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put(name, "Hello, " + name);
|
||||
String expectedResult = "{\"Mr.Test\":\"Hello, Mr.Test\"}";
|
||||
|
||||
when(greeting.sayHello(name)).thenReturn(map);
|
||||
MockHttpServletResponse result = mockMvc.perform(get(String.format("/hello/%s", name)))
|
||||
.andReturn()
|
||||
.getResponse();
|
||||
|
||||
verify(greeting, times(1)).sayHello(name);
|
||||
System.out.println();
|
||||
assertThat(result.getContentAsString()).isEqualTo(expectedResult);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?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>messager-starter-2020-05</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
|
||||
<maven-enforcer-plugin.version>3.0.0-M3</maven-enforcer-plugin.version>
|
||||
<minimal.maven.version>3.3.9</minimal.maven.version>
|
||||
|
||||
<maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>
|
||||
|
||||
<spring-boot-starter.version>2.3.0.RELEASE</spring-boot-starter.version>
|
||||
<messager.version>0.0.1-SNAPSHOT</messager.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jul-to-slf4j</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>messager-2020-05</artifactId>
|
||||
<version>${messager.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<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.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${maven-shade-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package ru.otus;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(Messager.class)
|
||||
@EnableConfigurationProperties(Props.class)
|
||||
public class MessagerAutoConfiguration {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MessagerAutoConfiguration.class);
|
||||
|
||||
private final Props props;
|
||||
|
||||
public MessagerAutoConfiguration(Props props) {
|
||||
this.props = props;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MessagerConfig messagerConfig() {
|
||||
String message = props.getMessage() == null ? "default message" : props.getMessage();
|
||||
logger.info("AutoConfig. creating MessagerConfig, default message:{}", message);
|
||||
return new MessagerConfig(message);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public Messager messager(MessagerConfig messagerConfig) {
|
||||
logger.info("AutoConfig. creating Messager");
|
||||
return new Messager(messagerConfig);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties(prefix="messager")
|
||||
public class Props {
|
||||
|
||||
private String message;
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
ru.otus.MessagerAutoConfiguration
|
||||
@@ -0,0 +1,51 @@
|
||||
<?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>messager-2020-05</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
|
||||
<maven-enforcer-plugin.version>3.0.0-M3</maven-enforcer-plugin.version>
|
||||
<minimal.maven.version>3.3.9</minimal.maven.version>
|
||||
|
||||
<maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<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.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${maven-shade-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus;
|
||||
|
||||
public class Messager {
|
||||
private final MessagerConfig config;
|
||||
|
||||
public Messager(MessagerConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public String sayMessage() {
|
||||
return config.getMessage();
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus;
|
||||
|
||||
public class MessagerConfig {
|
||||
private final String message;
|
||||
|
||||
public MessagerConfig(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?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>springBoot-hello</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<!-- Наследуем зависимости от глобального родителя -->
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.3.0.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<!-- нужные зависимости -->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<!-- собираем "жирный" jar-ник-->
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package ru.otus;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class HelloWorldDemo {
|
||||
public static void main(String[] args) {
|
||||
var context = SpringApplication.run(HelloWorldDemo.class, args);
|
||||
var welcome = context.getBean(Welcome.class);
|
||||
welcome.sayHello();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Welcome speaker() {
|
||||
return () -> System.out.println("Hello, World");
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package ru.otus;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Welcome {
|
||||
void sayHello();
|
||||
}
|
||||
Reference in New Issue
Block a user