今天下载了《JavaEE开发的颠覆者SpringBoot实战》这本书,发现Spring还有好多遗漏的部分,算是又恶补了一下,今天主要是学习下SpringBoot的配置。

一、基本配置

1.定制Banner

(1).在src/main/resource下新建banner.txt

(2).打开http://patorjk.com/software/taag,输入要显示的文字,选择想要的样式,拷贝到banner.txt中,再次启动时就会发现banner已变。

(3)关闭banner

可以修改main,设置设置banner mode为OFF关闭banner。

package com.example.demo;import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class CuiywTestApplication {public static void main(String[] args) {//SpringApplication.run(CuiywTestApplication.class, args);SpringApplication app=new SpringApplication(CuiywTestApplication.class);app.setBannerMode(Banner.Mode.OFF);app.run(args);}
}

View Code

2.springboot配置文件

SpringBoot使用application.properties作为全局的配置文件,对一些默认配置的值进行修改。它不仅支持properties类型的文件还支持yml类型的文件.

server.port=8081
server.servlet.context-path=/cywtest

这里修改了启动的默认端口8080和默认context-path:/。看它启动日志也可以看出来发生了变化.

3.使用xml配置

虽然SpringBoot不提倡使用xml配置,但有时候也还是需要用的,这里我们在src/main/java下创建com.example.cywtest包,在包下创建一个@service HelloService,由于SpringBoot默认扫描的是CuiywTestApplication main方法对应的包以及子包,不会扫到com.example.cywtest包,我们在helloController注入一个该服务,然后启动,发现会报错,找不到该类。

***************************
APPLICATION FAILED TO START
***************************Description:Field helloService in com.example.demo.helloController required a bean of type 'com.example.cywtest.HelloService' that could not be found.Action:Consider defining a bean of type 'com.example.cywtest.HelloService' in your configuration.

View Code

(1).创建application-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--自定义配置bean--><bean id="helloService" class="com.example.cywtest.HelloService"></bean></beans>

View Code

(2).在main方法对应的包下创建配置文件引入bean,以便让SpringBoot能扫描到

package com.example.demo;import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;@Configuration
@ImportResource(locations = {"application-bean.xml"})
public class ConfigClass {}

View Code

这样再启动就不会报错了。

HelloService:这个类不在CuiywTestApplication对应的包下,SpringBoot默认扫描不到。

package com.example.cywtest;import org.springframework.stereotype.Service;@Service
public class HelloService {public HelloService(){System.out.println("使用XML进行配置的Service");}
}

View Code

ConfigClass:这里引入了application-bean.xml

package com.example.demo;import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;@Configuration
@ImportResource(locations = {"application-bean.xml"})
public class ConfigClass {}

View Code

helloController: 这里注入了HelloService.

package com.example.demo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;import com.example.cywtest.HelloService;@RestController
//@Controller
//@ResponseBody
@RequestMapping("/sbs")
public class helloController {@AutowiredHelloService helloService;@RequestMapping("/hello")public String Hello(){return "Hello World";}
}

View Code

这样通过上面的方法引入之后就能让SpringBoot扫描到了,再次启动也不会报错了。

二、外部配置

1.常规属性配置

在SpringBoot中只需在application.properties中定义,使用@Value注入即可。

Test.Name=cuiyw

package com.example.demo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;import com.example.cywtest.HelloService;@RestController
//@Controller
//@ResponseBody
@RequestMapping("/sbs")
public class helloController {@AutowiredHelloService helloService;@Value("${Test.Name}")private String Name;@RequestMapping("/hello")public String Hello(){return "Hello World,"+Name;}
}

View Code

2.基于properties类型安全的配置

SpringBoot提供了类型安全的配置方式,通过@ConfigurationProperties将Properties属性和一个Bean及其属性关联,从而实现类型安全的配置。

(1).在src/main/resource下建了一个test.properties的属性文件

person.Name=cyw
person.Age=18

(2).在com.example.demo包下创建了PersonSetting的类用来与test属性文件进行关联。

package com.example.demo;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;@Component
@PropertySource(value = "classpath:/test.properties")
@ConfigurationProperties(prefix="person")
public class PersonSetting {private String name;private Long age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Long getAge() {return age;}public void setAge(Long age) {this.age = age;}}

View Code

(3).在helloController注入PersonSetting

package com.example.demo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;import com.example.cywtest.HelloService;@RestController
//@Controller
//@ResponseBody
@RequestMapping("/sbs")
public class helloController {@AutowiredHelloService helloService;@Value("${Test.Name}")private String Name;@AutowiredPersonSetting personSetting;@RequestMapping("/hello")public String Hello(){return "Hello World,"+Name+"Person Name:"+personSetting.getName();}
}

View Code

(4).启动,在浏览器输入http://localhost:8081/cywtest/sbs/hello,可以看到获取到了属性文件中的name值。

三、Profile配置

在开发中可能会部署多个环境,每个环境部署的配置可能不一样.我们可以使用application.properties进行多个环境的配置,通过application-{profile}.properties来控制加载哪个环境的配置,将于环境无关的属性,放置到application.properties文件里面,通过spring.profiles.active=profiles的值,加载不同环境的配置,如果不指定,则默认加载application.properties的配置,不会加载带有profile的配置 。

(1).创建application-dev.properties开发和application-prod.properties生产属性文件,分别指定不同的port和context-path。

server.port=8082
server.servlet.context-path=/devtest

server.port=8083
server.servlet.context-path=/prodtest

(2)在application.properties设置环境 ,这里设置的是dev环境,那启动的端口就是8082.

spring.profiles.active=dev

四、小结

今天算是学了几个简单的配置,其实还有好多配置,比如日志等,这个一篇博客也写不完,在以后的博客中学习。

转载于:https://www.cnblogs.com/5ishare/p/8998373.html

SpringBoot入门之简单配置相关推荐

  1. springboot 入门二- 读取配置信息一

    在上篇入门中简单介绍下springboot启动使用了大量的默认配置,在实际开发过程中,经常需要启动多个服务,那端口如何手动修改呢? 此篇就是简单介绍相关的配置文件信息. Spring Boot允许外部 ...

  2. SpringBoot入门与常用配置

    目录 入门 常用配置 配置数据库连接池 MyBatisPuls开启驼峰映射 MyBatisPuls开启打印SQL 在springboot中设置过滤器 在springboot中设置监听器 设置自动填充 ...

  3. springboot+hibernate如何简单配置多个数据源

    前言: 1,业务需求:使用原生的hibernate连接数据库,数据来源是两个数据库,并且为了后期维护,需要将数据库账号密码重hibernate.cfg.xml中抽离出来,放到properties文件中 ...

  4. SpringBoot入门——Thymeleaf简单使用

    本文只展示代码实现,具体参考此博客实现 举例使用Thymeleaf的:赋值,拼接,if判断,unless判断,for 循环,HTML文本替换 IndexController后台代码 @Controll ...

  5. SpringBoot + Spring Security 简单入门

    这篇文章主要介绍了SpringBoot + Spring Security 简单入门 Spring Security 基本介绍 这里就不对Spring Security进行过多的介绍了,具体的可以参考 ...

  6. SpringBoot入门建站全系列(二十七)WebSocket做简单的聊天室

    SpringBoot入门建站全系列(二十七)WebSocket做简单的聊天室 一.概述 WebSocket 是一种网络通信协议.RFC6455 定义了它的通信标准. WebSocket 是 HTML5 ...

  7. SpringBoot入门和配置

    一.SpringBoot入门和微服务简介  1.SpringBoot入门 springboot:J2EE一站式解决方案 springcloud:分布式整体解决方案 2.微服务简介    微服务:架构风 ...

  8. 最简单最详细的SpringBoot入门项目——HelloWorld

    最详细的SpringBoot入门项目--HelloWorld 关于SpringBoot的介绍以及优点这里就不说了,想了解的可以直接点击这里 Spring Boot百度百科 接下来我们直奔主题,开始用S ...

  9. springboot profile_SpringBoot简单配置

    使用Spring Boot,配置工作将会变得更加简单,我们只需要在application.properties中定义属性,然后在代码中直接使用@Value注入即可. 如下: book.author=x ...

最新文章

  1. jQuery 表格自动增加
  2. Marshal在C#中的应用(void *指针到IntPtr的转化)
  3. 【渝粤题库】广东开放大学标准文献检索与应用 形成性考核
  4. githup用户名密码怎么看_MacBook Pro 开机密码忘记解决方法
  5. After Keying for mac(AE头发细节优化还原抠像脚本)v1.04
  6. RD与RT MPLS
  7. gbase里的分布表与复制表
  8. winserver2012安装mysql8.0.22需要安装vc++2015时报错0x80240017未指定错误
  9. Excel如何快速根据身份证号码计算周岁?
  10. 基于WebSocket的网页聊天室
  11. 计算机怎么设置定时音乐,怎么让电脑自动开机播放音乐教程 以及自动关机计划任务设置...
  12. python-flask 设置网页保留缓存静态文件时间
  13. 防线 2020/3/31
  14. 【爬坑记录】记录搭建fabric 遇到的问题-network e2ecli_default not found
  15. 开机要按F1的解决方法
  16. KubeVela 云原生时代的应用管理平台
  17. 我们用4行代码节省了100万 相见恨晚的PCDN
  18. PHP-FFMpeg 操作视频/音频文件
  19. RXD、TXD你接错了没?
  20. NUAA-编译原理-语法分析

热门文章

  1. pytorch Resnet
  2. python与线性代数 矩阵方程
  3. 中南民族大学计算机科学学院转专业,【通知】2018-2019学年学生转专业及专业分流工作...
  4. centos7进入单用户模式
  5. vSphere 7 With K8s系列-1~9 (微信公众号需要收费)
  6. 离线迁移服务(闪电立方) > 常见问题 > 数据迁移
  7. VMware vSAN 的内部版本号和版本 (2150753)
  8. 分布式SQL学习总结(1)——蚂蚁金服资深总监韩鸿源:像使用集中式数据库一样使用OceanBase分布式数据库
  9. Java基础学习总结(126)——Log4j2.xml生产环境实用配置
  10. Linux学习总结(23)——SSH协议详解