7.15. 삽입에서 프록시 사용
빈의 라이프사이클이 서로 다르면 프록시가 주입에 사용됩니다. 프록시는 런타임 시 생성되는 빈의 하위 클래스이며 빈 클래스의 모든 비개인 메서드를 재정의합니다. 프록시는 호출을 실제 빈 인스턴스로 전달합니다.
이 예에서 PaymentProcessor 인스턴스는 플레이버에 직접 삽입되지 않습니다. 대신 프록시가 삽입되고 processPayment() 메서드가 호출되면 프록시는 현재 PaymentProcessor 빈 인스턴스를 조회하고 해당 서버에서 processPayment() 메서드를 호출합니다.
예제: 프록시 삽입
@ConversationScoped
class PaymentProcessor
{
public void processPayment(int amount)
{
System.out.println("I'm taking $" + amount);
}
}
@ApplicationScoped
public class Shop
{
@Inject
PaymentProcessor paymentProcessor;
public void buyStuff()
{
paymentProcessor.processPayment(100);
}
}