springboot 单测加入参数_SpringBoot基础笔记

1、IDEA无法下载jar包、无法联网

​ 解决:pom.xml文件里加入阿里云搭建的国内镜像http://maven.aliyun.com,跑起来速度很快,可以进行配置 ​

<repositories>
        <repository>
            <id>nexus-aliyun</id>
            <name>nexus-aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
</repositories>

2、SpringBoot 404启动失败

  • 文件映射路径不对
  • 注解没加 @RequestMapping("/路径") 或 @Responsebody
  • 文件必须处在启动类所在包的子包下

3、想同时存在两个项目,IDEA默认一个窗口一个项目

​ 解决:在已有项目的前提下,如果还想新增其他项目

​ 通过New->Module新建

4、IDEA文件夹包名不能断开

小齿轮设置->Flatten Packages,即可。

5、SpringBoot配置文件

application.properyies:

server.port = 8080   更改Tomcat端口

application.yml

server:
    port: 8080

若设置冲突,优先级为:properyies > yml

6、SpringBoot引导注释处理器未配置

使用注解@ConfigurationProperties(prefix = “girl”)之后报错

解决办法:POM文件加入依赖

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

7、静态资源

默认静态资源加载路径:

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/

注意:这是SpringBoot2.0之前,2.0之后有所变动,暂时未知如何配置

默认欢迎页:index.html

默认网页logo:favicon.ico

在任意静态路径中使用即可,但名称要一样。

自定义静态资源:配置文件中声明:spring.resources.static-locations = classpath:/static

如果有多个,用逗号分割

设置服务项目名server.servlet.context-path = /demo

8、@ConfigurationProperties和@Value的区别

| spring| @ConfigurationProperties | @Value | 实例 | | -------------- | ------------------------ | ---------- | ------------------ | | 实现功能 | 批量注入值 | 单一注入值 | | | 松散语法 | 支持 | 不支持 | last-name=lastName | | SpEL | 不支持 | 支持 | #{10*2} | | 复杂类型封装 | 支持 | 不支持 | emp.map | | JSR303数据校验 | 支持 | 不支持 | |

二者优先级:@ConfigurationProperties > @Value

@Value获取配置文件的值:例如:@Value("${emp.age}")

9、数据校验(@Value不支持)

实体类加注解@Validated(开启JSR303数据校验)

属性加校验条件:例如验证邮箱:@Email

10、加载局部配置文件(出现问题,暂未解决!!!)

类路径下新建配置文件,例如:emp.properties

设置好对应属性的值

实体类加注解:@PropertySource(value = {"classpath:emp.properties"}, encoding = "utf-8")

11、导入xml配置文件

有些时候,我们不得不使用xml配置文件,使用方法:

  1. 类路径下新建xml配置文件springConf.xml,右键New-> XML Configuration File -> Spring Config ,进行自己想要的配置
  2. 启动类加注解:@ImportResource(Locations={"classpath:springCo
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值