springboot实战(1)springboot基本配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wu2374633583/article/details/83275359

1 入口类和@SpringBootApplication

package com.wuk.springbootHello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication//开启自动配置
public class SpringbootHelloApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootHelloApplication.class, args);
	}
	
	@RequestMapping("/")
	public String index(){
		
		return "hello spring boot";
	}
}

http://127.0.0.1:8080/
运行结果:

hello spring boot

在这里插入图片描述

2 定制banner

1 定制自己的banner

在这里插入图片描述
图片定制地址:http://patorjk.com/software/taag

2 关闭banner

在这里插入图片描述

3 外部配置

springboot允许使用properties文件,yaml文件或者命令行参数作为外部配置。

1 命令行参数配置

在这里插入图片描述

2 常规性配置

application.properties

server.port=9090

book.author=wuk
book.name=spring boot
@RestController
@SpringBootApplication//开启自动配置
public class SpringbootHelloApplication {

	@Value("${book.author}")
	private String bookAuthor;
	
	@Value("${book.name}")
	private String bookName;
	
	public static void main(String[] args) {
		SpringApplication.run(SpringbootHelloApplication.class, args);
	}
	
	@RequestMapping("/")
	public String index(){
		
		return "bookAuthor="+bookAuthor+",bookName="+bookName;
	}
}

http://127.0.0.1:9090/
结果:

bookAuthor=wuk,bookName=spring boot

3 类型安全的配置(基于properties)

要注意 1.4版本的已经摒弃了@ConfigurationProperties的locations属性,那么解决办法是:

@Component//把普通pojo实例化到spring容器中
@ConfigurationProperties(prefix="author")//prefix="author"指定配置前缀 
@PropertySource("classpath:/config/my.properties")

案例如下:
my.properties

author.name=wuk
author.age=12

具体的实体类

package com.wuk.springbootHello;

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

@Component//把普通pojo实例化到spring容器中
@ConfigurationProperties(prefix="author")//prefix="author"指定配置前缀 
@PropertySource("classpath:/config/my.properties")
public class Author {

	private String name;
	private String age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
	
}

启动类

package com.wuk.springbootHello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication//开启自动配置
public class SpringbootHelloApplication {

	@Autowired
	private Author author;
	
	public static void main(String[] args) {
		SpringApplication.run(SpringbootHelloApplication.class, args);
	}
	
	@RequestMapping("/")
	public String index(){
		
		return "authorName="+author.getName();
	}
}

结果:
在这里插入图片描述

4 日志配置

市面上的日志框架;
JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j….

日志门面 (日志的抽象层) 
JCL(Jakarta Commons Logging) SLF4j(Simple Logging
Facade for Java) jboss-logging

日志实现
Log4j JUL(java.util.logging)
Log4j2 Logback

最佳组合
日志门面: SLF4J;
日志实现:Logback;

SpringBoot:
底层是Spring框架,Spring框架默认是用JCL;‘
SpringBoot选用 SLF4j和logback;

springboot日志的使用

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

注意:以后开发的时候,日志记录方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象层里面的方法; 给系统里面导入slf4j的jar和 logback的实现jar。

5 profile配置

在这里插入图片描述
prod为生产环境,dev为开发环境。
首先创建如下文件:
在这里插入图片描述
application-dev.properties

server.port=8888

application-prod.properties

server.port=80

application.properties

spring.profiles.active=prod  //表示当前的环境为生产环境 

运行结果:
在这里插入图片描述

springboot运行原理

1 查看springboot做了哪些自动配置

可以通过如下方式查看当前项目已启用和未启用的自动配置的报告:

(1)运行jar时候增加–debug参数

java  -jar xx.jar --debug 

(2)在application.properties中:

debug=true

猜你喜欢

转载自blog.csdn.net/wu2374633583/article/details/83275359