2017-07-13 10 views
1

私はちょうどDIと春から始めています。私は私が達成しようとしていることのAuthServiceはコンストラクタ引数のいずれかがChannelInboundHandlerAdapterクラスからChannelHandlerContextあるコンストラクタのパラメータ、を注入してきている注釈を付けた春の独特の注入

@Component 
public class AuthHandlerImpl extends ChannelInboundHandlerAdapter implements AuthHandler { 

    @Autowired 
    AuthService authService; 

    @Override 
    channelRead(ChannelHandlerContext ctx, Object msg) { 
     authService.authenticate(msg); // want to pass ctx to constructor of authService 
    } 
} 

@Component 
public class AuthServiceImpl implements AuthService { 

    private CustomerService customerService; 

    private ChannelHandlerContext ctx; 

    @Autowired 
    public AuthServiceImpl(CustomerService customerService, ChannelHandlerContext ctx) { 
     this.customerService = customerService; 
     this.ctx = ctx; 
    } 
} 

これら二つのコンポーネントを持っている(擬似コード)。それが可能かどうかはわかりません。

答えて

1

はい、可能ですが、CustomerServiceとChannerHandlerContextは、コンストラクタにオートワイヤードされるために、Spring Beans(@Component、@Service、@Controller、または@Beanアノテーションなど)として定義する必要があります。 this投稿を確認すると、トピックに関する詳細情報が表示されます。

+0

したがって、ChannelHandlerContextがサードパーティのフレームワークタイプである場合でも、これは可能ですか?そして、それはアノテーションで可能ですか? – Crystal

+1

\ @Configurationクラスを定義できます。内部では、サードパーティのコンポーネントを返す特定のアノテーション(\ @Bean)を持つメソッドを作成する必要があります。 \ @Configuration クラスMyConfigurationClass {@Bean 公共getChannelHandlerContext \ (){)(新しいChannelHandlerConextを返す 。 // ChannelHandlerContextは\ @Beanアノテーションのおかげでspring beanです。 } } –