1)创建项目,选择需要的模块
2)spring boot默认将需要的场景配置好了,只需要自己进行少量配置
3)编写业务代码
自动配置原理:
我们使用的场景spring boot帮我们做了什么?能不能修改?能改那些配置?能不能扩展
xxxxAutoConfiguration:帮我们给容器中自动配置组件,里面是一些写好的bean组件
xxxxProperties:配置类来封装配置文件的内容,相当于之前写的properties文件
spring boot对静态资源的加载的映射规则
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties {}
//可以设置和静态资源有关的参数,缓存时间等
//webmvcAutoConfiguration的内容
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));
}
}
}
四种方式
1)
所有的webjars/**,都去"classpath:/METAINF/resources/webjars/"找资源
webjars:以jar包的形式引入静态资源,访问的时候只需要写webjars下面的资源名称即可,比如:localhost:8080/webjars/jquery/3.3.1/jquery.js
从https://www.webjars.org/下载对应的mavenjar包
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
2)引入自己的资源文件使用方式----“/**”访问当前项目的任何资源
//staticPathPattern--“/**”
String staticPathPattern = this.webFluxProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
ResourceHandlerRegistration registration = registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations());
this.configureResourceCaching(registration);
this.customizeResourceHandlerRegistration(registration);
}
//在ResouceProperties类中
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
private String[] staticLocations;
private boolean addMappings;
private final ResourceProperties.Chain chain;
private final ResourceProperties.Cache cache;
public ResourceProperties() {
this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
staticLocations:会去这几个文件夹下找静态资源
“classpath:/META-INF/resources/”,
“classpath:/resources/”,
“classpath:/static/”,
“classpath:/public/”
“/”当前项目的根路径
localhost:8080/资源名称===会自动去resource中找对应名称
3)
欢迎页:静态资源下的所有html页面被“/**”映射
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders, ApplicationContext applicationContext, Optional<Resource> welcomePage, String staticPathPattern) {
if (welcomePage.isPresent() && "/**".equals(staticPathPattern)) {
logger.info("Adding welcome page: " + welcomePage.get());
this.setRootViewName("forward:index.html");
} else if (this.welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
logger.info("Adding welcome page template: index");
this.setRootViewName("index");
}
}
Notes:测试时主类上加上@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//去掉数据源
成功访问首页
4)所有的**/favicon.ico在静态资源文件夹下找,与普通的css等静态资源相同
以上均是使用spring boot写好的配置文件夹
可以自己在properties中自定义,如:
server.port=8081
spring.resources.static-locations=class:/com/lqh