Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
Мой репозиторий с домашками по курсу Spring Boot

Тараканова Екатерина, преподаватель Королёв Александр


Дз по теме DI:
Реализовать приложение (не важно какая логика, так как основой упор идет на DI), в котором:
- используется DI через конструктор
- используется DI через поле
- используется DI через сеттеры
- создаются и внедряются два бина разных классов, но реализующих один интерфейс
- при создании и “разрушении” бина в лог выводится сообщение
(примечание: здесь вам не понадобятся контроллеры. Достаточно описать только то, что описано выше)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.SpringBootProject.entity;

public interface Animal {
void makeSound();
}
13 changes: 13 additions & 0 deletions src/main/java/com/example/SpringBootProject/entity/Cat.java
Original file line number Diff line number Diff line change
@@ -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("Мяу мяу");
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/example/SpringBootProject/entity/Dog.java
Original file line number Diff line number Diff line change
@@ -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("Гав гав");
}
}
Original file line number Diff line number Diff line change
@@ -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");
Comment thread
EkaterinaTarakanova marked this conversation as resolved.
}

@PreDestroy
private void destroy(){
log.info("ConstructInjection is destroyed");
}

private void animalMakeNoise(){
cat.makeSound();
dog.makeSound();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}