mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2025-09\spring-06-bean-scopes-and-lifecycle + 2025-09\spring-07-advanced-config
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,20 @@
|
||||
#### Упражнение №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,49 @@
|
||||
<?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>3.4.2</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>beans-lifecycle-exercise</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</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>
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package ru.otus.example.beanslifecycledemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import ru.otus.example.beanslifecycledemo.domain.Phone;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BeansLifecycleDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext ctx = SpringApplication.run(BeansLifecycleDemoApplication.class, args);
|
||||
try {
|
||||
Phone phone = ctx.getBean(Phone.class);
|
||||
phone.callFavoriteNumber();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+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 "Подруга";
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package ru.otus.example.beanslifecycledemo.domain;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Component
|
||||
@ConditionalOnProperty(name = "lifecycle.print.enabled", havingValue = "false")
|
||||
@RequiredArgsConstructor
|
||||
public class Phone {
|
||||
private final String greeting = "Погнали к родителям";
|
||||
|
||||
private 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 "Спорт-лото";
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package ru.otus.example.beanslifecycledemo.lifecycle;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
public class CustomBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware {
|
||||
private Environment environment;
|
||||
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
|
||||
BeanDefinitionRegistry registry,
|
||||
BeanNameGenerator importBeanNameGenerator) {
|
||||
registerBeanDefinitions(importingClassMetadata, registry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata metadata,
|
||||
BeanDefinitionRegistry registry) {
|
||||
|
||||
registerBeanFactoryPostProcessor(registry);
|
||||
registerBeanPostProcessor(registry);
|
||||
|
||||
if (Optional.ofNullable(environment.getProperty("lifecycle.print.enabled", Boolean.class)).orElse(false)) {
|
||||
System.out.println("Шаг #0: ImportBeanDefinitionRegistrar.registerBeanDefinitions\n");
|
||||
registerCustomLifeCycleBean(registry);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void registerCustomLifeCycleBean(BeanDefinitionRegistry registry) {
|
||||
GenericBeanDefinition gbd = new GenericBeanDefinition();
|
||||
gbd.setBeanClass(CustomLifeCycleBean.class);
|
||||
gbd.setInitMethodName("customInitMethod");
|
||||
gbd.setDestroyMethodName("customDestroyMethod");
|
||||
registry.registerBeanDefinition("customLifeCycleBean", gbd);
|
||||
}
|
||||
|
||||
private void registerBeanFactoryPostProcessor(BeanDefinitionRegistry registry) {
|
||||
GenericBeanDefinition gbd = new GenericBeanDefinition();
|
||||
gbd.setBeanClass(CustomBeanFactoryPostProcessor.class);
|
||||
registry.registerBeanDefinition("customBeanFactoryPostProcessor", gbd);
|
||||
}
|
||||
|
||||
private void registerBeanPostProcessor(BeanDefinitionRegistry registry) {
|
||||
GenericBeanDefinition gbd = new GenericBeanDefinition();
|
||||
gbd.setBeanClass(CustomBeanPostProcessor.class);
|
||||
registry.registerBeanDefinition("customBeanPostProcessor", gbd);
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package ru.otus.example.beanslifecycledemo.lifecycle;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
|
||||
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
||||
|
||||
private static final String CLASS_NAME_ATTR = "className";
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
|
||||
if (beanFactory.containsBean("customLifeCycleBean")) {
|
||||
System.out.println("Шаг #1: BeanFactoryPostProcessor.postProcessBeanFactory\n");
|
||||
}
|
||||
|
||||
/* for (String beanName : beanFactory.getBeanDefinitionNames()) {
|
||||
var d = beanFactory.getBeanDefinition(beanName);
|
||||
if (d instanceof ScannedGenericBeanDefinition scannedBeanDefinition) {
|
||||
|
||||
if (!GirlfiendPhoneNumber.class.getName().equalsIgnoreCase(d.getBeanClassName())) {
|
||||
continue;
|
||||
}
|
||||
d.setBeanClassName(FriendPhoneNumber.class.getName());
|
||||
var classNameAttr = new BeanMetadataAttribute(CLASS_NAME_ATTR, FriendPhoneNumber.class.getName());
|
||||
scannedBeanDefinition.addMetadataAttribute(classNameAttr);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package ru.otus.example.beanslifecycledemo.lifecycle;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.InvalidPropertyException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
|
||||
import ru.otus.example.beanslifecycledemo.domain.Phone;
|
||||
|
||||
public class CustomBeanPostProcessor implements BeanPostProcessor {
|
||||
|
||||
public static final String GREETING_PROPERTY = "greeting";
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
private void updateGreeting(Object bean) {
|
||||
Class<?> aClass = Phone.class;
|
||||
try {
|
||||
Field greetingField = aClass.getDeclaredField(GREETING_PROPERTY);
|
||||
|
||||
greetingField.setAccessible(true);
|
||||
greetingField.set(bean, "Ай-да в гараж. Стихи читать!");
|
||||
} catch (Exception e) {
|
||||
throw new InvalidPropertyException(Phone.class, GREETING_PROPERTY,
|
||||
"Bean class does not have expected property", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package ru.otus.example.beanslifecycledemo.lifecycle;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.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");
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.beanslifecycledemo.lifecycle;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Import(CustomBeanDefinitionRegistrar.class)
|
||||
@Configuration
|
||||
public class LifeCycleConfig {
|
||||
/*
|
||||
@Bean
|
||||
public BeanFactoryPostProcessor customBeanFactoryPostProcessor() {
|
||||
return new CustomBeanFactoryPostProcessor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BeanPostProcessor customBeanPostProcessor() {
|
||||
return new CustomBeanPostProcessor();
|
||||
}
|
||||
|
||||
@ConditionalOnProperty(name = "lifecycle.print.enabled", havingValue = "true")
|
||||
@Bean(initMethod = "customInitMethod", destroyMethod = "customDestroyMethod")
|
||||
public CustomLifeCycleBean customLifeCycleBean() {
|
||||
return new CustomLifeCycleBean();
|
||||
}
|
||||
*/
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
|
||||
lifecycle:
|
||||
print:
|
||||
enabled: true
|
||||
@@ -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 @@
|
||||
#### Упражнение №1
|
||||
|
||||
- Расставить скоупы над сервисами так, чтобы приложение запустилось и заработало, как указано на
|
||||
странице http://localhost:8081
|
||||
- Подсказки в названии сервисов
|
||||
- Не забываем про proxyMode для некоторых скоупов
|
||||
|
||||
```
|
||||
Задать скоуп можно с помощью аннотации @Scope
|
||||
Возможные значения: singleton, prototype, session, request
|
||||
```
|
||||
@@ -0,0 +1,56 @@
|
||||
<?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>3.4.2</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>beans-scopes-exercise</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</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.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</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>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
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) {
|
||||
var ctx = SpringApplication.run(BeansScopesDemoApplication.class, args);
|
||||
var serverPort = ctx.getEnvironment().getProperty("local.server.port");
|
||||
System.out.printf("Чтобы смотреть результат переходи сюда: http://localhost:%s", serverPort);
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
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(GreetingService singletonGreetingService,
|
||||
@Qualifier("prototypeGreetingService")
|
||||
GreetingService prototypeGreetingService1,
|
||||
@Qualifier("prototypeGreetingService")
|
||||
GreetingService prototypeGreetingService2,
|
||||
GreetingService sessionGreetingService,
|
||||
GreetingService requestGreetingService
|
||||
) {
|
||||
this.singletonGreetingService = singletonGreetingService;
|
||||
this.prototypeGreetingService1 = prototypeGreetingService1;
|
||||
this.prototypeGreetingService2 = prototypeGreetingService2;
|
||||
this.sessionGreetingService = sessionGreetingService;
|
||||
this.requestGreetingService = requestGreetingService;
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String greetingPage(Model model) {
|
||||
model.addAttribute("singletonGreeting", singletonGreetingService.greeting());
|
||||
model.addAttribute("sessionGreeting", sessionGreetingService.greeting());
|
||||
model.addAttribute("requestGreeting", requestGreetingService.greeting());
|
||||
model.addAttribute("prototype1Greeting", prototypeGreetingService1.greeting());
|
||||
model.addAttribute("prototype2Greeting", prototypeGreetingService2.greeting());
|
||||
return "index";
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public abstract class AbstractGreetingServiceImpl implements GreetingService {
|
||||
|
||||
private final AtomicInteger counter;
|
||||
|
||||
public AbstractGreetingServiceImpl() {
|
||||
this.counter = new AtomicInteger(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String greeting() {
|
||||
return currentGreeting();
|
||||
}
|
||||
|
||||
private String currentGreeting() {
|
||||
return String.format("Привет! Это наша встреча №%d. Меня зовут: %s",
|
||||
counter.incrementAndGet(), Integer.toHexString(hashCode()));
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
public interface GreetingService {
|
||||
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 {
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
server:
|
||||
port: 8081
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
<!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>Singleton должен постоянно накручивать счетчик встреч и не менять имя</li>
|
||||
<li>Prototype1 должен вести себя так же, как и Singleton, но иметь имя отличное от Prototype2</li>
|
||||
<li>Prototype2 должен вести себя так же, как и Singleton, но иметь имя отличное от Prototype1</li>
|
||||
<li>Session должен вести себя как Singleton, но только до перезапуска браузера. После, счетчик встреч должен пойти
|
||||
заново
|
||||
</li>
|
||||
<li>Request должен всегда показывать первую встречу и разное имя</li>
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
<hr/>
|
||||
|
||||
<p th:text="'Singleton сказал: ' + ${singletonGreeting}"></p>
|
||||
<p th:text="'Prototype1 сказал: ' + ${prototype1Greeting}"></p>
|
||||
<p th:text="'Prototype2 сказал: ' + ${prototype2Greeting}"></p>
|
||||
<p th:text="'Session сказал: ' + ${sessionGreeting}"></p>
|
||||
<p th:text="'Request сказал: ' + ${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,56 @@
|
||||
<?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>3.4.2</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>beans-scopes-solution</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</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.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</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>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
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) {
|
||||
var ctx = SpringApplication.run(BeansScopesDemoApplication.class, args);
|
||||
var serverPort = ctx.getEnvironment().getProperty("local.server.port");
|
||||
System.out.printf("Чтобы смотреть результат переходи сюда: http://localhost:%s", serverPort);
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
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(GreetingService singletonGreetingService,
|
||||
@Qualifier("prototypeGreetingService")
|
||||
GreetingService prototypeGreetingService1,
|
||||
@Qualifier("prototypeGreetingService")
|
||||
GreetingService prototypeGreetingService2,
|
||||
GreetingService sessionGreetingService,
|
||||
GreetingService requestGreetingService
|
||||
) {
|
||||
this.singletonGreetingService = singletonGreetingService;
|
||||
this.prototypeGreetingService1 = prototypeGreetingService1;
|
||||
this.prototypeGreetingService2 = prototypeGreetingService2;
|
||||
this.sessionGreetingService = sessionGreetingService;
|
||||
this.requestGreetingService = requestGreetingService;
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String greetingPage(Model model) {
|
||||
model.addAttribute("singletonGreeting", singletonGreetingService.greeting());
|
||||
model.addAttribute("sessionGreeting", sessionGreetingService.greeting());
|
||||
model.addAttribute("requestGreeting", requestGreetingService.greeting());
|
||||
model.addAttribute("prototype1Greeting", prototypeGreetingService1.greeting());
|
||||
model.addAttribute("prototype2Greeting", prototypeGreetingService2.greeting());
|
||||
return "index";
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public abstract class AbstractGreetingServiceImpl implements GreetingService {
|
||||
|
||||
private final AtomicInteger counter;
|
||||
|
||||
public AbstractGreetingServiceImpl() {
|
||||
this.counter = new AtomicInteger(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String greeting() {
|
||||
return currentGreeting();
|
||||
}
|
||||
|
||||
private String currentGreeting() {
|
||||
return String.format("Привет! Это наша встреча №%d. Меня зовут: %s",
|
||||
counter.incrementAndGet(), Integer.toHexString(hashCode()));
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
public interface GreetingService {
|
||||
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(scopeName = "prototype")
|
||||
@Service("prototypeGreetingService")
|
||||
public class PrototypeGreetingServiceImpl extends AbstractGreetingServiceImpl {
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.context.annotation.ScopedProxyMode;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
|
||||
@Service("requestGreetingService")
|
||||
public class RequestGreetingServiceImpl extends AbstractGreetingServiceImpl {
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.example.beansscopesdemo.services;
|
||||
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.context.annotation.ScopedProxyMode;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@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 {
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
server:
|
||||
port: 8081
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
<!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>Singleton должен постоянно накручивать счетчик встреч и не менять имя</li>
|
||||
<li>Prototype1 должен вести себя так же, как и Singleton, но иметь имя отличное от Prototype2</li>
|
||||
<li>Prototype2 должен вести себя так же, как и Singleton, но иметь имя отличное от Prototype1</li>
|
||||
<li>Session должен вести себя как Singleton, но только до перезапуска браузера. После, счетчик встреч должен пойти
|
||||
заново
|
||||
</li>
|
||||
<li>Request должен всегда показывать первую встречу и разное имя</li>
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
<hr/>
|
||||
|
||||
<p th:text="'Singleton сказал: ' + ${singletonGreeting}"></p>
|
||||
<p th:text="'Prototype1 сказал: ' + ${prototype1Greeting}"></p>
|
||||
<p th:text="'Prototype2 сказал: ' + ${prototype2Greeting}"></p>
|
||||
<p th:text="'Session сказал: ' + ${sessionGreeting}"></p>
|
||||
<p th:text="'Request сказал: ' + ${requestGreeting}"></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,18 @@
|
||||
<?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>beans-scopes-exercise</module>
|
||||
<module>beans-scopes-solution</module>
|
||||
<module>beans-lifecycle-exercise</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -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,63 @@
|
||||
<?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>3.5.3</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>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<spring.shell.version>3.4.0</spring.shell.version>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</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>${spring.shell.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<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.applicationeventsdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.shell.command.annotation.CommandScan;
|
||||
|
||||
@CommandScan
|
||||
@SpringBootApplication
|
||||
public class ApplicationEventsDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ApplicationEventsDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
public class AbstractRespondent {
|
||||
public void delay(long mills) {
|
||||
try {
|
||||
Thread.sleep(mills);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.ApplicationEventMulticaster;
|
||||
import org.springframework.context.event.SimpleApplicationEventMulticaster;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
|
||||
//@Configuration
|
||||
public class EventsConfig {
|
||||
|
||||
// Имя бина важно
|
||||
@Bean
|
||||
public ApplicationEventMulticaster applicationEventMulticaster() {
|
||||
SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
|
||||
eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
|
||||
return eventMulticaster;
|
||||
}
|
||||
}
|
||||
+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));
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class NegativeRespondent extends AbstractRespondent implements ApplicationListener<HalfAGlassOfWaterEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(HalfAGlassOfWaterEvent halfAGlassOfWaterEvent) {
|
||||
delay(100);
|
||||
System.out.println("Негативно настроенный слушатель");
|
||||
System.out.println(String.format("- %s", halfAGlassOfWaterEvent.getPayload()));
|
||||
System.out.println("- Какой ужас. Теперь он наполовину пуст!!!\n\n");
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PositiveRespondent extends AbstractRespondent {
|
||||
|
||||
@EventListener
|
||||
public void onApplicationEvent(HalfAGlassOfWaterEvent halfAGlassOfWaterEvent) {
|
||||
delay(100);
|
||||
System.out.println("Позитивно настроенный слушатель");
|
||||
System.out.println(String.format("- %s", halfAGlassOfWaterEvent.getPayload()));
|
||||
System.out.println("- Ничего. Главное, что он наполовину полон!!!\n\n");
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package ru.otus.example.applicationeventsdemo.security;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static java.util.Objects.nonNull;
|
||||
|
||||
@Component
|
||||
public class InMemoryLoginContext implements LoginContext {
|
||||
private String userName;
|
||||
|
||||
@Override
|
||||
public void login(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUserLoggedIn() {
|
||||
return nonNull(userName);
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package ru.otus.example.applicationeventsdemo.security;
|
||||
|
||||
public interface LoginContext {
|
||||
void login(String userName);
|
||||
boolean isUserLoggedIn();
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
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;
|
||||
import ru.otus.example.applicationeventsdemo.security.LoginContext;
|
||||
|
||||
@ShellComponent(value = "Application Events Commands")
|
||||
@RequiredArgsConstructor
|
||||
public class ApplicationEventsCommands {
|
||||
|
||||
private final EventsPublisher eventsPublisher;
|
||||
|
||||
private final LoginContext loginContext;
|
||||
|
||||
@ShellMethod(value = "Login command", key = {"l", "login"})
|
||||
public String login(@ShellOption(defaultValue = "AnyUser") String userName) {
|
||||
loginContext.login(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 loginContext.isUserLoggedIn()
|
||||
? Availability.available()
|
||||
: Availability.unavailable("Сначала залогиньтесь");
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package ru.otus.example.applicationeventsdemo.shellnew;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.shell.command.annotation.Command;
|
||||
import org.springframework.shell.command.annotation.CommandAvailability;
|
||||
import org.springframework.shell.command.annotation.Option;
|
||||
import ru.otus.example.applicationeventsdemo.events.EventsPublisher;
|
||||
import ru.otus.example.applicationeventsdemo.security.LoginContext;
|
||||
|
||||
@Command(group = "Application Events Commands New Way")
|
||||
@RequiredArgsConstructor
|
||||
public class ApplicationEventsCommandsNewWay {
|
||||
|
||||
private final EventsPublisher eventsPublisher;
|
||||
|
||||
private final LoginContext loginContext;
|
||||
|
||||
@Command(description = "Login command new way", command = "login2", alias = "l2")
|
||||
public String login(@Option(defaultValue = "AnyUser") String userName) {
|
||||
loginContext.login(userName);
|
||||
return String.format("Добро пожаловать: %s", userName);
|
||||
}
|
||||
|
||||
@Command(description = "Publish event command new way", command = "publish2", alias = {"p2", "pub2"})
|
||||
@CommandAvailability(provider = "publishEventCommandAvailabilityProvider")
|
||||
public String publishEvent() {
|
||||
eventsPublisher.publish();
|
||||
return "Событие опубликовано";
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package ru.otus.example.applicationeventsdemo.shellnew;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.shell.Availability;
|
||||
import org.springframework.shell.AvailabilityProvider;
|
||||
import ru.otus.example.applicationeventsdemo.security.LoginContext;
|
||||
|
||||
@Configuration
|
||||
public class ShellConfig {
|
||||
|
||||
@Bean
|
||||
public AvailabilityProvider publishEventCommandAvailabilityProvider(LoginContext loginContext) {
|
||||
return () -> loginContext.isUserLoggedIn()
|
||||
? Availability.available()
|
||||
: Availability.unavailable("Сначала залогиньтесь");
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
|
||||
spring:
|
||||
shell:
|
||||
interactive:
|
||||
enabled: true
|
||||
main:
|
||||
allow-circular-references: true
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package ru.otus.example.applicationeventsdemo.shell;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.shell.CommandNotCurrentlyAvailable;
|
||||
import org.springframework.shell.InputProvider;
|
||||
import org.springframework.shell.ResultHandlerService;
|
||||
import org.springframework.shell.Shell;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
|
||||
import ru.otus.example.applicationeventsdemo.events.EventsPublisher;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@DisplayName("Тест команд shell ")
|
||||
@SpringBootTest
|
||||
class ApplicationEventsCommandsTest {
|
||||
|
||||
private static final String GREETING_PATTERN = "Добро пожаловать: %s";
|
||||
private static final String DEFAULT_LOGIN = "AnyUser";
|
||||
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";
|
||||
|
||||
private InputProvider inputProvider;
|
||||
|
||||
private ArgumentCaptor<Object> argumentCaptor;
|
||||
|
||||
//@MockBean
|
||||
@MockitoBean
|
||||
private EventsPublisher eventsPublisher;
|
||||
|
||||
|
||||
//@SpyBean
|
||||
@MockitoSpyBean
|
||||
private ResultHandlerService resultHandlerService;
|
||||
|
||||
@Autowired
|
||||
private Shell shell;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
inputProvider = mock(InputProvider.class);
|
||||
argumentCaptor = ArgumentCaptor.forClass(Object.class);
|
||||
}
|
||||
|
||||
@DisplayName(" должен возвращать приветствие для всех форм команды логина")
|
||||
@Test
|
||||
void shouldReturnExpectedGreetingAfterLoginCommandEvaluated() throws Exception {
|
||||
when(inputProvider.readInput())
|
||||
.thenReturn(() -> COMMAND_LOGIN)
|
||||
.thenReturn(() -> COMMAND_LOGIN_SHORT)
|
||||
.thenReturn(() -> String.format(COMMAND_LOGIN_PATTERN, COMMAND_LOGIN_SHORT, CUSTOM_LOGIN))
|
||||
.thenReturn(null);
|
||||
|
||||
shell.run(inputProvider);
|
||||
verify(resultHandlerService, times(3)).handle(argumentCaptor.capture());
|
||||
List<Object> results = argumentCaptor.getAllValues();
|
||||
assertThat(results).containsExactlyInAnyOrder(String.format(GREETING_PATTERN, DEFAULT_LOGIN),
|
||||
String.format(GREETING_PATTERN, DEFAULT_LOGIN),
|
||||
String.format(GREETING_PATTERN, CUSTOM_LOGIN));
|
||||
}
|
||||
|
||||
@DisplayName(" должен возвращать CommandNotCurrentlyAvailable если при попытке выполнения команды publish пользователь выполнил вход")
|
||||
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
|
||||
@Test
|
||||
void shouldReturnCommandNotCurrentlyAvailableObjectWhenUserDoesNotLoginAfterPublishCommandEvaluated() {
|
||||
when(inputProvider.readInput())
|
||||
.thenReturn(() -> COMMAND_PUBLISH)
|
||||
.thenReturn(null);
|
||||
|
||||
assertThatCode(() -> shell.run(inputProvider)).isInstanceOf(CommandNotCurrentlyAvailable.class);
|
||||
}
|
||||
|
||||
@DisplayName(" должен возвращать статус выполнения команды publish и вызвать соответствующий метод сервиса есл и команда выполнена после входа")
|
||||
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
|
||||
@Test
|
||||
void shouldReturnExpectedMessageAndFirePublishMethodAfterPublishCommandEvaluated() throws Exception {
|
||||
when(inputProvider.readInput())
|
||||
.thenReturn(() -> COMMAND_LOGIN)
|
||||
.thenReturn(() -> COMMAND_PUBLISH)
|
||||
.thenReturn(null);
|
||||
|
||||
shell.run(inputProvider);
|
||||
verify(resultHandlerService, times(2)).handle(argumentCaptor.capture());
|
||||
assertThat(argumentCaptor.getValue()).isEqualTo(COMMAND_PUBLISH_EXPECTED_RESULT);
|
||||
verify(eventsPublisher, times(1)).publish();
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
spring:
|
||||
shell:
|
||||
interactive:
|
||||
enabled: false
|
||||
main:
|
||||
allow-circular-references: true
|
||||
@@ -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 @@
|
||||
#### Упражнение №1
|
||||
Условия вечеринки:
|
||||
- Алексей придет если condition.alexey-exists=true
|
||||
- Анна придет если будет Алексей
|
||||
- Олег придет если включен профиль Oleg
|
||||
- Петр придет если включен профиль Peter
|
||||
- Янис придет если condition.yanis-exists=true
|
||||
- Яна придет если нет Алексея, но есть Янис
|
||||
|
||||
Измените только одну настройку в application.yml, чтобы на вечеринку пришло максимальное количество людей
|
||||
Напечатать имена пришедших на вечеринку можно с помощью команды "ppm" или "print-party-members"
|
||||
@@ -0,0 +1,57 @@
|
||||
<?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>3.5.3</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>conditional-and-profiles-exercise</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</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>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.Party;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ConditionalAndProfilesDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext ctx = SpringApplication.run(ConditionalAndProfilesDemoApplication.class, args);
|
||||
Party party = ctx.getBean(Party.class);
|
||||
party.printPartyMembers();
|
||||
}
|
||||
|
||||
}
|
||||
+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 "Олег";
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class Party {
|
||||
private final List<Friend> partyMembers;
|
||||
|
||||
public void printPartyMembers() {
|
||||
System.out.println("Участники вечеринки:");
|
||||
System.out.println(partyMembers.stream().map(Friend::getName)
|
||||
.collect(Collectors.joining("\n")));
|
||||
}
|
||||
}
|
||||
+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 {
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
condition:
|
||||
#yanis-exists: true
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
condition:
|
||||
alexey-exists: true
|
||||
yanis-exists: false
|
||||
|
||||
#Доступные профили: Oleg и Peter
|
||||
spring:
|
||||
profiles:
|
||||
active:
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
config:
|
||||
activate:
|
||||
on-profile: Peter
|
||||
# Старый вариант включения профилей
|
||||
#spring:
|
||||
# profiles: Peter
|
||||
|
||||
condition:
|
||||
yanis-exists: true
|
||||
@@ -0,0 +1,24 @@
|
||||
<?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>conditional-and-profiles-exercise</module>
|
||||
<module>application-events-demo</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>
|
||||
<module>test-caching-demo</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,57 @@
|
||||
<?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>3.5.3</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-caching-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</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>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.example.testconfigurationdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
public class TestConfigurationDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfigurationDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo.statefulservices;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class Service1 {
|
||||
@Getter
|
||||
private final String name = "Service1";
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String state = "State1";
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo.statefulservices;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class Service2 {
|
||||
@Getter
|
||||
private final String name = "Service2";
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String state = "State2";
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package ru.otus.example.testconfigurationdemo.statefulservices;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
//@SpringBootTest(classes = {Service1.class, Service2.class})
|
||||
//@TestPropertySource("classpath:test.properties")
|
||||
//@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
|
||||
@SpringBootTest
|
||||
class IntegrationTest1 {
|
||||
|
||||
@Autowired
|
||||
private Service1 service1;
|
||||
|
||||
//@MockBean
|
||||
//@MockitoBean
|
||||
@Autowired
|
||||
private Service2 service2;
|
||||
|
||||
@Test
|
||||
void test1() {
|
||||
System.out.println(service1.getName() + ": " + service1.getState());
|
||||
System.out.println(service2.getName() + ": " + service2.getState());
|
||||
|
||||
service1.setState("State7");
|
||||
service2.setState("State8");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test2() {
|
||||
System.out.println(service1.getName() + ": " + service1.getState());
|
||||
System.out.println(service2.getName() + ": " + service2.getState());
|
||||
|
||||
service1.setState("State9");
|
||||
service2.setState("State10");
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package ru.otus.example.testconfigurationdemo.statefulservices;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class IntegrationTest2 {
|
||||
|
||||
@Autowired
|
||||
private Service1 service1;
|
||||
|
||||
@Autowired
|
||||
private Service2 service2;
|
||||
|
||||
@Test
|
||||
void test3() {
|
||||
System.out.println(service1.getName() + ": " + service1.getState());
|
||||
System.out.println(service2.getName() + ": " + service2.getState());
|
||||
|
||||
service1.setState("State3");
|
||||
service2.setState("State4");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test4() {
|
||||
System.out.println(service1.getName() + ": " + service1.getState());
|
||||
System.out.println(service2.getName() + ": " + service2.getState());
|
||||
|
||||
service1.setState("State5");
|
||||
service2.setState("State6");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
any.prop=10
|
||||
@@ -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 @@
|
||||
#### Упражнение №2. Должен остаться только один
|
||||
|
||||
С помощью вложенных конфигураций сделать так,
|
||||
чтобы прошел NestedConfigurationDemoTest
|
||||
(family должно содержать только собаку)
|
||||
|
||||
- Сделать вложенный класс конфигурации. Не забываем про static
|
||||
- Выбрать тип конфигурации (@Configuration/@TestConfiguration)
|
||||
- С помощью @Bean/@ComponentScan добавить в контекст нужный бин(ы)
|
||||
@@ -0,0 +1,57 @@
|
||||
<?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>3.5.3</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-exercise-1</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</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>
|
||||
</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 @@
|
||||
#### Упражнение №3. Вернуть отца в семью
|
||||
|
||||
С помощью вложенных конфигураций сделать так,
|
||||
чтобы прошел NestedTestConfigurationDemoTest
|
||||
(family должно содержать маму, папу, сына и собаку)
|
||||
|
||||
- Сделать вложенный класс конфигурации. Не забываем про static
|
||||
- Выбрать тип конфигурации (@Configuration/@TestConfiguration)
|
||||
- С помощью @Bean/@ComponentScan добавить в контекст нужный бин(ы)
|
||||
@@ -0,0 +1,57 @@
|
||||
<?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>3.5.3</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-exercise-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</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>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user