註解
@RequestMapping("/user") 用於127.0.0.1:8080/user
@RequestBody 返回數據json
@Controller 用於標註控制端
@PathVariable("userId")String userId 用於127.0.0.1:8080/123 123代表userId
@Options(useGeneratedKeys = true, keyProperty = "userId")
@Select("select * from sys_user") @Results(id="User",value = { @Result(id=true,property = "userId", column = "user_id"), @Result(property = "userName", column = "user_name"), })
StringBoot web資源解析
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties {
可以設置spring.resources 靜態資源路徑 以及緩存
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
addResourceHandlers 是 WebMvcAutoConfiguration.class 中的一個函數 主要用於配置webjars
webjars配置是到classpath:/META-INF/resources/webjars/找資源路徑 比如 jsp html css 等
以jar包的方式引入靜態資源
可以參考https://www.webjars.org/ 以maven的方式把js jq等等配置到pom.xml項目裏
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.5.1</version> </dependency>
配置好後,在webjars下直接可以訪問到
如果請求沒人處理會到
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
靜態資源
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/**"
路徑下找
@Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) { WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern()); welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider)); welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations()); return welcomePageHandlerMapping; } private Optional<Resource> getWelcomePage() { String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations()); return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst(); } private Resource getIndexHtml(String location) { return this.resourceLoader.getResource(location + "index.html"); }
配置歡迎頁 登錄頁面或者主頁
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/**"
所有的index.html
FAVICON(new String[]{"/**/favicon.ico"}); //配置圖標 靜態資源路徑下favicon.ico
//配置圖標 靜態資源路徑下favicon.ico
SpringBoot自動配置原理
修改以及擴展
xxxAutoConfiguration: 幫我們給容器中自動配置組件
xxxProperties:配置類來封裝配置文件的內容
@Value("${xxx.xxx}")
//註解value的使用
//類必須交給spring來管理 類上加上@Component或其他註解
//要想在靜態方法中調用
private static String name;
@Value("${xxx.name}")
public void setAccessKeyId(String name) {
this.name= name;
}
//this 很重要
//下面靜態方法就可以引用了
public static String getName() {
return name;
}
//使用redis儲存session
spring.session.store-type=redis
//不使用redis儲存session
spring.session.store-type=none
//禁用security
security.basic.enabled = false