2020-02 spring 05 classwork has been updated

This commit is contained in:
stvort
2020-03-11 18:42:19 +04:00
parent 36dc6535c1
commit 63ca6bb7c2
14 changed files with 46 additions and 65 deletions
@@ -1,14 +1,14 @@
package ru.otus.example.applicationeventsdemo.events;
import lombok.SneakyThrows;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class PositiveRespondent implements ApplicationListener<HalfAGlassOfWaterEvent> {
public class PositiveRespondent {
@SneakyThrows
@Override
@EventListener
public void onApplicationEvent(HalfAGlassOfWaterEvent halfAGlassOfWaterEvent) {
Thread.sleep(100);
System.out.println("Позитивно настроенный слушатель");
@@ -17,7 +17,7 @@ public class ApplicationEventsCommands {
private String userName;
@ShellMethod(value = "Login command", key = {"l", "login"})
public String login(@ShellOption(defaultValue = "stvort") String userName) {
public String login(@ShellOption(defaultValue = "AnyUser") String userName) {
this.userName = userName;
return String.format("Добро пожаловать: %s", userName);
}
@@ -24,12 +24,6 @@
<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>
@@ -2,12 +2,19 @@ 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) {
SpringApplication.run(BeansLifecycleDemoApplication.class, args);
ApplicationContext ctx = SpringApplication.run(BeansLifecycleDemoApplication.class, args);
try {
Phone phone = ctx.getBean(Phone.class);
phone.callFavoriteNumber();
}catch (Exception e) {
}
}
}
@@ -1,9 +1,11 @@
package ru.otus.example.beanslifecycledemo.domain;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(name = "lifecycle.print.enabled", havingValue = "false")
@RequiredArgsConstructor
public class Phone {
private String greeting = "Погнали к родителям";
@@ -19,7 +19,7 @@ public class LifeCycleConfig {
return new CustomBeanPostProcessor();
}
@ConditionalOnProperty(name = "spring.shell.interactive.enabled", havingValue = "false")
@ConditionalOnProperty(name = "lifecycle.print.enabled", havingValue = "true")
@Bean(initMethod = "customInitMethod", destroyMethod = "customDestroyMethod")
public CustomLifeCycleBean customLifeCycleBean() {
return new CustomLifeCycleBean();
@@ -1,18 +0,0 @@
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();
}
}
@@ -2,7 +2,6 @@ logging:
level:
root: ERROR
spring:
shell:
interactive:
enabled: false
lifecycle:
print:
enabled: true
@@ -59,14 +59,6 @@
<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>
@@ -2,12 +2,16 @@ 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) {
SpringApplication.run(ConditionalAndProfilesDemoApplication.class, args);
ApplicationContext ctx = SpringApplication.run(ConditionalAndProfilesDemoApplication.class, args);
Party party = ctx.getBean(Party.class);
party.printPartyMembers();
}
}
@@ -0,0 +1,19 @@
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")));
}
}
@@ -1,21 +0,0 @@
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"));
}
}
@@ -4,7 +4,7 @@ condition:
spring:
profiles:
#Доступные профили: Oleg, Peter
#Доступные профили: Oleg и Peter
active:
logging:
@@ -0,0 +1,3 @@
1583937635277:p
1583937640796:l
1583937643194:p