spring boot学习之web开发

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值