springboot 部分知識點
https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/htmlsingle/#getting-started-first-application-annotations
springboot
parent
spring-boot-starter-parent 包含了默認的一些設置,如編譯級別,字符編碼,依賴管理等等。
dependencies
spring-boot-starter-web 包含了web容器tomcat
spring-boot-starter-test 測試用
spring-boot-devtools 熱加載LiveReload 自動重啓動
spring-boot-maven-plugin
If you don’t want to use @SpringBootApplication,
the @EnableAutoConfiguration and @ComponentScan annotations that it imports defines
that behaviour so you can also use that instead.
You need not put all your @Configuration into a single class.
The @Import annotation can be used to import additional configuration classes.
Alternatively, you can use @ComponentScan to automatically pick up all Spring components,
including @Configuration classes.
Spring Beans and Dependency Injection
@ComponentScan 可以自動加載下列組件
@Component, @Service, @Repository, @Controller etc
@SpringBootApplication annotation
make it possible that use auto-configuration, component scan and be able to define extra configuration
The @SpringBootApplication annotation is equivalent to
using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.
@EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
@ComponentScan: enable @Component scan on the package where the application is located (see the best practices)
@Configuration: allow to register extra beans in the context or import additional configuration classes
@Import the user-defined beans are imported explicitly .
Property Defaults 例如緩存一些模板,記錄日誌等默認的屬性。都可以在application.properties文件中進行修改設置。
spring.devtools.add-properties to false in your application.properties
排除不需要重啓的
spring.devtools.restart.exclude=static/**,public/**
禁止重啓 可以在application.properties中添加spring.devtools.restart.enabled=false 或者在啓動類中添加下面一句。
System.setProperty("spring.devtools.restart.enabled", "false");
If you do not want to start the LiveReload server when your application runs,
you can set the spring.devtools.livereload.enabled property to false.
The context can be injected by implementing ApplicationContextAware or,
if the listener is a bean, by using @Autowired.
If you need to run some specific code once the SpringApplication has started,
you can implement the ApplicationRunner or CommandLineRunner interfaces
https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/htmlsingle/#getting-started-first-application-annotations
24. Externalized Configuration
Property values can be injected directly into your beans by using the @Value annotation,
accessed through Spring’s Environment abstraction,
or be bound to structured objects through @ConfigurationProperties
@Value("${name}")
In other words, if no profiles are explicitly activated,
then properties from application-default.properties are loaded.
Placeholders in Properties
app.description=${app.name} is a Spring Boot application
@ConfigurationProperties("acme")
@ConfigurationProperties(prefix="acme")
@Configuration
@EnableConfigurationProperties(AcmeProperties.class)
public class MyConfiguration {
}
@Component
@ConfigurationProperties(prefix="acme")
public class AcmeProperties {
// ... see the preceding example
}
@Validated 校驗 @NotNull @Positive
javax.validation.constraints.Positive;
javax.validation.constraints.NotNull;
@Profile 根據配置來選擇要激活使用哪個環境。
https://blog.csdn.net/wufaliang003/article/details/77662556
Any @Component or @Configuration can be marked with @Profile to limit when it is loaded
logback
http://logback.qos.ch
logging
Log Level: ERROR, WARN, INFO, DEBUG, or TRACE.
最佳教程
https://cloud.tencent.com/developer/article/1121242
color console output
https://www.cnblogs.com/gc65/p/9904096.html
spring mvc guide
https://docs.spring.io/spring/docs/5.1.7.RELEASE/spring-framework-reference/web.html#mvc
spring webflux guide
https://docs.spring.io/spring/docs/5.1.7.RELEASE/spring-framework-reference/web.html#webflux
在springboot的properties中已經包含了很多的默認配置 這些默認配置能夠幫我們完成大部分的配置,但是不能通過properties配置所有的bean,這個時候就需要Springboot中的@Configuration和@Bean來幫我完成了
@Configuration註解可以達到在Spring中使用xml配置文件的作用。
@Bean就等同於xml配置文件中的
@Configuration可理解爲用spring的時候的xml文件
@Bean可理解爲用spring的時候xml裏面的bean標籤
you can use the @JsonComponent annotation directly on JsonSerializer or JsonDeserializer implementations
All @JsonComponent beans in the ApplicationContext are automatically registered with Jackson
static content customize
spring.mvc.static-path-pattern=/resources/**
spring.resources.static-locations=/
Any resources with a path in /webjars/** are served from jar files if they are packaged in the Webjars format.
When you use one of these templating engines with the default configuration,
your templates are picked up automatically from src/main/resources/templates.
=============springboot2 error page============
https://www.baeldung.com/spring-boot-custom-error-page
error page
[1]resources\templates\error\404.html
[2]
@RestController
public class MyErrorController implements ErrorController {
public static final String PATH = "/error";
public String error() {
return "myError";
}
@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (statusCode == 401) {
return "/401";
} else if (statusCode == 404) {
return "/404";
} else if (statusCode == 403) {
return "/403";
} else {
return "/500";
}
}
@Override
public String getErrorPath() {
return PATH;
}
}
https://www.cnblogs.com/jpfss/p/8478644.html
[3]
2.1 html靜態頁面:在resources/public/error/ 下定義
如添加404頁面: resources/public/error/404.html頁面,中文注意頁面編碼
2.2 模板引擎頁面:在templates/error/下定義
如添加5xx頁面: templates/error/5xx.ftl
注:templates/error/ 這個的優先級比較 resources/public/error/高
================================
Adding both spring-boot-starter-web and spring-boot-starter-webflux modules
in your application results in Spring Boot auto-configuring Spring MVC, not WebFlux.
This behavior has been chosen because many Spring developers add spring-boot-starter-webflux
to their Spring MVC application to use the reactive WebClient.
You can still enforce your choice by setting the chosen application type to SpringApplication.
setWebApplicationType(WebApplicationType.REACTIVE).
==============jersey===============
https://www.jianshu.com/p/c14a9028e6e7
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
public interface ISomeService {
void sayHi(String msg);
}
import org.springframework.stereotype.Service;
import com.example.demo.services.ISomeService;
@Service
public class SomeServiceImpl implements ISomeService {
@Override
public void sayHi(String msg) {
System.out.println(msg);
}
}
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.demo.services.ISomeService;
@Component
@Path("resource")
public class SpringbootResource {
@Autowired
private ISomeService someService;
@Path("sayhi")
@GET
public String sayHi(@QueryParam("msg") String msg) {
this.someService.sayHi(msg);
return "success";
}
}
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
@Component
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(SpringbootResource.class);
}
}
test url:
http://localhost:8080/resource/sayhi?msg=wolfcode
ResourceConfig類型的@Bean
第二種方式,使用@Bean創建一個ResourceConfig類實例即可。
註釋掉上節中的JerseyConfig類,我們只需要修改一下Appication類(component類裏),添加方法:
@Bean
public ResourceConfig resourceConfig() {
ResourceConfig config = new ResourceConfig();
config.register(SpringbootResource.class);
return config;
}
想要配置Jersey的基礎路徑,就需要在application.properties文件中配置一個
spring.jersey.application-path=webapi
=======config container=========
Customizing Embedded Servlet Containers
Common servlet container settings can be configured by using Spring Environment properties.
Usually, you would define the properties in your application.properties file.
See the ServerProperties class for a complete list.
https://github.com/spring-projects/spring-boot/tree/v2.1.5.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java
If you need to programmatically configure your embedded servlet container,
you can register a Spring bean that implements the WebServerFactoryCustomizer interface
=================OAuth2 =============
https://www.javainuse.com/spring/spring-boot-oauth-authorization-code
31 Working with SQL Databases