mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2020-02 spring 05 classwork has been added
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,2 @@
|
||||
# application-events-demo
|
||||
Пример работы с событиями
|
||||
@@ -0,0 +1,85 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.7.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>application-events-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>application-events-demo</name>
|
||||
<description>Application events demo</description>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<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>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.shell</groupId>
|
||||
<artifactId>spring-shell-starter</artifactId>
|
||||
<version>2.0.1.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.applicationeventsdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.context.event.ApplicationEventMulticaster;
|
||||
import org.springframework.context.event.SimpleApplicationEventMulticaster;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ApplicationEventsDemoApplication {
|
||||
|
||||
//@Bean @Primary
|
||||
//@Bean(name = "applicationEventMulticaster")
|
||||
public ApplicationEventMulticaster applicationEventMulticaster() {
|
||||
SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
|
||||
eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
|
||||
return eventMulticaster;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ApplicationEventsDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
public interface EventsPublisher {
|
||||
void publish();
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
public class HalfAGlassOfWaterEvent extends ApplicationEvent {
|
||||
|
||||
@Getter
|
||||
private final String payload;
|
||||
|
||||
public HalfAGlassOfWaterEvent(Object source) {
|
||||
super(source);
|
||||
payload = "Осталось половина стакана воды!!!";
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class HalfAGlassOfWaterEventPublisher implements EventsPublisher {
|
||||
|
||||
private final ApplicationEventPublisher publisher;
|
||||
|
||||
@Override
|
||||
public void publish() {
|
||||
publisher.publishEvent(new HalfAGlassOfWaterEvent(this));
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class NegativeRespondent implements ApplicationListener<HalfAGlassOfWaterEvent> {
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void onApplicationEvent(HalfAGlassOfWaterEvent halfAGlassOfWaterEvent) {
|
||||
Thread.sleep(100);
|
||||
System.out.println("Негативно настроенный слушатель");
|
||||
System.out.println(String.format("- %s", halfAGlassOfWaterEvent.getPayload()));
|
||||
System.out.println("- Какой ужас. Теперь он наполовину пуст!!!\n\n");
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PositiveRespondent implements ApplicationListener<HalfAGlassOfWaterEvent> {
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void onApplicationEvent(HalfAGlassOfWaterEvent halfAGlassOfWaterEvent) {
|
||||
Thread.sleep(100);
|
||||
System.out.println("Позитивно настроенный слушатель");
|
||||
System.out.println(String.format("- %s", halfAGlassOfWaterEvent.getPayload()));
|
||||
System.out.println("- Ничего. Главное, что он наполовину полон!!!\n\n");
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package ru.otus.example.applicationeventsdemo.shell;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.shell.Availability;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
import org.springframework.shell.standard.ShellMethod;
|
||||
import org.springframework.shell.standard.ShellMethodAvailability;
|
||||
import org.springframework.shell.standard.ShellOption;
|
||||
import ru.otus.example.applicationeventsdemo.events.EventsPublisher;
|
||||
|
||||
@ShellComponent
|
||||
@RequiredArgsConstructor
|
||||
public class ApplicationEventsCommands {
|
||||
|
||||
private final EventsPublisher eventsPublisher;
|
||||
|
||||
private String userName;
|
||||
|
||||
@ShellMethod(value = "Login command", key = {"l", "login"})
|
||||
public String login(@ShellOption(defaultValue = "stvort") String userName) {
|
||||
this.userName = userName;
|
||||
return String.format("Добро пожаловать: %s", userName);
|
||||
}
|
||||
|
||||
@ShellMethod(value = "Publish event command", key = {"p", "pub", "publish"})
|
||||
@ShellMethodAvailability(value = "isPublishEventCommandAvailable")
|
||||
public String publishEvent() {
|
||||
eventsPublisher.publish();
|
||||
return "Событие опубликовано";
|
||||
}
|
||||
|
||||
private Availability isPublishEventCommandAvailable() {
|
||||
return userName == null? Availability.unavailable("Сначала залогиньтесь"): Availability.available();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package ru.otus.example.applicationeventsdemo.shell;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.shell.Availability;
|
||||
import org.springframework.shell.CommandNotCurrentlyAvailable;
|
||||
import org.springframework.shell.Shell;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import ru.otus.example.applicationeventsdemo.events.EventsPublisher;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@DisplayName("Тест команд shell ")
|
||||
@SpringBootTest
|
||||
class ApplicationEventsCommandsTest {
|
||||
|
||||
@MockBean
|
||||
private EventsPublisher eventsPublisher;
|
||||
|
||||
@Autowired
|
||||
private Shell shell;
|
||||
|
||||
private static final String GREETING_PATTERN = "Добро пожаловать: %s";
|
||||
private static final String DEFAULT_LOGIN = "stvort";
|
||||
private static final String CUSTOM_LOGIN = "Вася";
|
||||
private static final String COMMAND_LOGIN = "login";
|
||||
private static final String COMMAND_LOGIN_SHORT = "l";
|
||||
private static final String COMMAND_PUBLISH = "publish";
|
||||
private static final String COMMAND_PUBLISH_EXPECTED_RESULT = "Событие опубликовано";
|
||||
private static final String COMMAND_LOGIN_PATTERN = "%s %s";
|
||||
|
||||
@DisplayName(" должен возвращать приветствие для всех форм команды логина")
|
||||
@Test
|
||||
void shouldReturnExpectedGreetingAfterLoginCommandEvaluated() {
|
||||
String res = (String) shell.evaluate(() -> COMMAND_LOGIN);
|
||||
assertThat(res).isEqualTo(String.format(GREETING_PATTERN, DEFAULT_LOGIN));
|
||||
|
||||
res = (String) shell.evaluate(() -> COMMAND_LOGIN_SHORT);
|
||||
assertThat(res).isEqualTo(String.format(GREETING_PATTERN, DEFAULT_LOGIN));
|
||||
|
||||
res = (String) shell.evaluate(() -> String.format(COMMAND_LOGIN_PATTERN, COMMAND_LOGIN_SHORT, CUSTOM_LOGIN));
|
||||
assertThat(res).isEqualTo(String.format(GREETING_PATTERN, CUSTOM_LOGIN));
|
||||
}
|
||||
|
||||
@DisplayName(" должен возвращать CommandNotCurrentlyAvailable если при попытке выполнения команды publish пользователь выполнил вход")
|
||||
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
|
||||
@Test
|
||||
void shouldReturnCommandNotCurrentlyAvailableObjectWhenUserDoesNotLoginAfterPublishCommandEvaluated() {
|
||||
Object res = shell.evaluate(() -> COMMAND_PUBLISH);
|
||||
assertThat(res).isInstanceOf(CommandNotCurrentlyAvailable.class);
|
||||
}
|
||||
|
||||
@DisplayName(" должен возвращать статус выполнения команды publish и вызвать соответствующий метод сервиса есл икоманда выполнена после входа")
|
||||
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
|
||||
@Test
|
||||
void shouldReturnExpectedMessageAndFirePublishMethodAfterPublishCommandEvaluated() {
|
||||
shell.evaluate(() -> COMMAND_LOGIN);
|
||||
String res = (String) shell.evaluate(() -> COMMAND_PUBLISH);
|
||||
assertThat(res).isEqualTo(COMMAND_PUBLISH_EXPECTED_RESULT);
|
||||
verify(eventsPublisher, times(1)).publish();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
spring:
|
||||
shell:
|
||||
interactive:
|
||||
enabled: false
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,19 @@
|
||||
#### Упражнение №2
|
||||
- Изучить классы ничего в них не меняя:
|
||||
- CustomBeanFactoryPostProcessor
|
||||
- CustomBeanPostProcessor
|
||||
- CustomLifeCycleBean
|
||||
- Запуститить приложение
|
||||
- Изучить распечатанный жизненный цикл
|
||||
|
||||
|
||||
#### Упражнение №3
|
||||
- В application.yml выставить spring.shell.interactive.enabled в true
|
||||
- Запуститить приложение командой "cfn" или "call-favorite-number"
|
||||
- Запомнить результат
|
||||
- В CustomBeanFactoryPostProcessor раскомментировать блок кода
|
||||
- Запуститить приложение командой "cfn" или "call-favorite-number"
|
||||
- Что изменилось?
|
||||
- В CustomBeanPostProcessor раскомментировать строку
|
||||
- Запуститить приложение командой "cfn" или "call-favorite-number"
|
||||
- Что изменилось теперь? Круто, да?
|
||||
@@ -0,0 +1,48 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>beans-lifecycle-exercise</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<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>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.shell</groupId>
|
||||
<artifactId>spring-shell-starter</artifactId>
|
||||
<version>2.0.1.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.example.beanslifecycledemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BeansLifecycleDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BeansLifecycleDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package ru.otus.example.beanslifecycledemo.domain;
|
||||
|
||||
public class FriendPhoneNumber extends PhoneNumber {
|
||||
@Override
|
||||
public String getOwnerName() {
|
||||
return "Друг";
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ru.otus.example.beanslifecycledemo.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class GirlfiendPhoneNumber extends PhoneNumber {
|
||||
@Override
|
||||
public String getOwnerName() {
|
||||
return "Подруга";
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.example.beanslifecycledemo.domain;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class Phone {
|
||||
private String greeting = "Погнали к родителям";
|
||||
|
||||
private final PhoneNumber favoriteNumber;
|
||||
|
||||
public void callFavoriteNumber() {
|
||||
System.out.println(favoriteNumber.getOwnerName() + " " + greeting);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.example.beanslifecycledemo.domain;
|
||||
|
||||
public abstract class PhoneNumber {
|
||||
public String getOwnerName() {
|
||||
return "Спорт-лото";
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package ru.otus.example.beanslifecycledemo.lifecycle;
|
||||
|
||||
import org.springframework.beans.BeanMetadataAttribute;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
|
||||
import ru.otus.example.beanslifecycledemo.domain.FriendPhoneNumber;
|
||||
import ru.otus.example.beanslifecycledemo.domain.GirlfiendPhoneNumber;
|
||||
|
||||
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
|
||||
if (beanFactory.containsBean("customLifeCycleBean")) {
|
||||
System.out.println("Шаг #1: BeanFactoryPostProcessor.postProcessBeanFactory\n");
|
||||
}
|
||||
|
||||
/*
|
||||
for (String beanName : beanFactory.getBeanDefinitionNames()) {
|
||||
BeanDefinition d = beanFactory.getBeanDefinition(beanName);
|
||||
|
||||
if (GirlfiendPhoneNumber.class.getName().equalsIgnoreCase(d.getBeanClassName())) {
|
||||
d.setBeanClassName(FriendPhoneNumber.class.getName());
|
||||
((ScannedGenericBeanDefinition) d).addMetadataAttribute(new BeanMetadataAttribute("className", FriendPhoneNumber.class.getName()));
|
||||
d.setAutowireCandidate(true);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package ru.otus.example.beanslifecycledemo.lifecycle;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import ru.otus.example.beanslifecycledemo.domain.Phone;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class CustomBeanPostProcessor implements BeanPostProcessor {
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (bean.getClass().equals(CustomLifeCycleBean.class)) {
|
||||
System.out.println("Шаг #5: BeanPostProcessor.postProcessBeforeInitialization\n");
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (bean.getClass().equals(CustomLifeCycleBean.class)) {
|
||||
System.out.println("Шаг #9: BeanPostProcessor.postProcessAfterInitialization\n");
|
||||
}
|
||||
|
||||
if (bean.getClass().isAssignableFrom(Phone.class)) {
|
||||
//updateGreeting(bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
private void updateGreeting(Object bean) {
|
||||
Class aClass = Phone.class;
|
||||
Field greetingField = aClass.getDeclaredField("greeting");
|
||||
|
||||
greetingField.setAccessible(true);
|
||||
greetingField.setAccessible(true);
|
||||
greetingField.set(bean, "Ай-да в гараж. Стихи читать!");
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package ru.otus.example.beanslifecycledemo.lifecycle;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.*;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
public class CustomLifeCycleBean implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware {
|
||||
|
||||
@Override
|
||||
public void setBeanName(String s) {
|
||||
System.out.println("Шаг #2: BeanNameAware\n");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
System.out.println("Шаг #3: BeanFactoryAware\n");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
System.out.println("Шаг #4: ApplicationContextAware\n");
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
System.out.println("Шаг #6: @PostConstruct\n");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
System.out.println("Шаг #7: InitializingBean.afterPropertiesSet\n");
|
||||
}
|
||||
|
||||
public void customInitMethod() {
|
||||
System.out.println("Шаг #8: CustomLifeCycleBean.customInitMethod\n");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void preDestroy() {
|
||||
System.out.println("Шаг #10: @PreDestroy\n");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
System.out.println("Шаг #11: DisposableBean.destroy\n");
|
||||
}
|
||||
|
||||
public void customDestroyMethod() {
|
||||
System.out.println("Шаг #12: CustomLifeCycleBean.customDestroyMethod\n");
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ru.otus.example.beanslifecycledemo.lifecycle;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class LifeCycleConfig {
|
||||
|
||||
@Bean
|
||||
public BeanFactoryPostProcessor customBeanFactoryPostProcessor() {
|
||||
return new CustomBeanFactoryPostProcessor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BeanPostProcessor customBeanPostProcessor() {
|
||||
return new CustomBeanPostProcessor();
|
||||
}
|
||||
|
||||
@ConditionalOnProperty(name = "spring.shell.interactive.enabled", havingValue = "false")
|
||||
@Bean(initMethod = "customInitMethod", destroyMethod = "customDestroyMethod")
|
||||
public CustomLifeCycleBean customLifeCycleBean() {
|
||||
return new CustomLifeCycleBean();
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package ru.otus.example.beanslifecycledemo.shell;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
import org.springframework.shell.standard.ShellMethod;
|
||||
import ru.otus.example.beanslifecycledemo.domain.Phone;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@ShellComponent
|
||||
public class LifecycleDemoCommands {
|
||||
|
||||
private final Phone phone;
|
||||
|
||||
@ShellMethod(value = "Call favorite number", key = {"call-favorite-number", "cfn"})
|
||||
public void callFavoriteNumber() {
|
||||
phone.callFavoriteNumber();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
|
||||
spring:
|
||||
shell:
|
||||
interactive:
|
||||
enabled: false
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,9 @@
|
||||
#### Упражнение №1
|
||||
- Расставить скоупы над сервисами так, чтобы приложение запустилось и заработало, как указано на странице http://localhost:8080
|
||||
- Подсказки в названии сервисов
|
||||
- Не забываем про proxyMode для некоторых скоупов
|
||||
|
||||
```
|
||||
Задать скоуп можно с помощью аннотации @Scope
|
||||
Возможные значения: singleton, prototype, session, request
|
||||
```
|
||||
@@ -0,0 +1,48 @@
|
||||
<?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>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>beans-scopes-exercise</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<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-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.example.beansscopesdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BeansScopesDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BeansScopesDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package ru.otus.example.beansscopesdemo.controllers;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import ru.otus.example.beansscopesdemo.services.GreetingService;
|
||||
|
||||
@Controller
|
||||
public class GreetingController {
|
||||
private final GreetingService singletonGreetingService;
|
||||
private final GreetingService prototypeGreetingService1;
|
||||
private final GreetingService prototypeGreetingService2;
|
||||
private final GreetingService sessionGreetingService;
|
||||
private final GreetingService requestGreetingService;
|
||||
|
||||
public GreetingController(@Qualifier("SingletonGreetingService") GreetingService singletonGreetingService,
|
||||
@Qualifier("PrototypeGreetingService")GreetingService prototypeGreetingService1,
|
||||
@Qualifier("PrototypeGreetingService")GreetingService prototypeGreetingService2,
|
||||
@Qualifier("SessionGreetingService")GreetingService sessionGreetingService,
|
||||
@Qualifier("RequestGreetingService")GreetingService requestGreetingService
|
||||
) {
|
||||
this.singletonGreetingService = singletonGreetingService;
|
||||
this.prototypeGreetingService1 = prototypeGreetingService1;
|
||||
this.prototypeGreetingService2 = prototypeGreetingService2;
|
||||
this.sessionGreetingService = sessionGreetingService;
|
||||
this.requestGreetingService = requestGreetingService;
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String greetingPage(Model model) {
|
||||
boolean isFirstGreetingSuccess = prototypeGreetingService1.isFirstGreetingSuccess();
|
||||
model.addAttribute("singletonGreeting", singletonGreetingService.greeting());
|
||||
model.addAttribute("sessionGreeting", sessionGreetingService.greeting());
|
||||
model.addAttribute("requestGreeting", requestGreetingService.greeting());
|
||||
model.addAttribute("prototype1Greeting", prototypeGreetingService1.greeting());
|
||||
model.addAttribute("prototype2Greeting", isFirstGreetingSuccess? prototypeGreetingService2.greeting(): "Пока жду");
|
||||
return "index";
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
public abstract class AbstractGreetingServiceImpl implements GreetingService {
|
||||
|
||||
@Value("${greetings.first-greeting}")
|
||||
private String firstGreeting;
|
||||
|
||||
@Value("${greetings.re-greeting}")
|
||||
private String reGreeting;
|
||||
|
||||
private boolean isFirstGreetingSuccess;
|
||||
|
||||
public AbstractGreetingServiceImpl() {
|
||||
this.isFirstGreetingSuccess = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFirstGreetingSuccess() {
|
||||
return isFirstGreetingSuccess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String greeting() {
|
||||
return currentGreeting();
|
||||
}
|
||||
|
||||
private synchronized String currentGreeting() {
|
||||
String greeting = isFirstGreetingSuccess ? reGreeting : firstGreeting;
|
||||
isFirstGreetingSuccess = true;
|
||||
return greeting;
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
public interface GreetingService {
|
||||
boolean isFirstGreetingSuccess();
|
||||
String greeting();
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("PrototypeGreetingService")
|
||||
public class PrototypeGreetingServiceImpl extends AbstractGreetingServiceImpl {
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("RequestGreetingService")
|
||||
public class RequestGreetingServiceImpl extends AbstractGreetingServiceImpl {
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("SessionGreetingService")
|
||||
public class SessionGreetingServiceImpl extends AbstractGreetingServiceImpl {
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("SingletonGreetingService")
|
||||
public class SingletonGreetingServiceImpl extends AbstractGreetingServiceImpl {
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
greetings:
|
||||
first-greeting: Привет
|
||||
re-greeting: Рад снова тебя видеть
|
||||
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Advanced configuration demo</title>
|
||||
<style>
|
||||
body {
|
||||
font-size: 16pt;
|
||||
}
|
||||
|
||||
h3 {
|
||||
color: CornflowerBlue;
|
||||
}
|
||||
|
||||
li {
|
||||
color: DarkGray;
|
||||
}
|
||||
|
||||
p {
|
||||
color: OrangeRed;
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
<h3>Демонстрация @Scope</h3>
|
||||
<ul>
|
||||
<li>SingletonGreetingService должен говорить сначала "Привет", а потом все время "Рад снова тебя видеть"</li>
|
||||
<li>PrototypeGreetingService1 должен говорить сначала "Привет", а потом все время "Рад снова тебя видеть"</li>
|
||||
<li>PrototypeGreetingService2 должен говорить сначала "Пока жду", после обновления страницы "Привет", а потом все время "Рад снова тебя видеть"</li>
|
||||
<li>SessionGreetingService должен вести себя как SingletonGreetingService, но только до перезапуска браузера</li>
|
||||
<li>RequestGreetingService должен каждый раз говорить "Привет"</li>
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
<hr/>
|
||||
|
||||
<p th:text = "'SingletonGreetingService сказал: ' + ${singletonGreeting}"></p>
|
||||
<p th:text = "'PrototypeGreetingService1 сказал: ' + ${prototype1Greeting}"></p>
|
||||
<p th:text = "'PrototypeGreetingService2 сказал: ' + ${prototype2Greeting}"></p>
|
||||
<p th:text = "'SessionGreetingService сказал: ' + ${sessionGreeting}"></p>
|
||||
<p th:text = "'RequestGreetingService сказал: ' + ${requestGreeting}"></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,15 @@
|
||||
#### Решение к упражнению №1
|
||||
|
||||
@Scope("singleton")
|
||||
SingletonGreetingServiceImpl
|
||||
|
||||
@Scope("prototype")
|
||||
PrototypeGreetingServiceImpl
|
||||
|
||||
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
|
||||
RequestGreetingServiceImpl
|
||||
|
||||
@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
|
||||
SessionGreetingServiceImpl
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?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>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>beans-scopes-solution</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<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-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.example.beansscopesdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BeansScopesDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BeansScopesDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package ru.otus.example.beansscopesdemo.controllers;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import ru.otus.example.beansscopesdemo.services.GreetingService;
|
||||
|
||||
@Controller
|
||||
public class GreetingController {
|
||||
private final GreetingService singletonGreetingService;
|
||||
private final GreetingService prototypeGreetingService1;
|
||||
private final GreetingService prototypeGreetingService2;
|
||||
private final GreetingService sessionGreetingService;
|
||||
private final GreetingService requestGreetingService;
|
||||
|
||||
public GreetingController(@Qualifier("SingletonGreetingService") GreetingService singletonGreetingService,
|
||||
@Qualifier("PrototypeGreetingService")GreetingService prototypeGreetingService1,
|
||||
@Qualifier("PrototypeGreetingService")GreetingService prototypeGreetingService2,
|
||||
@Qualifier("SessionGreetingService")GreetingService sessionGreetingService,
|
||||
@Qualifier("RequestGreetingService")GreetingService requestGreetingService
|
||||
) {
|
||||
this.singletonGreetingService = singletonGreetingService;
|
||||
this.prototypeGreetingService1 = prototypeGreetingService1;
|
||||
this.prototypeGreetingService2 = prototypeGreetingService2;
|
||||
this.sessionGreetingService = sessionGreetingService;
|
||||
this.requestGreetingService = requestGreetingService;
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String greetingPage(Model model) {
|
||||
boolean isFirstGreetingSuccess = prototypeGreetingService1.isFirstGreetingSuccess();
|
||||
model.addAttribute("singletonGreeting", singletonGreetingService.greeting());
|
||||
model.addAttribute("sessionGreeting", sessionGreetingService.greeting());
|
||||
model.addAttribute("requestGreeting", requestGreetingService.greeting());
|
||||
model.addAttribute("prototype1Greeting", prototypeGreetingService1.greeting());
|
||||
model.addAttribute("prototype2Greeting", isFirstGreetingSuccess? prototypeGreetingService2.greeting(): "Пока жду");
|
||||
return "index";
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
public abstract class AbstractGreetingServiceImpl implements GreetingService {
|
||||
|
||||
@Value("${greetings.first-greeting}")
|
||||
private String firstGreeting;
|
||||
|
||||
@Value("${greetings.re-greeting}")
|
||||
private String reGreeting;
|
||||
|
||||
private boolean isFirstGreetingSuccess;
|
||||
|
||||
public AbstractGreetingServiceImpl() {
|
||||
this.isFirstGreetingSuccess = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFirstGreetingSuccess() {
|
||||
return isFirstGreetingSuccess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String greeting() {
|
||||
return currentGreeting();
|
||||
}
|
||||
|
||||
private synchronized String currentGreeting() {
|
||||
String greeting = isFirstGreetingSuccess ? reGreeting : firstGreeting;
|
||||
isFirstGreetingSuccess = true;
|
||||
return greeting;
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
public interface GreetingService {
|
||||
boolean isFirstGreetingSuccess();
|
||||
String greeting();
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Scope("prototype")
|
||||
@Service("PrototypeGreetingService")
|
||||
public class PrototypeGreetingServiceImpl extends AbstractGreetingServiceImpl {
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.context.annotation.ScopedProxyMode;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
|
||||
@Service("RequestGreetingService")
|
||||
public class RequestGreetingServiceImpl extends AbstractGreetingServiceImpl {
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.context.annotation.ScopedProxyMode;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
|
||||
@Service("SessionGreetingService")
|
||||
public class SessionGreetingServiceImpl extends AbstractGreetingServiceImpl {
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Scope("singleton")
|
||||
@Service("SingletonGreetingService")
|
||||
public class SingletonGreetingServiceImpl extends AbstractGreetingServiceImpl {
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
greetings:
|
||||
first-greeting: Привет
|
||||
re-greeting: Рад снова тебя видеть
|
||||
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Advanced configuration demo</title>
|
||||
<style>
|
||||
body {
|
||||
font-size: 16pt;
|
||||
}
|
||||
|
||||
h3 {
|
||||
color: CornflowerBlue;
|
||||
}
|
||||
|
||||
li {
|
||||
color: DarkGray;
|
||||
}
|
||||
|
||||
p {
|
||||
color: OrangeRed;
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
<h3>Демонстрация @Scope</h3>
|
||||
<ul>
|
||||
<li>SingletonGreetingService должен говорить сначала "Привет", а потом все время "Рад снова тебя видеть"</li>
|
||||
<li>PrototypeGreetingService1 должен говорить сначала "Привет", а потом все время "Рад снова тебя видеть"</li>
|
||||
<li>PrototypeGreetingService2 должен говорить сначала "Пока жду", после обновления страницы "Привет", а потом все время "Рад снова тебя видеть"</li>
|
||||
<li>SessionGreetingService должен вести себя как SingletonGreetingService, но только до перезапуска браузера</li>
|
||||
<li>RequestGreetingService должен каждый раз говорить "Привет"</li>
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
<hr/>
|
||||
|
||||
<p th:text = "'SingletonGreetingService сказал: ' + ${singletonGreeting}"></p>
|
||||
<p th:text = "'PrototypeGreetingService1 сказал: ' + ${prototype1Greeting}"></p>
|
||||
<p th:text = "'PrototypeGreetingService2 сказал: ' + ${prototype2Greeting}"></p>
|
||||
<p th:text = "'SessionGreetingService сказал: ' + ${sessionGreeting}"></p>
|
||||
<p th:text = "'RequestGreetingService сказал: ' + ${requestGreeting}"></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,11 @@
|
||||
#### Упражнение №4
|
||||
Условия вечеринки:
|
||||
- Алексей придет если condition.alexey-exists=true
|
||||
- Анна придет если будет Алексей
|
||||
- Олег придет если включен профиль Oleg
|
||||
- Петр придет если включен профиль Peter
|
||||
- Янис придет если condition.yanis-exists=true
|
||||
- Яна придет если нет Алексея, но есть Янис
|
||||
|
||||
Измените только одну настройку в application.yml, чтобы на вечеринку пришло максимальное количество людей
|
||||
Напечатать имена пришедших на вечеринку можно с помощью команды "ppm" или "print-party-members"
|
||||
@@ -0,0 +1,81 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>conditional-and-profiles-exercise</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.shell</groupId>
|
||||
<artifactId>spring-shell-starter</artifactId>
|
||||
<version>2.0.1.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ConditionalAndProfilesDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConditionalAndProfilesDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
@ConditionalOnProperty(name = "condition.alexey-exists", havingValue = "true")
|
||||
@Component
|
||||
public class Alexey extends Friend {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Алексей";
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
@ConditionalOnBean(Alexey.class)
|
||||
@Component
|
||||
public class Anna extends Friend {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Аня";
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
@Profile("Oleg")
|
||||
@Component
|
||||
public class Oleg extends Friend {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Олег";
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
@Profile("Peter")
|
||||
@Component
|
||||
public class Peter extends Friend {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Петр";
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.conditions.YanaConditions;
|
||||
|
||||
|
||||
@Conditional(YanaConditions.class)
|
||||
@Component
|
||||
public class Yana extends Friend {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Яна";
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
@ConditionalOnProperty(name = "condition.yanis-exists", havingValue = "true")
|
||||
@Component
|
||||
public class Yanis extends Friend {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Янис";
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model.base;
|
||||
|
||||
public abstract class Friend {
|
||||
public abstract String getName();
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model.conditions;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
|
||||
public class YanaConditions extends AllNestedConditions {
|
||||
|
||||
public YanaConditions() {
|
||||
super(ConfigurationPhase.PARSE_CONFIGURATION);
|
||||
}
|
||||
|
||||
|
||||
@ConditionalOnProperty(name = "condition.alexey-exists", havingValue = "false")
|
||||
static class AlexeyDoesNotExistsCondition {
|
||||
}
|
||||
|
||||
@ConditionalOnProperty(name = "condition.yanis-exists", havingValue = "true")
|
||||
static class YanisExistsCondition {
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.shell;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
import org.springframework.shell.standard.ShellMethod;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ShellComponent
|
||||
@RequiredArgsConstructor
|
||||
public class ConditionalAndProfilesDemoCommands {
|
||||
|
||||
private final List<Friend> partyMembers;
|
||||
|
||||
@ShellMethod(value = "Print party members", key = {"print-party-members", "ppm"})
|
||||
public String printPartyMembers() {
|
||||
return partyMembers.stream().map(Friend::getName).collect(Collectors.joining("\n"));
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
condition:
|
||||
#yanis-exists: true
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
condition:
|
||||
alexey-exists: true
|
||||
yanis-exists: false
|
||||
|
||||
spring:
|
||||
profiles:
|
||||
#Доступные профили: Oleg, Peter
|
||||
active:
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: Peter
|
||||
|
||||
condition:
|
||||
yanis-exists: true
|
||||
@@ -0,0 +1,26 @@
|
||||
<?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>advanced-config-class-work</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>application-events-demo</module>
|
||||
<module>beans-lifecycle-exercise</module>
|
||||
<module>beans-scopes-exercise</module>
|
||||
<module>beans-scopes-solution</module>
|
||||
<module>conditional-and-profiles-exercise</module>
|
||||
<module>test-configuration-exercise-1</module>
|
||||
<module>test-configuration-exercise-2</module>
|
||||
<module>test-configuration-exercise-3</module>
|
||||
<module>test-configuration-solution-1</module>
|
||||
<module>test-configuration-solution-2</module>
|
||||
<module>test-configuration-solution-3</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,9 @@
|
||||
#### Упражнение №5. Должен остаться только один
|
||||
|
||||
С помощью вложенных конфигураций сделать так,
|
||||
чтобы прошел NestedConfigurationDemoTest
|
||||
(family должно содержать только собаку)
|
||||
|
||||
- Сделать вложенный класс конфигурации. Не забываем про static
|
||||
- Выбрать тип конфигурации (@Configuration/@TestConfiguration)
|
||||
- С помощью @Bean/@ComponentScan добавить в контекст нужный бин(ы)
|
||||
@@ -0,0 +1,74 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-exercise-1</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan("ru.otus.example.testconfigurationdemo.family")
|
||||
@SpringBootApplication
|
||||
public class TestConfigurationDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfigurationDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.testconfigurationdemo.family;
|
||||
|
||||
public abstract class FamilyMember {
|
||||
public abstract String getName();
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.childrens;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Son extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Сын";
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
public class Father extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Папа";
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Mother extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Мама";
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.pets;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Dog extends FamilyMember {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Собака";
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В NestedConfigurationDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class NestedConfigurationDemoTest {
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать только собаку ")
|
||||
@Test
|
||||
void shouldContainOnlyDog() {
|
||||
assertThat(family).containsOnlyKeys("dog");
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В PlainSpringBootTestDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class PlainSpringBootTestDemoTest {
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать маму, сына и собаку ")
|
||||
@Test
|
||||
void shouldContainAllFamilyExceptFather() {
|
||||
assertThat(family).containsOnlyKeys("mother", "son", "dog");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,9 @@
|
||||
#### Упражнение №6. Вернуть отца в семью
|
||||
|
||||
С помощью вложенных конфигураций сделать так,
|
||||
чтобы прошел NestedTestConfigurationDemoTest
|
||||
(family должно содержать маму, папу, сына и собаку)
|
||||
|
||||
- Сделать вложенный класс конфигурации. Не забываем про static
|
||||
- Выбрать тип конфигурации (@Configuration/@TestConfiguration)
|
||||
- С помощью @Bean/@ComponentScan добавить в контекст нужный бин(ы)
|
||||
@@ -0,0 +1,74 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-exercise-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan("ru.otus.example.testconfigurationdemo.family")
|
||||
@SpringBootApplication
|
||||
public class TestConfigurationDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfigurationDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.testconfigurationdemo.family;
|
||||
|
||||
public abstract class FamilyMember {
|
||||
public abstract String getName();
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.childrens;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Son extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Сын";
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
public class Father extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Папа";
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Mother extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Мама";
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.pets;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Dog extends FamilyMember {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Собака";
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В NestedTestConfigurationDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class NestedTestConfigurationDemoTest {
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать маму, папу, сына и собаку ")
|
||||
@Test
|
||||
void shouldContainAllFamilyWithFather() {
|
||||
assertThat(family).containsOnlyKeys("mother", "father", "son", "dog");
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В PlainSpringBootTestDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class PlainSpringBootTestDemoTest {
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать маму, сына и собаку ")
|
||||
@Test
|
||||
void shouldContainAllFamilyExceptFather() {
|
||||
assertThat(family).containsOnlyKeys("mother", "son", "dog");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,8 @@
|
||||
#### Упражнение №7. Вся семья, но без собаки
|
||||
|
||||
С помощью внешней конфигурации сделать так,
|
||||
чтобы прошел SpringBootTestWithExternalLimitationDemoTest
|
||||
(family должно содержать маму, папу и сына)
|
||||
|
||||
- Над TestSpringBootConfiguration повесить @SpringBootConfiguration
|
||||
- С помощью @Bean/@ComponentScan добавить в контекст нужный бин(ы)
|
||||
@@ -0,0 +1,76 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-exercise-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>test-configuration-demo</name>
|
||||
<description>Test configuration demo</description>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan("ru.otus.example.testconfigurationdemo.family")
|
||||
@SpringBootApplication
|
||||
public class TestConfigurationDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfigurationDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.testconfigurationdemo.family;
|
||||
|
||||
public abstract class FamilyMember {
|
||||
public abstract String getName();
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.childrens;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Son extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Сын";
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
public class Father extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Папа";
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Mother extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Мама";
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user