java游戏服务器开发之整合spring

匿名 (未验证) 提交于 2019-12-02 21:53:52
netty和spring的结合
上篇将netty大致部署好了,但是里面的配置我们都是写在代码里面,其实这样是不太好的,毕竟你改个参数就要打包一下。
所以打算写在配置文件中,顺便把spring也集成进来


第一步,在pom文件中加入spring的包,

<!-- spring框架包 -->


# code.debug=true port=8088 channelType=NIO protocolType=TCP
第三步,添加spring的配置文件ApplicationContext.xml,里面的各个标签都有加注解,应该挺容易阅读的

@Value("#{cfgProps['port']}")


/**  * Copyright (C), 2015-2018  * FileName: ServerConfig  * Author:   zhao  * Date:     2018/6/12 11:16  * Description: 服务的配置内容  * History:  * <author>          <time>          <version>          <desc>  * 作者姓名           修改时间           版本号              描述  */ package com.lizhaoblog.server.pojo;  import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component;  import javax.annotation.PostConstruct;  /**  * 〈一句话功能简述〉<br>  * 〈服务的配置内容〉  *  * @author zhao  * @date 2018/6/12 11:16  * @since 1.0.0  */ @Component @Scope("singleton") public class ServerConfig {   private static final Logger logger = LoggerFactory.getLogger(ServerConfig.class);    @Value("#{cfgProps['port']}")   private Integer port;   @Value("#{cfgProps['channelType']}")   private String channelType;   @Value("#{cfgProps['protocolType']}")   private String protocolType;    private ApplicationContext applicationContext;    private static ServerConfig instance = null;    private ServerConfig() {   }    public static ServerConfig getInstance() {     if (instance == null) {       instance = new ServerConfig();       logger.debug("ServerConfig is not init by spring");     }     return instance;   }    @PostConstruct   public void init() {     instance = this;     printServerInfo();   }    public void printServerInfo() {     logger.info("**************Server INFO******************");     logger.info("protocolType  : " + protocolType);     logger.info("port          : " + port);     logger.info("channelType   : " + channelType);     logger.info("**************Server INFO******************");   }    public Integer getPort() {     return port;   }    public void setPort(Integer port) {     this.port = port;   }    public String getChannelType() {     return channelType;   }    public void setChannelType(String channelType) {     this.channelType = channelType;   }    public String getProtocolType() {     return protocolType;   }    public void setProtocolType(String protocolType) {     this.protocolType = protocolType;   }    public ApplicationContext getApplicationContext() {     return applicationContext;   }    public void setApplicationContext(ApplicationContext applicationContext) {     this.applicationContext = applicationContext;   } }

这样spring就整合完成了,可以在各个地方使用 applicationContext.getBean("xxx")获取某些类了
而applicationContext对象我在ServerConfig之中保存了,需要
ServerConfig.getInstance().getApplicationContext().getBean("xxx");





上面的代码在码云上 https://gitee.com/lizhaoandroid/JgServer
后续代码添加,不好阅读的话,可以查看分支netty-factory
可以加qq群一起探讨Java游戏服务器开发的相关知识 676231524


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!