springboot知识点

一、静态属性

1、配置文件属性映射(完全映射,名称必须一致)

@Value(value="${config.name}")

2、有时候属性太多了,一个个绑定到属性字段上太累,官方提倡绑定一个对象的bean,这里我们建一个ConfigBean.java类,顶部需要使用注解@ConfigurationProperties(prefix = "com.dudu")来指明使用哪个

这里配置完还需要在spring Boot入口类加上@EnableConfigurationProperties并指明要加载哪个bean,如果不写ConfigBean.class,在bean类那边添加

复制代码
@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class Chapter2Application {

    public static void main(String[] args) {
        SpringApplication.run(Chapter2Application.class, args);
    }
}
复制代码

最后在Controller中引入ConfigBean使用即可,如下:

复制代码
@RestController
public class UserController {
    @Autowired
    ConfigBean configBean;

    @RequestMapping("/")
    public String hexo(){
        return configBean.getName()+configBean.getWant();
    }
}
复制代码

3、引入自己的属性文件

@Configuration
@ConfigurationProperties(prefix = "com.md") 可不完全匹配,可大小写,加-,例如Name
@PropertySource("classpath:test.properties")
public class ConfigTestBean {
    private String name;
    private String want;
    // 省略getter和setter
}

4、

配置文件的优先级


application.properties和application.yml文件可以放在一下四个位置:


  • 外置,在相对于应用程序运行目录的/congfig子目录里。
  • 外置,在应用程序运行的目录里
  • 内置,在config包内
  • 内置,在Classpath根目录

同样,这个列表按照优先级排序,也就是说,src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性,如图:


 
 

此外,如果你在相同优先级位置同时有application.properties和application.yml,那么application.yml里面的属性就会覆盖application.properties里的属性。

4、命令行参数

java -jar xx.jar --server.port=9090

可以看出,命令行中连续的两个减号--就是对application.properties中的属性值进行赋值的标识。
所以java -jar xx.jar --server.port=9090等价于在application.properties中添加属性server.port=9090
如果你怕命令行有风险,可以使用SpringApplication.setAddCommandLineProperties(false)禁用它。

5、springboot默认的静态资源路径

#spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources

registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

静态资源路径优先级

优先级顺序为:META/resources > resources > static > public

static、public、resources 等目录都在 classpath: 下面

6、spring热部署

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

热部署依赖   默认是true  改动重启

也可在配置文件中进行设置

spring.devtools.restart.enabled=false

//静态资源文件修改后不重启
spring.devtools.restart.exclude=/META-INF/maven/**,/META-INF/resources/**,/resources/**,/static/**,/templates/**,/public/**
##监听目录(可不设置)
#spring.devtools.restart.additional-paths=src/main/java

7、springboot项目打包成可执行jar

<build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <configuration>
                                <mainClass>com.tj.SpringBoot1Application</mainClass>
                            </configuration>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
    </build>

原文地址:https://www.cnblogs.com/jentary/p/11387450.html