SpringBoot入门之简单配置

今天下载了《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);

    }
}

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.

(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>

(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 {

}

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

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

package com.example.cywtest;

import org.springframework.stereotype.Service;

@Service
public class HelloService {
      public HelloService(){
            System.out.println("使用XML进行配置的Service");
        }
}

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 {

}

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 {
    @Autowired
    HelloService helloService;
    @RequestMapping("/hello")
    public String Hello(){

        return "Hello World";
    }
}

这样通过上面的方法引入之后就能让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 {
    @Autowired
    HelloService helloService;
    @Value("${Test.Name}")
    private String Name;
    @RequestMapping("/hello")
    public String Hello(){

        return "Hello World,"+Name;
    }
}

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;
    }

}

(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 {
    @Autowired
    HelloService helloService;
    @Value("${Test.Name}")
    private String Name;

    @Autowired
    PersonSetting personSetting;

    @RequestMapping("/hello")
    public String Hello(){

        return "Hello World,"+Name+"Person Name:"+personSetting.getName();
    }
}

(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

时间: 2024-07-30 21:26:40

SpringBoot入门之简单配置的相关文章

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

在上篇入门中简单介绍下springboot启动使用了大量的默认配置,在实际开发过程中,经常需要启动多个服务,那端口如何手动修改呢? 此篇就是简单介绍相关的配置文件信息. Spring Boot允许外部化你的配置,这样你就可以在不同的环境中使用相同的应用程序代码.你可以使用属性文件.YAML文件.环境变量和命令行参数来外部化配置.属性的值获取可以通过注解@Value . spring Environment或注解@ConfigurationProperties 这些方式优先级如下: @TestPr

springboot入门之简单demo

项目构建 我们采用maven构建SpringBoot工程,首先创建一个maven工程,对应的pom文件如下: <properties> <java.version>1.8</java.version> </properties> <!--集成springboot的父依赖--> <parent> <groupId>org.springframework.boot</groupId> <artifactId&

springboot 入门三- 读取配置信息二(读取属性文件方式)

在上篇文章中简单介绍自带读取方式.springboot提供多种方式来读取 一.@ConfigurationProperties(value="my") 支持更灵活的绑定及元数据的支持,但不支持spEL 定义appConfig接收(需要set get方法) @Component@ConfigurationProperties(value = "my")public class AppConfig {    private String name;    private

SpringBoot入门基础

SpringBoot入门 (一) HelloWorld 一 什么是springboot springboot是一个全新的框架,它设计的目的简化spring项目的初始环境的搭建和开发,主要有以下几个特点: 1.简化初始配置 ,可与主流框架集成: 2.内置Servlet容器,无需在打War包: 3.使用了Starter(启动器)管理依赖并版本控制: 4.大量的自动配置,简化开发,方便集成第三方: 5.提供准生产环境运行时的监控,如指标,健康,外部配置等: 6.无需XML配置,减少冗余代码 . 未使用

SpringBoot入门十九,简单文件上传

项目基本配置参考SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用myEclipse新建一个SpringBoot项目即可.现在来给项目添加一个MyBatis支持,添加方式非常简单,仅需两步即可,具体内容如下: 1. pom.xml添加以下配置信息 <!-- 文件上传配置开始 --> <!-- 9.引入commons-io依赖 --> <dependency> <groupId>commons-io</groupId

springboot入门简单,深入难

18年1月份的时候在腾讯课堂学习springboot.springcloud搭建微服务,老师告诉我们,springboot入门容易,深入难. 因为你必须东西SpringMVC.Spring.Mybatis这样的SSM,Redis.SpringData.Rabbitmq.KAFKA等各种各样的组件,然后怎么把它们整合到springboot里面去. 还得配置自动打包.编译,如Jkens,docker等才能将微服务很好的用起来,不然出现分布式的问题事务一致性问题.发布的问题,用微服务是非常痛苦的 原文

SpringBoot入门十八,自定义注解的简单实现

项目基本配置参考文章SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用myEclipse新建一个SpringBoot项目即可,此示例springboot升级为2.2.1版本. 1. pom.xml添加aop支持 <!-- 引入aop切面支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-

springBoot日志快速上手简单配置

默认配置 日志级别从低到高分为: TRACE < DEBUG < INFO < WARN < ERROR < FATAL. 如果设置为 INFO ,则低于 INFO 的信息都不会输出其他的依次类推 默认情况下,Spring Boot会用Logback来记录内部日志,并用INFO级别输出到控制台你不用做任何设置 从上图可以看到,日志输出内容元素具体如下: 时间日期:精确到毫秒 日志级别: 进程ID 分隔符:--- 标识实际日志的开始 线程名:方括号括起来(可能会截断控制台输出)

物联网架构成长之路(13)-SpringBoot入门

1. 前言 下载最新版的JavaEE eclipse-jee-oxygen-2-win32-x86_64.zip 安装STS插件 Window->Eclipse Marketplace -> popular 下那个 Spring Tools(aka Spring IDE and Spring Tool Suite) 然后通过STS工具创建一个新的Spring boot工程,这里就不细说了.网上资料很多,也比较简单就可以搭建起来.后面对SpringBoot也只是简单的提一下,还有说一下注意点.没