SpringBoot基本配置(二)

修改端口及加上项目名

在application.properties添加

#端口
server.port=9090
server.servlet.context-path=/boot

main方法启动,浏览器输入http://localhost:9090/boot测试
在这里插入图片描述

spring boot常规配置

在application.properties添加

#spring常规配置
user.author=zt
user.proname=springboot

测试类编写

package com.boot;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//控制器@RestController可以省略方法上@ResponseBody 
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})//springboot启动支持注解,exclude= {DataSourceAutoConfiguration.class}用于关闭特定的自动配置
public class SpringbootApplication {
	
	//自动获取properties中配置的user.author赋值于author
	@Value("${user.author}")
	private String author;
	//自动获取properties中配置的user.name赋值于name
	@Value("${user.proname}")
	private String proname;
	
	
	@RequestMapping("/")
	public String index() {
		return "测试获取的author="+author+"===proname="+proname;
	}

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);//springboot启动入口
	}

}

浏览器输入http://localhost:9090/boot测试结果
在这里插入图片描述

springboot配置文件自匹配实体类

创建User.java

package com.boot.entity;

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

@Component
@ConfigurationProperties(prefix="user")//通过@ConfigurationProperties自动加载properties,并根绝prefix适配properties前缀。
//@ConfigurationProperties(prefix="user",loactions={"classpath:config/user.properties"})loactions是指定加载config目录下的user.properties
public class User {
	
	private String author;
	private String proname;
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getProname() {
		return proname;
	}
	public void setProname(String proname) {
		this.proname = proname;
	}
	
}

创建测试控制器UserController

package com.boot.controller;

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

import com.boot.entity.User;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private User user;
	
	@RequestMapping("")
	public String getUser() {
		return "测试作者="+user.getAuthor()+"||测试名称="+user.getProname();
		
	}

}

启动后浏览器输入http://localhost:9090/boot/user 测试结果

在这里插入图片描述

日志配置

application.properties添加

#日志配置
#配置日志路径
logging.file=log/boot.log
#配置日志级别logging.level.包名=级别
logging.level.org.springframework.web=DEBUG

启动后
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值