-
Notifications
You must be signed in to change notification settings - Fork 0
3_DI #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
3_DI #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package org.javaspringcourse.controller; | ||
|
|
||
| import jakarta.annotation.PostConstruct; | ||
| import jakarta.annotation.PreDestroy; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.javaspringcourse.service.TransactionService; | ||
| import org.springframework.stereotype.Controller; | ||
|
|
||
| @Slf4j | ||
| @Controller | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему Controller? никаких web вызовов я не увидел |
||
| @RequiredArgsConstructor | ||
| public class AccountBaseLevel { | ||
| private final TransactionService service; // Внедрение через конструктор | ||
|
|
||
| @PostConstruct | ||
| public void create() { | ||
| log.info("Create AccountBaseLevel bean"); | ||
| } | ||
|
|
||
| public void someOperation() { | ||
| service.createTransaction(); | ||
| service.transaction(); | ||
| } | ||
|
|
||
| @PreDestroy | ||
| public void destroy() { | ||
| log.info("Destroy AccountBaseLevel bean"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package org.javaspringcourse.controller; | ||
|
|
||
| import jakarta.annotation.PostConstruct; | ||
| import jakarta.annotation.PreDestroy; | ||
| import lombok.Setter; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.javaspringcourse.service.TransactionService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.stereotype.Controller; | ||
|
|
||
| @Slf4j | ||
| @Controller | ||
| public class AccountPremiumLevel { | ||
| @Autowired | ||
| private TransactionService deposit; // Внедрение через поле | ||
|
|
||
| @Setter(onMethod_ = @Autowired) | ||
| @Qualifier("WithdrawService") | ||
| private TransactionService withdraw; // Внедрение через сеттер | ||
|
|
||
| @PostConstruct | ||
| public void create() { | ||
| log.info("Create AccountPremiumLevel bean"); | ||
| } | ||
|
|
||
| public void deposit() { | ||
| deposit.createTransaction(); | ||
| deposit.transaction(); | ||
| } | ||
|
|
||
| public void withdraw() { | ||
| withdraw.createTransaction(); | ||
| withdraw.transaction(); | ||
| } | ||
|
Comment on lines
+27
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Методы не используются |
||
|
|
||
| @PreDestroy | ||
| public void destroy() { | ||
| log.info("Destroy AccountPremiumLevel bean"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package org.javaspringcourse.service; | ||
|
|
||
| public interface TransactionService { | ||
| void createTransaction(); | ||
| void transaction(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package org.javaspringcourse.service.implementation; | ||
|
|
||
| import jakarta.annotation.PostConstruct; | ||
| import jakarta.annotation.PreDestroy; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.javaspringcourse.service.TransactionService; | ||
| import org.springframework.context.annotation.Primary; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @Primary | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. неудачное использование Primary, можно обойтись и без него (да и без Qualifire). Используй внедрение через имя поля (когда имя поля соответствует имени бина). Как правило Qualifire это дурной тон, аннотация хоть и бывает полезной в определенных случаях (как и Primary) но в реальном мире ее стараются не использовать |
||
| public class DepositService implements TransactionService { | ||
|
|
||
| @PostConstruct | ||
| public void create() { | ||
| log.info("Create DepositService bean"); | ||
| } | ||
|
|
||
| @Override | ||
| public void createTransaction() { | ||
| System.out.println("Create Deposit Transaction"); | ||
| } | ||
|
|
||
| @Override | ||
| public void transaction() { | ||
| System.out.println("Deposit transaction..."); | ||
| } | ||
|
|
||
| @PreDestroy | ||
| public void destroy() { | ||
| log.info("Destroy DepositService bean"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package org.javaspringcourse.service.implementation; | ||
|
|
||
| import jakarta.annotation.PostConstruct; | ||
| import jakarta.annotation.PreDestroy; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.javaspringcourse.service.TransactionService; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| public class WithdrawService implements TransactionService { | ||
|
|
||
| @PostConstruct | ||
| public void create() { | ||
| log.info("Create WithdrawService bean"); | ||
| } | ||
|
|
||
| @Override | ||
| public void createTransaction() { | ||
| System.out.println("Create Withdraw Transaction"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ты ведь здесь же в классе логирование используешь, зачем System.out?)) Всегда используй логгер, вместо System.out |
||
| } | ||
|
|
||
| @Override | ||
| public void transaction() { | ||
| System.out.println("Withdraw transaction..."); | ||
| } | ||
|
|
||
| @PreDestroy | ||
| public void destroy() { | ||
| log.info("Destroy WithdrawService bean"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше будет указать в compileOnly и версию можно не указывать