(1)导入打包插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
(2)打包
(3)运行
在该jar包目录下,打开控制台,输入命令:
java -jar 包名.jar
(1)导入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
(2)编译代码 Ctrl + F9
(3)配置IDEA的自动编译功能
点击OK即可。
按住ctrl +shift + alt + / -> 打开registry
勾选标记项。
application.yml:
school:
teacher:
name: laozhao
age: 30
tel: 666666
绑定配置对象
@Component
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
@Value("${school.teacher.name}")
private String name;
@Value("${school.teacher.age}")
private int age;
@Value("${school.teacher.tel}")
private String tel;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
", tel='" + tel + '\'' +
'}';
}
}
application.yml:
user:
name: lisi
age: 25
address: jiuyanqiao
绑定配置的对象
@Component
@ConfigurationProperties(prefix = "user")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String address;
}
application.yml:
spring:
profiles:
active: test #激活(选择)环境test
---
spring:
profiles: dev #指定环境名字dev
server:
port: 9999
---
spring:
profiles: test #指定环境名字test
server:
port: 8888
注意:jar包方式指定配置运行:java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
通过配置文件的名字来识别环境
application-dev.yml:
server:
port: 9999
application-test.yml:
server:
port: 8888
application.yml:
spring:
profiles:
active: test
#根据文件名字配置 application-dev.properties
private Logger logger = LoggerFactory.getLogger(MySpringBootTest.class);
...
logge.error("我是一个error日志.....");
logger.warn("我是一个warn日志.....");
logger.info("我是一个info日志.....");
logger.debug("我是一个debug日志.....");
logger.trace("我是一个trace日志.....");
(1)安装Lombok插件
安装完毕需要重启。
(2)打印日志
@Controller
@Slf4j
public class EmployeeController {
@RequestMapping("/")
@ResponseBody
String log() throws SQLException {
log.debug("调试信息。。。。。。。。。。。。。");
log.info("详细信息。。。。。。。。。。。。。。");
log.trace("全部信息。。。。。。。。。。。。。。");
log.warn("警告信息。。。。。。。。。。。。。。");
log.error("错误信息。。。。。。。。。。。。。。");
return "";
}
}
#logging.level.cn.itsource=error
#logging.file=my.txt
#logging.file.max-size=1KB
#logging.pattern.console="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"
<configuration debug="true" >
<property name="CONSOLE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} ----> [%thread] ---> %-5level %logger{50} - %msg%n"/>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}pattern>
<charset>UTF-8charset>
encoder>
appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/springboot.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/springboot-%d{yyyyMMdd}-%i.log.gzfileNamePattern>
<maxFileSize>1KBmaxFileSize>
<maxHistory>30maxHistory>
<totalSizeCap>5GBtotalSizeCap>
rollingPolicy>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}pattern>
<charset>UTF-8charset>
encoder>
appender>
<root level="info">
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
root>
<logger name="cn.itsource" level="debug" additivity="false">
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
logger>
configuration>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
resources/templates/hello.html:
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="webjars/jquery/3.4.1/jquery.js">script>
<script>
alert($);
script>
head>
<body>
<p th:text="'您好'+${username}">p>
body>
html>
/**
* thymeleaf测试
* @param model
* @return
*/
@RequestMapping("/thyme")
public String index(Model model){
model.addAttribute("username","老赵");
return "index";
}
启动服务,访问localhost:8080/thyme/
@EnableAutoConfiguration 开启自动配置功能,通过一个AutoConfigurationImportSelector导入选择器去扫描 spring-boot-autoconfigure-2.0.5.RELEASE.jar 自动配置包下面的 spring.factories 文件中的很多很多的自动配置的类
而:ThymeleafAutoConfiguration 是的Thymeleaf的自动配置 ,在这个自动配置类里面通过一个ThymeleafProperties去读取配置文件中的配置(也有默认配置) ,来自动配置Thymeleaf,比如Thymeleaf的视图解析器的自动配置如下:
@Bean
@ConditionalOnMissingBean(name = "thymeleafViewResolver")
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
...
return resolver;
}
定义拦截器:
@Component
public class MyInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("我是拦截器。。。。。。。。。");
return true;
}
}
在主配置类中注册拦截器:
/**
* 主配置类
*/
@SpringBootApplication
@MapperScan(basePackages = "cn.dxc.springboot.mapper")
public class ApplicationConfig implements WebMvcConfigurer{
@Autowired
private MyInterceptor myInterceptor;
/**
* 配置拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/login");
}
}
在主配置类编写视图解析器:
/**
* 配置试图解析器
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/temp").setViewName("/index");
}
当访问/temp就会定位到 templates/index.html