配置

创建项目的时候选择

application.yml内容如下

  1. spring:
  2. redis:
  3. host: 127.0.0.1
  4. port: 6379
  5. database: 0
  6.  
  7. datasource:
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. url: jdbc:mysql://127.0.0.1:3306/alertremotecontrol?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=true
  10. username: root
  11. password: 123456
  12.  
  13. jpa:
  14. hibernate:
  15. ddl-auto: update
  16. show-sql: true
  17. user:
  18. redis_prefix: USERS_REDIS_PREFIX
  19. token_name: LOGIN_TOKEN

Redis

  1. @Autowired
  2. private StringRedisTemplate template;
  3. ....
  4. template.opsForValue().set("hello", para);
  5. template.expire("hello", 60, TimeUnit.SECONDS);

日志

每一个类添加成员

  1. private final Logger logger = LoggerFactory.getLogger(getClass());

然后记录的时候,logger.info("xxx"),logger.error("xxx")即可。

如果还要记录到文件,YML配置添加

  1. logging:
    file: ./logback.txt

netty

Springboot中集成Netty

Springboot主类实现 接口

  1. CommandLineRunner

在run方法中运行netty 。

Handler中注入Springboot对象,比如UserService

  1. public class UserHandler extends ChannelInboundHandlerAdapter {
  2. private static UserService userService;
  3. static {
  4. userService = SpringUtil.getBean(UserService.class);
  5. }
  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.context.ApplicationContextAware;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Component
  7. public class SpringUtil implements ApplicationContextAware {
  8. private static ApplicationContext applicationContext;
  9.  
  10. @Override
  11. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  12. if (SpringUtil.applicationContext == null) {
  13. SpringUtil.applicationContext=applicationContext;
  14. }
  15. }
  16.  
  17. public static ApplicationContext getApplicationContext() {
  18. return applicationContext;
  19. }
  20.  
  21. public static Object getBean(String name) {
  22. return getApplicationContext().getBean(name);
  23. }
  24.  
  25. public static <T> T getBean(Class<T> clazz) {
  26. return getApplicationContext().getBean(clazz);
  27. }
  28.  
  29. public static <T> T getBean(String name, Class<T> clazz) {
  30. return getApplicationContext().getBean(name, clazz);
  31. }
  32. }

SpringUtil的代码

异步执行

@SpringBootApplication注解下面加上

@EnableAsync注解

需要异步的方法上加上@Async注解

该类前面加上@Component注解

打包和运行

在下面 terminal中输入

  1. mvn package -DskipTests

或者在“Maven”窗口中先点击,表示不测试,然后然后双击package

在target目录中有jar包生成

运行

java -jar XYZ.jar

注意Win10控制台默认点击窗口会暂停程序,

此时需要右键窗口,点击“属性”,取消勾选“快速编辑模式”

springboot 极简使用例子: redis,MySql数据库,日志,netty,打包和运行的更多相关文章

  1. MySQL数据库日志文件(redo与undo)

    +++++++++++++++++++++++++++++++++++++++++++标题:MySQL数据库日志文件时间:2019年2月25日内容:MySQL数据库日志文件(redo日志和undo日志 ...

  2. Mysql数据库日志类型查询与配置详解

    在mysql中日志分为很多种,下面小编来给大家介绍Mysql数据库日志类型查询与使用,希望对各位同学会有所帮助 mysql常见的日志类型有五种:错误日志.二进制日志.查询日志.慢查日志和中继日志. 一 ...

  3. delphi+mysql做的图书管理系统,怎么把mysql数据库也一起打包进去?我用的是delphi的Express组件。

    sqlconnection,sqlquery1这些组件,我连接数据库的时候是用对象编辑器里的属性进行连接的,在sqlconnection中指定了字符集utf8,有些人做的方法是利用代码连接的数据库,如 ...

  4. 发布Rest风格的WebService的SpringBoot极简例子

    JDK:1.8.0_212 IDE:STS4(Spring Tool Suit4 Version: 4.3.2.RELEASE) 工程下载:https://files.cnblogs.com/file ...

  5. SpringBoot框架:使用mybatis连接mysql数据库完成数据访问(二)

    一.导入依赖包 1.在创建项目时勾选: 勾选SQL中的JDBC API.MyBatis Framework.MySQL Driver,创建项目后就会自动配置和引入这些包. 2.在pom.xml文件中添 ...

  6. Springboot+mybatis中整合过程访问Mysql数据库时报错

    报错原因如下:com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone.. 产生这个 ...

  7. SpringBoot 集成 Mybatis 使用 Druid数据源 MySQL数据库

    思路: 1.创建项目 项目结构如下: 2.导入相应包 POM.xml文件如下: <?xml version="1.0" encoding="UTF-8"? ...

  8. 《Springboot极简教程》问题解决:Springboot启动报错 Whitelabel Error Page: This application has no explicit mapping for(转)

    13.2 Spring Boot启动报错:Whitelabel Error Page 13.2 Spring Boot启动报错:Whitelabel Error Page 问题描述 Whitelabe ...

  9. Mysql 数据库日志与数据文件分开

    参考:https://blog.csdn.net/jiao_fuyou/article/details/78366621 myql默认配置情况下,二进制日志文件是保存在默认的数据目录 data 下,如 ...

随机推荐

  1. [人物存档]【AI少女】【捏脸数据】活泼少女

    AISChaF_20191028022750507.png

  2. Error from server (ServiceUnavailable): the server is currently unable to handle the request

    grep image /root/kubernetes-metrics-server/kubernetes-metrics-server/metrics-server-deployment.yaml ...

  3. poj 2718 Smallest Difference(暴力搜索+STL+DFS)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6493   Accepted: 17 ...

  4. [HNOI2004][bzoj1211] 树的计数(prufer序列)

    1211: [HNOI2004]树的计数 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3432  Solved: 1295[Submit][Stat ...

  5. codevs 2291 糖果堆 x

                         题目描述 Description [Shadow 1]第一题 WJMZBMR买了很多糖果,分成了N堆,排成一列.WJMZBMR说,如果Shadow能迅速求出第 ...

  6. 未关闭虚拟机直接关闭vmware引发的一系列问题——Windows下linux虚拟机

    虚拟机长时间挂起重新打开时卡顿,无法开启,脑抽直接关闭了vmware软件引起的一系列问题. 原因是关闭了vmware,但是相应的虚拟机并没有关闭,所以虚拟机不能重开 会出现如下提示 解决方案如下: 1 ...

  7. 「JOI 2019 Final」 硬币收藏

    题目链接 戳我 \(Solution\) 先将所有棋子移动到最近的目标点上 我们设两个变量\(ans1,ans2\)表示到目前为止这个点上可以移动棋子的数目,然后\(f[i][j]\)表示\((i,j ...

  8. 伪分布式下Hadoop3.2版本打不开localhost:50070,可以打开localhost:8088

    一.问题描述 伪分布式下Hadoop3.2版本打不开localhost:50070,可以打开localhost:8088 二.解决办法 Hadoop3.2版本namenode的默认端口配置已经更改为9 ...

  9. .NET DLL 保护措施详解(非混淆加密加壳)

    为什么要保护DLL,我就不多说了,各人有各人的理由.总的来说,就是不想核心逻辑泄露及授权验证被破解两大方面的因素.市面上的混淆加密工具对.NET源码保护的效果天差地别,很多网上下到的混淆工具破解版对. ...

  10. redux 第二部分

    redux 的使用方法, 为什么使用 action.js 文件,进行优化代码将其分开,然后我们通过工厂函数的每次返回不同的对象,由于参数是固定的,每次返回的都是事件类型和事件数据,所以我们可以使用一个 ...