1. 通過(guò)DEBUG查看自動(dòng)配置的組件
在resources/application.properties中添加如下,開(kāi)啟DEBUG功能:
debug=true
然后啟動(dòng)springboot應(yīng)用,就可以看到DEBUG的日志。Positive表示生效,Negative表示不生效。如下所示:
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
-----------------
AopAutoConfiguration matched:
- @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)
AopAutoConfiguration.ClassProxyingConfiguration matched:
- @ConditionalOnMissingClass did not find unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)
- @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)
......省略部分......
Negative matches:
-----------------
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)
AopAutoConfiguration.AspectJAutoProxyingConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.aspectj.weaver.Advice' (OnClassCondition)
......省略部分......
Exclusions:
-----------
None
Unconditional classes:
----------------------
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
2. springboot啟動(dòng)圖標(biāo)修改
下載百度的logo到resouces目錄下,然后在resources/application.properties添加如下內(nèi)容:
spring.banner.image.location=classpath:baidu.png
最后啟動(dòng)springboot應(yīng)用。打印的日志如下,小圖標(biāo)已經(jīng)改成百度了
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ #@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8 @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@ &@@@@@@@@@@@@ :@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@: @@@@* @@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@ @@ @@ @@@ &@ @@
@@@@ @@@@@@@@@@@@@@@@@@@@@@ @@ @@@@@@@@@@@@ @@@@@@@ @ @@@@ @
@@@@@ @: @ @@@@@ @@ @@@@@o @@ @ @@@@ @
o@@@@@@@@8 @ @@@& @@#&@@ @@ @@ @@@o @@@@@@@ @@ @ *@
@@@@@ @ @ @@@ @@ @@ @@ @@ @@o @@ . 8
&&&& @ @@@@8 @ @@@ @@@@@ o@@@@@ @@@o @@@@@@@ @@ @@ *@@
@@@@ @ @@@@. @@@@o @@o @ *@@
2022-11-03 14:47:18.614 INFO 10600 --- [ main] com.hh.springbootTest.MyApplication : Starting MyApplication using Java 11.0.15
......省略部分......
也可以使用配置spring.main.banner-mode=off
,對(duì)banner進(jìn)行關(guān)閉
在線banner生成工具戳我
3. Lombok
Lombok是一個(gè)簡(jiǎn)化Java Bean開(kāi)發(fā)的小工具。自動(dòng)在編譯時(shí)生成getter、setter等方法
還有Slf4j的日志注解
在pom.xml添加依賴
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
然后IDEA安裝lombok插件(新版IDEA已集成)
lombok針對(duì)Java Bean的注解,常用的如下:
- @NoArgsConstructor:無(wú)參構(gòu)造器
- @AllArgsConstructor:全參構(gòu)造器
- @Getter:getter方法
- @Setter:setter方法
- @Data:等同于@NoArgsConstructor + @Getter + @Setter + @EqualsAndHashCode + @ToString
- @ToString:toString方法
- @EqualsAndHashCode:hashCode方法
如果構(gòu)造參數(shù)即不是無(wú)參構(gòu)造,也不是全參構(gòu)造,可以按常規(guī)方法定義構(gòu)造器
package com.hh.springbootTest.myBean;
import lombok.*;
// 如果構(gòu)造參數(shù)即不是無(wú)參構(gòu)造,也不是全參構(gòu)造,可以按常規(guī)方法定義構(gòu)造器
@NoArgsConstructor // 無(wú)參構(gòu)造器
@AllArgsConstructor // 全參構(gòu)造器
@Data // getter、setter方法
@ToString // toString方法
@EqualsAndHashCode // hashCode方法
public class User {
private String name;
}
lombok針對(duì)日志的注解有@Slf4j
可以看到可以直接使用全參構(gòu)造器,創(chuàng)建一個(gè)User類對(duì)象
package com.hh.springbootTest.myController;
import com.hh.springbootTest.myBean.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j // 日志
public class HelloController {
@RequestMapping("user")
public User userName1(@RequestParam("name") String name) {
log.info("======user的請(qǐng)求進(jìn)來(lái)了======");
// 可以直接使用全參構(gòu)造器
return new User(name);
}
}
訪問(wèn)http://localhost:8080/user?name=user1,效果如下:
同時(shí)也會(huì)打印如下的日志
......省略部分......
2022-11-03 15:12:18.809 INFO 31128 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-11-03 15:12:18.809 INFO 31128 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-11-03 15:12:18.810 INFO 31128 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2022-11-03 15:12:18.842 INFO 31128 --- [nio-8080-exec-1] c.h.s.myController.HelloController : ======user的請(qǐng)求進(jìn)來(lái)了======
4. devtools
在pom.xml添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
然后修改代碼,再在IDEA按CTRL + F9,進(jìn)行項(xiàng)目的重新編譯。devtools就會(huì)自動(dòng)重新進(jìn)行加載。修改就會(huì)自動(dòng)生效
不需要按CTRL + F9的,有付費(fèi)的Jrebel插件
5. 通過(guò)IDEA的Spring Initializr快速創(chuàng)建新項(xiàng)目
如下所示:
創(chuàng)建完項(xiàng)目,resources目錄下的static是放CSS、JS靜態(tài)資源的,templates是放html頁(yè)面的文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-610538.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-610538.html
到了這里,關(guān)于SpringBoot開(kāi)發(fā)小技巧使用(DEBUG、啟動(dòng)圖標(biāo)修改、Lombok、devtools、Spring Initializr)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!