diff --git a/README.md b/README.md index ac9e853..f1eba84 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ Мой репозиторий с домашками по курсу Spring Boot Тараканова Екатерина, преподаватель Королёв Александр + + +Дз по теме DI: +Реализовать приложение (не важно какая логика, так как основой упор идет на DI), в котором: +- используется DI через конструктор +- используется DI через поле +- используется DI через сеттеры +- создаются и внедряются два бина разных классов, но реализующих один интерфейс +- при создании и “разрушении” бина в лог выводится сообщение +(примечание: здесь вам не понадобятся контроллеры. Достаточно описать только то, что описано выше) diff --git a/src/main/java/com/example/SpringBootProject/entity/Animal.java b/src/main/java/com/example/SpringBootProject/entity/Animal.java new file mode 100644 index 0000000..ea4a5e7 --- /dev/null +++ b/src/main/java/com/example/SpringBootProject/entity/Animal.java @@ -0,0 +1,5 @@ +package com.example.SpringBootProject.entity; + +public interface Animal { + void makeSound(); +} diff --git a/src/main/java/com/example/SpringBootProject/entity/Cat.java b/src/main/java/com/example/SpringBootProject/entity/Cat.java new file mode 100644 index 0000000..fc02eb5 --- /dev/null +++ b/src/main/java/com/example/SpringBootProject/entity/Cat.java @@ -0,0 +1,13 @@ +package com.example.SpringBootProject.entity; + +import lombok.extern.log4j.Log4j2; +import org.springframework.stereotype.Component; + +@Component +@Log4j2 +public class Cat implements Animal { + @Override + public void makeSound(){ + log.info("Мяу мяу"); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/SpringBootProject/entity/Dog.java b/src/main/java/com/example/SpringBootProject/entity/Dog.java new file mode 100644 index 0000000..cbdbefc --- /dev/null +++ b/src/main/java/com/example/SpringBootProject/entity/Dog.java @@ -0,0 +1,13 @@ +package com.example.SpringBootProject.entity; + +import lombok.extern.log4j.Log4j2; +import org.springframework.stereotype.Component; + +@Component +@Log4j2 +public class Dog implements Animal { + @Override + public void makeSound() { + log.info("Гав гав"); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/SpringBootProject/services/ConstructorInjection.java b/src/main/java/com/example/SpringBootProject/services/ConstructorInjection.java new file mode 100644 index 0000000..047f526 --- /dev/null +++ b/src/main/java/com/example/SpringBootProject/services/ConstructorInjection.java @@ -0,0 +1,33 @@ +package com.example.SpringBootProject.services; + + +import com.example.SpringBootProject.entity.Animal; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import lombok.RequiredArgsConstructor; +import lombok.extern.log4j.Log4j2; +import org.springframework.stereotype.Service; + + +@Log4j2 +@Service +@RequiredArgsConstructor +public class ConstructorInjection { + private final Animal dog; + private final Animal cat; + + @PostConstruct + private void init(){ + log.info("ConstructInjection is started"); + } + + @PreDestroy + private void destroy(){ + log.info("ConstructInjection is destroyed"); + } + + private void animalMakeNoise(){ + cat.makeSound(); + dog.makeSound(); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/SpringBootProject/services/FieldInjection.java b/src/main/java/com/example/SpringBootProject/services/FieldInjection.java new file mode 100644 index 0000000..3de1c3b --- /dev/null +++ b/src/main/java/com/example/SpringBootProject/services/FieldInjection.java @@ -0,0 +1,32 @@ +package com.example.SpringBootProject.services; + +import com.example.SpringBootProject.entity.Animal; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Log4j2 +@Service +public class FieldInjection { + @Autowired + private Animal dog; + @Autowired + private Animal cat; + + @PostConstruct + private void init(){ + log.info("FieldInjection is started"); + } + + @PreDestroy + private void destroy(){ + log.info("FieldInjection is destroyed"); + } + + private void animalMakeNoise(){ + cat.makeSound(); + dog.makeSound(); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/SpringBootProject/services/SetterInjection.java b/src/main/java/com/example/SpringBootProject/services/SetterInjection.java new file mode 100644 index 0000000..278841b --- /dev/null +++ b/src/main/java/com/example/SpringBootProject/services/SetterInjection.java @@ -0,0 +1,39 @@ +package com.example.SpringBootProject.services; + +import com.example.SpringBootProject.entity.Animal; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Log4j2 +@Service +public class SetterInjection { + private Animal cat; + private Animal dog; + + @PostConstruct + private void init(){ + log.info("SetterInjection is started"); + } + + @PreDestroy + private void destroy(){ + log.info("SetterInjection is destroyed"); + } + @Autowired + private void setDog(Animal dog){ + this.dog = dog; + } + + @Autowired + private void setCat(Animal cat){ + this.cat = cat; + } + + private void animalMakeNoise(){ + cat.makeSound(); + dog.makeSound(); + } +} \ No newline at end of file