注解
@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