Springboot 第二讲 属性配置及访问静态资源

1、项目默认属性配置文件所在位置及配置实例

创建Spring Boot项目时,会默认生成一个全局配置文件application.properties(修改后缀为.yml)
【修改默认配置】在application.yml中操作

1.修改访问端口号
server:
#  设置访问端口号
  port: 8080
2.默认访问路径为/,如果需要修改访问路径,则需要在application.yml添加以下记录:
server:
#  设置访问端口号
  port: 8080
  servlet:
#    设置访问前缀
    context-path: /

补充:多环境配置文件 mavenprofile
实际开发中会有不同的开发环境,为了便于切换所以有以下操作

在这里插入图片描述
application-dev.yml
在这里插入图片描述
application-test.yml和application-pro.yml略 和dev写法一样
application.yml中的写法
在这里插入图片描述
active指明是哪个环境

2、自定义属性及读取

(1)在application.yml定义几个常量:
test_ip:
         1.181.1.1
test_port:
         9999
(2)编写Controller类读取自定义属性
public class HelloConfigController {
    
    
@Value("${test_ip}")	
private String test_ip;
@Value("${test_port}")	
private String test_port;

3、实体类属性赋值

在配置文件application.yml中添加代码

方式一:使用application.yml文件中配置信息 (两步)

在这里插入图片描述
注意:该配置文件的书写格式一定要按照规定,子属性要换行,至少两个空格,:后面至少一个空格
(1)、User类要加一个注解
@ConfigurationProperties(prefix=“userbody”)

@ConfigurationProperties(prefix="userbody")
public class User{
    
    
    private String name;
    private String password;
    private String birthday;
    private String mobile;
    private String address;
}

(2)、允许配置属性的类生效,在与启动类同包的任意类上加一个注解,建议加在Controller类上
@EnableConfigurationProperties(User.class)

@RestController
//允许配置属性的类生效
@EnableConfigurationProperties(User.class)
@RequestMapping("/demo")
public class demoController {
    
    
方式二:自定义配置文件

(1)、创建配置文件

在这里插入图片描述
文件内容:
注意:传中文时可能会出现乱码,所以可以转成unicode
在这里插入图片描述
(2.)、创建Dog类,在该类上需要加两个注解
//加载的自定义的配置文件
@Configuration
@ConfigurationProperties(prefix = “dog”)
@PropertySource(“classpath:myuser.properties”)

在这里插入图片描述
(3)、允许配置属性的类生效,在与启动类同包的任意类上加一个注解,建议加在Controller类上
@EnableConfigurationProperties(Dog.class)
当有多个类需要生效时的写法
@EnableConfigurationProperties({User.class,Dog.class})

4.访问静态资源

(1)、默认静态资源映射

Spring Boot 对静态资源映射提供了默认配置
Spring Boot 默认将 /** 所有访问映射到以下目录:
classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources
在这里插入图片描述
通过浏览器可以直接访问到
http://localhost:8080/1.jpg

(2)、自定义静态资源访问
第一种方式:配置类的方式

1.在本地创建一个文件夹,比如在D盘创建一个pic的文件夹存一个图片,图片名为1.jpg

2.创建一个配置类
在这里插入图片描述
代码

package com.offcn.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class staticConfig implements WebMvcConfigurer {
    
    
    @Override
    public voi
    d addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
        //自定义静态资源存储目录   file://表示本地
        registry.addResourceHandler("/pic/**").addResourceLocations("file:D:\\pic\\");
    }
}

(3)、刷新maven重启
(4)、通过地址访问图片
http://localhost:8080/pic/1.jpg

第二种方式:配置文件application.properties

(1)、在本地D盘创建一个文件夹mypath存一个图片,名为2.jpg
(2)、创建配置文件application.properties
在这里插入图片描述

#配置自定义静态资源访问目录
mypath=D:\\mypath\\
spring.resources.static-locations=classpath:/static/,classpath:/public/,classpath:/resources/,classpath:/META-INF/resources/,file:${mypath}

(3)、通过地址栏访问测试
http://localhost:8080/2.jpg

5.WebJars

概念:把静态资源打到jar包里面,相关Springboot项目通过引入jar包就可以实现静态资源引入
这里用一个Demo来讲解,更利于理解

(1)、首先创建文件夹及文件

在这里插入图片描述
在这里插入图片描述
通过地址访问测试页面可以访问到图片

(2)、新创一个maven项目,来生成静态资源包

在这里插入图片描述
在pom.xml添加代码

   <build>
        <resources>
            <resource>
                <!--打包目录-->
                <directory>${project.basedir}/src/main/resources/META-INF/resources/webjars</directory>
                <!--输出目录-->
                <targetPath>${project.build.outputDirectory}/META-INF/resources/webjars</targetPath>
            </resource>
        </resources>
    </build>

执行install操作,打包到本地仓库

(3)、删除

在这里插入图片描述

(4)、导入静态资源的依赖包

在这里插入图片描述
在这里插入图片描述

(5)、然后再启动执行去测试页面查看,发现图片还可以正常访问

Guess you like

Origin blog.csdn.net/JavaSupeMan/article/details/105660330