Ima*_*n H 1 java jpa netty spring-boot
我有一个具有以下结构的 Spring Boot 应用程序:
根类是:
@ComponentScan(basePackages = {"com.test"})
//@EnableJpaRepositories
//@EntityScan
public class MyApplication {
...
Run Code Online (Sandbox Code Playgroud)
Netty服务器:
package com.test.netty;
@Service
@Slf4j
public class NettyServer {
private EventLoopGroup boss = new NioEventLoopGroup();
private EventLoopGroup work = new NioEventLoopGroup();
@PostConstruct
public void start() {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss, work).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port))
// .option(ChannelOption.SO_BACKLOG, 1024)
.handler(new LoggingHandler(LogLevel.INFO)).childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.TCP_NODELAY, true).childHandler(new ServerChannelInit());
try {
ChannelFuture future = bootstrap.bind().sync();
if (future.isSuccess()) {
log.info("Netty Server Started!");
}
} catch (InterruptedException ie) {
log.error("Error Initializing Netty Server. Error: " + ie.getMessage());
}
}
@PreDestroy
public void destroy() throws InterruptedException {
boss.shutdownGracefully().sync();
work.shutdownGracefully().sync();
log.info("Netty Server Shut Down!");
}
Run Code Online (Sandbox Code Playgroud)
和:
public class ServerChannelInit extends ChannelInitializer<SocketChannel>{
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("mainHandler", new ServiceHandler());
}
Run Code Online (Sandbox Code Playgroud)
和:
package com.test.netty;
@Component
public class ServiceHandler extends ChannelInboundHandlerAdapter {
private SomeEntity en;
@Autowired
SomeRepository sr;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// Read data and persist some entitys using injected repository
Run Code Online (Sandbox Code Playgroud)
和存储库:
package com.test.jpa;
//@Repository
public interface SomeRepository extends JpaRepository<SomeEntity, BigInteger> {
}
Run Code Online (Sandbox Code Playgroud)
问题是:存储库没有注入到 com.test.netty 类中。我在根类和 JUnit 测试中使用它没有任何问题。我将 @Repository 添加到存储库,并在 @EnableJPARepositories 中添加了存储库包,但没有任何变化。
有任何想法吗?
好吧,如果您要创建自己的实例ServiceHandler而不是使用 Spring 为您创建的 bean 实例,那么当然不会执行依赖注入。您需要将ServiceHandlerbean注入ServerChannelInit并创建ServerChannelInit它@Component本身:
@Component
public class ServerChannelInit extends ChannelInitializer<SocketChannel>{
private final ServiceHandler handler;
public ServerChannelInit(ServiceHandler handler) {
this.handler = handler;
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("mainHandler", handler);
}
...
}
Run Code Online (Sandbox Code Playgroud)
然后ServerChannelInit注入NettyServer:
@Service
@Slf4j
public class NettyServer {
private final ServerChannelInit channelInit;
public NettyServer(ServerChannelInit channelInit) {
this.channelInit = channelInit;
}
private EventLoopGroup boss = new NioEventLoopGroup();
private EventLoopGroup work = new NioEventLoopGroup();
@PostConstruct
public void start() {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss, work).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port))
// .option(ChannelOption.SO_BACKLOG, 1024)
.handler(new LoggingHandler(LogLevel.INFO)).childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.TCP_NODELAY, true).childHandler(channelInit);
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
636 次 |
| 最近记录: |