弹簧注射用注释

时间:2017-07-13 21:18:20

标签: java spring

我刚刚开始使用DI和Spring。我有这两个组件(伪代码)

@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;
    }
}

我想要实现的是使用构造函数参数注入AuthService,其中一个构造函数参数是ChannelInboundHandlerAdapter类的ChannelHandlerContext。不确定这是否可能。

1 个答案:

答案 0 :(得分:1)

是的,这是可能的,但必须将CustomerService和ChannerHandlerContext定义为spring bean(如@ Component,@ Service,@ Controller或@Bean注释)才能在构造函数中自动装配。您可以查看this帖子以获取有关该主题的更多信息。

相关问题