SpringBoot核心

Spring boot核心

1.Spring Boot的基本配置

1.1 启动类和核心注解@SpringBootApplication

Spring Boot应用通常都有一个名为*Application的程序入口类,该入口类需要使用Spring Boot的核心注解@SpringBootApplication标注伪应用的启动类。

在该启动类下,有一个标准的Java应用程序的main方法,在main方法中通过“SpringApplication.run(* Application.class,args)”启动Spring Boot应用。

demo如下:

package com.test.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootDemoApplication {

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

}

Spring Boot的核心注解@SpringBootApplication是一个组合注解,主要组合了@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan注解

  1. @SpringBootConfiguration注解

    是Spring Boot应用的配置注解,用它替代@Configuration注解

  2. @EnableAutoConfiguration注解

    可以让Spring Boot根据当前应用项目所依赖的jar自动配置项目的相关配置。

  3. @ComponentScan注解

    该注解的功能是让Spring Boot自动扫描@SpringBootApplication所在类的同级包以及它的子包中的配置,所以一般将入口类放在项目包下

1.2 关闭某个特定的自动配置

如果开发者不需要Spring Boot的某一项自动配置,可以使用@SpringBootApplication的exclude参数关闭特定的自动配置

@SpringBootApplication(exclude={***Configuration.class})

1.3 定制Banner

一般启动Spring Boot应用时,可以在控制台看到默认的启动图案,如何希望看到自己指定的启动信息,可以进行下列操作

  1. 在src/main/resources/下新建banner.txt,并添加任意字符串内容
  2. 打开网页https://patorjk.com/software/taag,输入自定义的字符串,然后点击网页中的select &copy,然后复制到bannner.txt中

关闭banner

在application.properties中添加下面配置

spring.main.banner - mode=off

1.4 Spring Boot的全局配置文件

Spring Boot的全局配置文件(application.properties或application.yml)可以放置在Spring Boot项目的src/main/resources目录下或者类路径的/config目录下

1.4.1 设置端口号

server.port=8888

1.4.2 设置web应用的上下文路径

server.servlet.context - path=/xxx

这个时候通过https://localhost:8080/xxx/hello访问下面控制器类中的请求处理方法

@RequestsMapping("/hello")
public String index(){
……
}
1.4.3 配置文档

在Spring Boot的全局配置文件中,可以配置和修改多个参数,更具体的可以查看官方文档说明

1.5 Spring Boot的Starters

Spring Boot提供了很多开箱即用的Starters,只要使用了所需要的Starters就可以自动关联项目开发所需要的相关依赖

1.6 读取应用配置

Spring Boot提供了3种方式读取项目的application.properties配置文件的内容。这三种方式分别为Environment类、@Value注解以及

@ConfigurationProperties注解

1.6.1 Environment

Environment是一个通用的读取应用程序运行时的环境变量的类,可以通过key-value的方式读取application.properties、命令行输入参数、系统属性、操作系统环境变量等。

image-20210729131009955

package com.test.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ReadConfigController {
    @Autowired
    private Environment env;
    @RequestMapping("/testEnv")
    public String testEnv(){
        return "用Environment读取配置文件中test.msg的值为"+env.getProperty("test.msg");
    }
}

image-20210729131034143

1.6.2 @Value

使用@Value注解读取配置文件内容十分简单

package com.test.demo.controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ValueReadConfigController {
    @Value("${test.msg}")
    private String msg;
    @RequestMapping("/testValue")
    public String testvalue(){
        return "用@Value读取配置文件中test.msg的值为"+msg;
    }
}

image-20210729165758768

1.6.3 @ConfigurationProperties

使用@ConfigurationProperties首先建立配置文件与对象的映射关系,然后在控制器方法中使用@Autowired注解将对象写入

实体类

package com.test.demo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="test")
public class msgProperties {
    private String msg;

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    @Override
    public String toString(){
        return "用@ConfigurationProperties读取配置文件中test.msg的值为"+msg;
    }


}

controller

package com.test.demo.controller;


import com.test.demo.msgProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigurationPropertiesController {
    @Autowired
    msgProperties msgp;
    @RequestMapping("/testConfigurationProperties")
    public String test(){
        return msgp.toString();
    }
}

image-20210729172939174

1.6.4 @PropertySource

如果我们想读取其他配置文件而不是全局配置文件,可以用PropertySource

image-20210729175414718

image-20210729175428008

package com.test.demo.controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@PropertySource({"test.properties","ok.properties"})
public class PropertySourceValueReaderOtherController {
    @Value("${test.msg}")
    private String testmsg;
    @Value("${ok.msg}")
    private String okmsg;

    @RequestMapping("/testProperty")
    public String testProperty(){
        return "其他配置文件test.properties:"+testmsg+"<br>"+"ok.properties:"+okmsg;
    }
}

image-20210729175541089

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shelgi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值