mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2019-08-Spring-05 examples 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,53 @@
|
||||
<?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>
|
||||
</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>
|
||||
|
||||
|
||||
</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.applicationeventsdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ApplicationEventsDemoApplication {
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class NegativeRespondent implements ApplicationListener<HalfAGlassOfWaterEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(HalfAGlassOfWaterEvent halfAGlassOfWaterEvent) {
|
||||
System.out.println("Негативно настроенный слушатель");
|
||||
System.out.println(String.format("- %s", halfAGlassOfWaterEvent.getPayload()));
|
||||
System.out.println("- Какой ужас. Теперь он наполовину пуст!!!\n\n");
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PositiveRespondent implements ApplicationListener<HalfAGlassOfWaterEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(HalfAGlassOfWaterEvent halfAGlassOfWaterEvent) {
|
||||
System.out.println("Позитивно настроенный слушатель");
|
||||
System.out.println(String.format("- %s", halfAGlassOfWaterEvent.getPayload()));
|
||||
System.out.println("- Ничего. Главное, что он наполовину полон!!!\n\n");
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
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 void publishEvent() {
|
||||
eventsPublisher.publish();
|
||||
}
|
||||
|
||||
private Availability isPublishEventCommandAvailable() {
|
||||
return userName == null? Availability.unavailable("Сначала залогиньтесь"): Availability.available();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
@@ -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,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.1.7.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>beans-lifecycle-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>beans-lifecycle-demo</name>
|
||||
<description>Beans lifecycle demo</description>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</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 "Спорт-лото";
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
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("Шаг #8: 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, "Ай-да в гараж. Стихи читать!");
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
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;
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
System.out.println("Шаг #6: InitializingBean.afterPropertiesSet\n");
|
||||
}
|
||||
|
||||
public void customInitMethod() throws Exception {
|
||||
System.out.println("Шаг #7: CustomLifeCycleBean.customInitMethod\n");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
System.out.println("Шаг #9: DisposableBean.destroy\n");
|
||||
}
|
||||
|
||||
public void customDestroyMethod() throws Exception {
|
||||
System.out.println("Шаг #10: 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,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.1.7.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>beans-scopes-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>beans-scopes-demo</name>
|
||||
<description>Beans scopes demo</description>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.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.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 = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
@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 = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
@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
|
||||
@@ -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,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.1.7.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>conditional-and-profiles-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>conditional-and-profiles-demo</name>
|
||||
<description>Conditional and profiles demo</description>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.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.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"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
greetings:
|
||||
first-greeting: Привет
|
||||
re-greeting: Рад снова тебя видеть
|
||||
|
||||
|
||||
condition:
|
||||
alexey-exists: true
|
||||
yanis-exists: false
|
||||
|
||||
spring:
|
||||
profiles:
|
||||
#active: Oleg
|
||||
#active: Peter
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: Peter
|
||||
|
||||
condition:
|
||||
yanis-exists: true
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.test;
|
||||
|
||||
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.test.context.ActiveProfiles;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
//@ActiveProfiles({"Oleg"})
|
||||
//@ActiveProfiles({"Peter"})
|
||||
//@ActiveProfiles({"Oleg", "Peter"})
|
||||
@DisplayName("FriendsMap должна")
|
||||
@SpringBootTest
|
||||
class ConditionalAndProfilesDemoApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private Map<String, Friend> friendsMap;
|
||||
|
||||
|
||||
@DisplayName(" содержать Олега")
|
||||
@Test
|
||||
void shouldContainOleg() {
|
||||
assertThat(friendsMap).containsKey("oleg");
|
||||
}
|
||||
|
||||
@DisplayName(" содержать Петра")
|
||||
@Test
|
||||
void shouldContainPetr() {
|
||||
assertThat(friendsMap).containsKey("peter");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
spring:
|
||||
shell:
|
||||
interactive:
|
||||
enabled: false
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>spring-05</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>beans-scopes-demo</module>
|
||||
<module>beans-lifecycle-demo</module>
|
||||
<module>application-events-demo</module>
|
||||
<module>conditional-and-profiles-demo</module>
|
||||
<module>test-configuration-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,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.1.7.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>test-configuration-demo</name>
|
||||
<description>Test configuration demo</description>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.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.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 "Собака";
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo1;
|
||||
|
||||
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.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
import ru.otus.example.testconfigurationdemo.family.pets.Dog;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В NestedConfigurationDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class NestedConfigurationDemoTest {
|
||||
|
||||
@Configuration
|
||||
static class NestedConfiguration {
|
||||
@Bean
|
||||
FamilyMember dog() {
|
||||
return new Dog();
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать только собаку ")
|
||||
@Test
|
||||
void shouldContainOnlyDog() {
|
||||
assertThat(family).containsOnlyKeys("dog");
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo1;
|
||||
|
||||
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.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
import ru.otus.example.testconfigurationdemo.family.parents.Father;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В NestedTestConfigurationDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class NestedTestConfigurationDemoTest {
|
||||
|
||||
@TestConfiguration
|
||||
static class NestedTestConfiguration {
|
||||
@Bean
|
||||
FamilyMember father() {
|
||||
return new Father();
|
||||
}
|
||||
}
|
||||
|
||||
@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.demo1;
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo2;
|
||||
|
||||
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("В SpringBootTestWithExternalLimitationDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class SpringBootTestWithExternalLimitationDemoTest {
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать маму, папу и сына")
|
||||
@Test
|
||||
void shouldContainAllFamilyExceptFather() {
|
||||
assertThat(family).containsOnlyKeys("mother", "father", "son");
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo2;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
import ru.otus.example.testconfigurationdemo.family.parents.Father;
|
||||
|
||||
@ComponentScan({"ru.otus.example.testconfigurationdemo.family.parents",
|
||||
"ru.otus.example.testconfigurationdemo.family.childrens"})
|
||||
@SpringBootConfiguration
|
||||
public class TestSpringBootConfiguration {
|
||||
@Bean
|
||||
FamilyMember father() {
|
||||
return new Father();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user