'Class field in Netty ChannelInboundHandler could not autowired
I try to autowire some service class in channelInboundhandler class field.
but when handler use repository field it always null.
Here is my Netty Configuration
my.netty:
server:
bind: 9000
@SpringBootApplication
public class NettyApplication {
public static void main(String[] args) {
SpringApplication.run(NettyApplication.class, args);
}
@Bean
NettyServerConfig serverConfig(@Autowired ServerHandler serverHandler) {
return NettyServerConfig.builder()
.propertiesPrefix("my.netty.server")
.channelInitializer(pipelineOf(loggingChannelHandler(), serverHandler))
.build();
}
@Bean
ChannelHandler loggingChannelHandler() {
return new LoggingHandler(INFO);
}
}
And channelinboundhandler, Service(Repository)
@Slf4j
@Component
@Sharable
public class ServerHandler extends ChannelInboundHandlerAdapter {
@Autowired ServerRepository repository; // always null
@Override
public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception {
log.info("ServerHandler.channelRead()");
String data = repository.findOne();
log.info("data={}", data);
ctx.writeAndFlush(object);
}
}
@Repository
public class ServerRepository {
public String findOne() {
sleep(1000); // data-access time
return "data";
}
private void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Is There any Problem?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
