#35 springboot: @Profile

This commit is contained in:
haerong22
2023-03-21 01:42:04 +09:00
parent cd964ecf23
commit 524ddcf50a
7 changed files with 88 additions and 1 deletions

View File

@@ -13,7 +13,9 @@ import org.springframework.context.annotation.Import;
//@Import(MyDataSourceConfigV1.class)
//@Import(MyDataSourceConfigV2.class)
@Import(MyDataSourceConfigV3.class)
@SpringBootApplication(scanBasePackages = "hello.datasource")
@SpringBootApplication(
scanBasePackages = {"hello.datasource", "hello.pay"}
)
@ConfigurationPropertiesScan
public class ExternalReadApplication {

View File

@@ -0,0 +1,11 @@
package hello.pay;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LocalPayClient implements PayClient {
@Override
public void pay(int money) {
log.info("로컬 결제 money={}", money);
}
}

View File

@@ -0,0 +1,18 @@
package hello.pay;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class OrderRunner implements ApplicationRunner {
private final OrderService orderService;
@Override
public void run(ApplicationArguments args) throws Exception {
orderService.order(1000);
}
}

View File

@@ -0,0 +1,15 @@
package hello.pay;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class OrderService {
private final PayClient payClient;
public void order(int money) {
payClient.pay(money);
}
}

View File

@@ -0,0 +1,5 @@
package hello.pay;
public interface PayClient {
void pay(int money);
}

View File

@@ -0,0 +1,25 @@
package hello.pay;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Slf4j
@Configuration
public class PayConfig {
@Bean
@Profile("default")
public LocalPayClient localPayClient() {
log.info("LocalPayClient 빈 등록");
return new LocalPayClient();
}
@Bean
@Profile("prod")
public ProdPayClient prodPayClient() {
log.info("ProdPayClient 빈 등록");
return new ProdPayClient();
}
}

View File

@@ -0,0 +1,11 @@
package hello.pay;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ProdPayClient implements PayClient {
@Override
public void pay(int money) {
log.info("운영 결제 money={}", money);
}
}