修改端口及加上项目名
在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
启动后