阅读背景:

用spring的InitializingBean的afterPropertiesSet初始化

来源:互联网 


实例

@Component
public class NettyServer implements InitializingBean{
	@Resource
	private ChannelInitializer<SocketChannel> childHandler;
	@Value("${netty.host}")
	private String host;
	@Value("${netty.port}")
	private int port;
	public void afterPropertiesSet() throws Exception {
		System.out.println("server afterPropertiesSet");
		EventLoopGroup parentGroup = new NioEventLoopGroup(Runtime.getRuntime()
	            .availableProcessors() * 2);
		EventLoopGroup childGroup = new NioEventLoopGroup(Runtime.getRuntime()
	            .availableProcessors() * 2);
		ServerBootstrap server = new ServerBootstrap();
		server.group(parentGroup, childGroup);
		server.channel(NioServerSocketChannel.class);
		server.childHandler(childHandler);
		server.bind(new InetSocketAddress(host, port)).sync();
	}
	public void setChildHandler(ChannelInitializer<SocketChannel> childHandler) {
		this.childHandler = childHandler;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public void setPort(int port) {
		this.port = port;
	}
	
	
}
@Component
public class NettyServer 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: