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
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ repositories {

dependencies {
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.projectlombok:lombok:1.18.30")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше будет указать в compileOnly и версию можно не указывать

testImplementation("org.springframework.boot:spring-boot-starter-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
annotationProcessor("org.projectlombok:lombok")
}

tasks.withType<Test> {
Expand Down
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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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");

Choose a reason for hiding this comment

The 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");
}
}