美文网首页
springboot

springboot

作者: luckee | 来源:发表于2019-03-02 14:53 被阅读0次

注意

springboot(或spring框架)不是只能用来做web项目,非web项目一样可以用

注解

  • @SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan
  • @RestController = @Controller + @ResponseBody,用于返回json格式
  • @ControllerAdvice,统一异常处理,@RestControllerAdvice=@ControllerAdvice+@ResponseBody

特点

  • 依赖中引入了某个功能的jar包(比如数据库),springboot就会去自动配置,所以就需要我们配置好数据库连接属性(url,username,password等),否则会报错

注意

  • 将对象以json的形式返回,该对象要有对应的get/set方法,否则会报无法转换的错误

springboot访问静态资源

需要配置下面两个属性(properties文件),如下是默认值,编译后,main/java和main/resources下的文件都会放到classes里面

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
spring.mvc.static-path-pattern=/**

或者通过代码自定义配置

package com.sam.demo.conf;

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

/**
 * 配置静态资源映射
 * @author sam
 * @since 2017/7/16
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

springboot使用Apache Commons的commons-fileupload

ServletFileUpload在解析request的时候得到空值,原因是springboot提供了默认的文件上传,所以要禁用掉springboot的文件上传,

spring:
  servlet:
    multipart:
      enabled: false

另外在使用getServletContext().getRealPath()的时候,通过调试发现是一个很奇怪的路径,不知道是不是因为springboot使用内置的Tomcat的原因;在使用Files的copy函数来把上传到文件保存到本地磁盘的时候也不成功

配置数据源

https://blog.csdn.net/pengjunlee/article/details/80081231

配置文件中的配置项

https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-documentation

配置属性的优先级

引用配置文件中的属性

https://www.cnblogs.com/zhaojinxin/p/7567500.html

配置文件中不同的类型的属性的写法

https://blog.csdn.net/qq_33524158/article/details/79600434

context-path一定要以/开头

冒号和属性值之间有个空格

k:(空格)v:表示一对键值对(空格必须有),以缩进来表示层级关系,只要是左对齐的一列数据,都是同一个层级的

相关文章

网友评论

      本文标题:springboot

      本文链接:https://www.haomeiwen.com/subject/qdweuqtx.html