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
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