小鸡炸

导航

SpringBoot基础使用

一、SpringBoot 介绍
  官网:https://start.spring.io/;阿里云服务器地址:https://start.aliyun.com/
spring boot是一套快速使用spring产品的便捷工具
特点:绝对没有代码生成并且对XML也没有配置要求、提供生产就绪型功能,如指标,健康检查和外部配置、尽可能自动配置Spring和第三方库、简化构建配置、嵌入的Tomcat,无需部署WAR文件、创建独立的Spring应用程序SpringApplication.run(...)默认会加载classpath下的application.yml或application.properties配置文件
二、SpringBoot版的Spring MVC
  1、新建Spring Starter Project、选war包,功能选择web、devtools两个功能模块
2、工程介绍
目录名
目录作用
src/main/resources/static
可被外部访问,等同于webapp目录、用来存放静态资源;所有访问的资源 默认从static路径访问、html也默认访问static、除非添加对应模板的jar包、才会默认访问templates
src/main/resources/templates
文件夹是受保护的、只允许被服务器访问,等同于web-inf目录<br>springboot推荐使用Thymeleaf 、Freemarker等模板、并不支持JSP模板<br>导入jar包后、会变成静态模板的默认路径
3、创建一个普通类、使用SpringMVC的注解@Controller、@RestController、@RequestMapping等注解
@RestController
@RequestMapping("/student")
public class StudentController {
    @RequestMapping("/show1")
    public Object show1() {
        return "show11111111";
    }
}
4、启动Springboot001Application类、SpringBoot项目默认集成了Tomcat的jar包,故无需依赖tomcat可直接运行
@SpringBootApplication(scanBasePackages = "包名")
public class Springboot001Application {
    public static void main(String[] args) {
        SpringApplication.run(Springboot001Application.class, args);
    }
}
三、自定义配置文件
我们都把配置文件写到application.yml中。有时我们不愿意把配置都写到application配置文件中,这时需要我们自定义配置文件,比如person.properties
1、student.properties
info.name=admin
info.pwd=123456
info.cinfo.name=一年一班
2、Student实体类配置
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

//加载属性文件
@PropertySource("classpath:/properties/student.properties")
//属性文件的配置
@ConfigurationProperties(prefix = "info")
@Component
public class Student {
    private int id;
    private String name;
    private String pwd;
    private String img;
    private int classid;
    private Classes cinfo;
    //get、set和构造函数省略
}
3、StudentController的配置
@RestController
@RequestMapping("/student")
public class StudentController {
    //如果已经在其他地方使用@PropertySource则此处可直接使用
    //否则需要在这个controller上面添加@PropertySource注解
    @Value("${info.name}")
    private String name;
    @Value("${info.pwd}")
    private String pwd;
    @Autowired
    Student info;
    @RequestMapping("/show1")
    public Object show1() {
        return info;
    }
    @RequestMapping("/show2")
    public Object show2() {
        return "2222"+this.name+","+this.pwd;
    }
}
四、过滤器的使用
1、使用过滤器、务必在启动类添加 @ServletComponentScan(basePackages = "包名")
mport java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/*")
public class CoreFiler implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request2 = (HttpServletRequest) request;
        HttpServletResponse response2 = (HttpServletResponse) response;

        // 添加参数,允许访问我的域名跨域--->request2.getHeader("Origin"):谁访问的我
        response2.setHeader("Access-Control-Allow-Origin", request2.getHeader("Origin"));
        // 允许客户端携带验证信息
        response2.setHeader("Access-Control-Allow-Credentials", "true");
        // 这个allow-headers要配为*,这样才能允许所有的请求头 ---
        response2.setHeader("Access-Control-Allow-Headers", "*");
        // 允许的请求方式
        response2.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
        // 复杂请求多少秒内、不需要预请求(复杂请求:在请求的head里面添加参数)
        // response2.setHeader("Access-Control-Max-Age", "100000");
        System.out.println("CoreFilter执行!!!!!");
        chain.doFilter(request2, response2);
    }
}
2、使用@CrossOrigin注解、@CrossOrigin既可使用在Controller
@RestController
@RequestMapping("/student")
//整个Controller的方法皆可跨域
@CrossOrigin
public class StudentController {
    
    //单个方法支持跨域
    @CrossOrigin
    @RequestMapping("/show1")
    public Object show1() {
        return "show1";
    }
}
3、使用SpringBoot的全局配置
@Configuration
@EnableWebMvc
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
        .allowedOriginPatterns("*")
        .allowCredentials(true)
        .allowedMethods("GET", "POST", "DELETE", "PUT")
        .maxAge(3600 * 24);
    }
} 
五、Linux后台启动SpringBoot项目命令
shell命令:    
nohup java -jar springbootssm-0.0.1-SNAPSHOT.jar > /log/springbootssm.out 2>&1 &
六、静态资源文件访问配置
# 映射到地址栏的路径设置 比如http://localhost:8080/aaa/1.jpg
spring.mvc.static-path-pattern=/aaa/**
# 实际的图片映射路径  实际访问的是 http://localhost:8080/image/1.jpg  
# 可以是本项目地址、亦可以是本地电脑的绝对路径地址、亦亦可是网络路径地址
spring.web.resources.static-locations=file:d://img/,classpath:image/,http://locahost:8080/img/

posted on 2022-01-19 10:20  小鸡炸  阅读(201)  评论(0编辑  收藏  举报