SpringBoot第三谈

Stella981
• 阅读 559

SpringBoot第三谈

本人是一名即将进入大三的物联网工程专业的学生,写博客即是为了记录自己的学习历程,又希望能够帮助到很多和自己一样处于起步阶段的萌新。
临渊羡鱼,不如退而结网。一起加油!
博客主页:https://blog.csdn.net/qq_44895397

Spring Boot 集成 Dubbo

1、开发 Dubbo 服务接口

创建普通 Maven 项目,dubbo 服务接口工程
SpringBoot第三谈
SpringBoot第三谈

创建一个接口:

SpringBoot第三谈

public interface StudentService {

    String getName(String name);
}

2、开发 Dubbo 服务提供者

创建 SpringBoot 框架的 WEB 项目

1)加入依赖

  1. Dubbo 集成 SpringBoot 框架起步依赖

    com.alibaba.spring.boot dubbo-spring-boot-starter 2.0.0
  2. Zookeeper 客户端依赖

    com.101tec zkclient 0.10
  3. mysql驱动

    mysql mysql-connector-java
  4. Mybatis整合spring boot的起步依赖

    org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0
  5. redis依赖

    org.springframework.boot spring-boot-starter-data-redis
  6. 接口工程

    com.yky.springboot springbootdemossminterface 1.0-SNAPSHOT

2)核心配置文件

  1. tomcat的配置

    #tomcat的配置 server.port=8090 server.servlet.context-path=/

  2. mysql的配置

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=yky

  3. redis的配置

    spring.redis.host=localhost spring.redis.port=6379 #spring.redis.password=yky #密码

  4. 指定mapper中映射文件存放的位置

    mybatis.mapper-locations=classpath:mapper/*.xml

  5. dubbo的配置

    #服务名称 spring.application.name=springbootdemossmprivoder #设置当前工程为服务提供者 spring.dubbo.server=true #设置注册中心 spring.dubbo.registry=zookeeper://localhost:2181

3)编写 Dubbo 的接口实现类

SpringBoot第三谈

@Component
@Service(interfaceName = "com.yky.springboot.service.StudentService",
        version = "1.0-SNAPSHOT",timeout = 15000)
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student queryStudentById(Integer id) {
        return studentMapper.selectByPrimaryKey(id);
    }
}

4)启动类上添加@MapperScan@EnableDubboConfiguration注解

  1. @MapperScan(basePackages = “com.yky.springboot.mapper”):扫描mapper文件

  2. @EnableDubboConfiguration:加开启 Dubbo 配置

    @SpringBootApplication @MapperScan(basePackages = "com.yky.springboot.mapper") @EnableDubboConfiguration public class SpringbootdemossmprivoderApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemossmprivoderApplication.class, args);
    }
    

    }

3、开发 Dubbo 服务消费者

SpringBoot第三谈

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.*</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include>
        </includes>
    </resource>
</resources>

1)添加依赖

  1. Dubbo 集成 SpringBoot 框架起步依赖

  2. Zookeeper 客户端依赖

  3. spring boot内嵌的jsp依赖,解析jsp页面

  4. 接口工程

    <!--Dubbo 集成 SpringBoot 框架起步依赖-->
    <dependency>
        <groupId>com.alibaba.spring.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>
    <!--Zookeeper 客户端依赖-->
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.10</version>
    </dependency>
    <!--spring boot内嵌的jsp依赖,解析jsp页面-->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <!--接口工程-->
    <dependency>
        <groupId>com.yky.springboot</groupId>
        <artifactId>springbootdemossminterface</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    

2)核心配置文件

  1. tomcat的配置

  2. 视图解析器

  3. dubbo服务消费者的配置

    #tomcat server.port=8080 server.servlet.context-path=/

    #视图解析器 spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsp

    #dubbo服务消费者的配置 #设置dubbo的配置 spring.application.name=springbootdemossmconsumer #设置注册中心 spring.dubbo.registry=zookeeper://localhost:2181

3)控制层的编写

SpringBoot第三谈

@Controller
public class StudentController {

    @Reference(interfaceName = "com.yky.springboot.service.StudentService",
            version = "1.0-SNAPSHOT", check = false)
    private StudentService service;

    @RequestMapping("/student/{stuid}")
    public ModelAndView selectStudent(@PathVariable("stuid") Integer id) {
        ModelAndView mv = new ModelAndView();
        Student student = service.queryStudentById(id);
        mv.addObject("student", student);
        mv.setViewName("studentjsp");
        return mv;
    }
}

4)启动类上添加@EnableDubboConfiguration

Spring Boot 非 web 应用程序

SpringBoot第三谈

方式一:直接在 main 方法中,根据 SpringApplication.run()方 法获取返回的 Spring 容器对象,再获取业务 bean 进行调用

  • SpringBoot 程序启动后,返回值是 ConfigurableApplicationContext,它也是一个
    Spring 容器对象

  • 它其它相当于原来 Spring 中启动容器

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("");

SpringBoot第三谈

方式二:Spring boot 的入口类实现 CommandLineRunner 接口

  1. 第一步:SpringBoot 的启动程序,会初始化 spring 容器
  2. 第二步:通过容器获取 bean,并注入给 userService
  3. 第三步:容器启动后调用 run 方法,在该方法中调用业务方法
    SpringBoot第三谈

SpringBoot 启动Logo的修改或关闭

关闭 SpringBoot Logo 图标及启动日志

@SpringBootApplication
public class Application {
     public static void main(String[] args) {
         SpringApplication springApplication = new SpringApplication(Application.class);
         //关闭启动 logo 的输出
         springApplication.setBannerMode(Banner.Mode.OFF);
         springApplication.run(args);
 } }

SpringBoot第三谈

修改启动的 logo 图标

在 src/main/resources 放入 banner.txt 文件

可以利用网站生成图标: https://www.bootschool.net/ascii

SpringBoot第三谈
SpringBoot第三谈
启动
SpringBoot第三谈

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
2个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
不温卜火 不温卜火
3年前
爬虫入门经典(五) | 简单一文教你如何爬取高德地图
大家好,我是不温卜火,是一名计算机学院大数据专业大三的学生,昵称来源于成语—不温不火,本意是希望自己性情温和。作为一名互联网行业的小白,博主写博客一方面是为了记录自己的学习过程,另一方面是总结自己所犯的错误希望能够帮助到很多和自己一样处于起步阶段的萌新。但由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教!暂时只在csdn这一个平台进行
不温卜火 不温卜火
3年前
爬虫入门经典(二十四) | 爬取当当网图书信息并进行数据清洗
大家好,我是不温卜火,是一名计算机学院大数据专业大三的学生,昵称来源于成语—不温不火,本意是希望自己性情温和。作为一名互联网行业的小白,博主写博客一方面是为了记录自己的学习过程,另一方面是总结自己所犯的错误希望能够帮助到很多和自己一样处于起步阶段的萌新。但由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教!暂时只在csdn这一个平台进行
不温卜火 不温卜火
3年前
爬虫入门经典(六) | 一文带你深入了解为什么使用代理IP及其如何使用
大家好,我是不温卜火,是一名计算机学院大数据专业大三的学生,昵称来源于成语—不温不火,本意是希望自己性情温和。作为一名互联网行业的小白,博主写博客一方面是为了记录自己的学习过程,另一方面是总结自己所犯的错误希望能够帮助到很多和自己一样处于起步阶段的萌新。但由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教!暂时只在csdn这一个平台进行
不温卜火 不温卜火
3年前
爬虫入门经典(四) | 如何爬取豆瓣电影Top250
大家好,我是不温卜火,是一名计算机学院大数据专业大三的学生,昵称来源于成语—不温不火,本意是希望自己性情温和。作为一名互联网行业的小白,博主写博客一方面是为了记录自己的学习过程,另一方面是总结自己所犯的错误希望能够帮助到很多和自己一样处于起步阶段的萌新。但由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教!暂时只在csdn这一个平台进行
Easter79 Easter79
2年前
SpringBoot第三谈
!在这里插入图片描述(https://static.oschina.net/uploads/img/202006/30142500_OL4H.png)本人是一名即将进入大三的物联网工程专业的学生,写博客即是为了记录自己的学习历程,又希望能够帮助到很多和自己一样处于起步阶段的萌新。临渊羡鱼,不如退而结网。一起加油!博客主页:h
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
8个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这