栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【java框架之Spring框架之SpringBoot框架】SpringBoot是什么?如何搭建?

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【java框架之Spring框架之SpringBoot框架】SpringBoot是什么?如何搭建?

【java框架之Spring框架】创建ssm框架项目详细步骤之 上(spring集成mybatis)

【java框架之Spring框架】创建ssm框架项目详细步骤之下(搭建SpringMVC)

文章目录

SpringBoot

SpringBoot是什么?SpringBoot核心功能springboot搭建

配置文件两种格式springboot集成JDBCspringboot集成mybatisspringboot配置拦截器

SpringBoot

回顾spring :

优点:

    轻量级的、非侵入式的、IOC和AOP一站式框架 ( jdbc事物管理 、springmvc的web层封装)简化企业级开发

缺点:

    spring配置却是重量级的

    有许多模板化配置,比如开启注解扫描…

    配置的jar的依赖过多

SpringBoot是什么?

在spring基础上进行封装

spring搭建比较麻烦: 配置多、依赖过多

springboot 对spring项目的搭建进行简化

基于"约定"优于配置的思想

搭建更加简化,配置不需要xml,使用其他方式进行配置,不需要在java和xml之间进行切换

SpringBoot核心功能

起步依赖 会自动依赖框架中能够使用到的第三方Jar

自动配置 明天再说

springboot搭建

和前端对等开发方式

方式一: 去官网在线生成下载 https://start.spring.io/

方式二: 在idea中创建

springboot中内置了tomcat

配置文件两种格式

①application.properties

键= 值

②application.yml

一级和一级之间要有缩进

访问:http://127.0.0.1:9999/loginCtl/login (类路径/方法路径)访问springboot项目不需要加项目名,因为springboot项目中,内嵌了专属的服务器,所以9999端口的服务器只有一个项目

springboot集成JDBC

springboot添加jdbc模块,默认使用自己框架爱内的数据源管理

可以把数据源的管理类配置为阿里的 德鲁一 数据源

    导入 jar 包 pom.xml

    		
    		
    			org.springframework.boot
    			spring-boot-starter-jdbc
    		
    		
    		
    			mysql
    			mysql-connector-java
    			8.0.28
    		
    		
    		
    			com.alibaba
    			druid
    			1.1.10
    		
    

    配置数据源链接

    application.yml

#为内置的tomcat配置信息 port配置端口号  冒号后面必须有空格,层级之间必须有缩进
server:
  port: 9999

#spiring配置
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/web_db?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

package config;



import javax.sql.DataSource;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;


@Configuration  //也是用来创建对象的,表示此类是配置类
public class DruidDataSourceConfig {

      
       @Bean  //只能用在方法上,表示此方法会返回一个对象
      @ConfigurationProperties(prefix = "spring.datasource")  //读取application.yml文件汇总spring.datasource下面的内容
       public DataSource druid() {
          DruidDataSource druidDataSource = new DruidDataSource();
          //druidDataSource.setUrl("");
           return new DruidDataSource();
       } 
}

package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlbasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.Collections;
@Configuration
public class CorsConfig {

    
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //1,允许任何来源
        corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
        //2,允许任何请求头
        corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
        //3,允许任何方法
        corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
        //4,允许凭证
        corsConfiguration.setAllowCredentials(true);

        UrlbasedCorsConfigurationSource source = new UrlbasedCorsConfigurationSource();
        source.registerCorsConfiguration("
@Configuration
public class IsLoginInterceptor implements HandlerInterceptor {

    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("后端验证用户登录是否有效");
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        if(user!=null){
            return true;
        }else{
            response.getWriter().print(5);//表示后端登录失效
            return false;//不继续向下执行
        }
    }
}
package com.ffyc.ssmback.config;

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

@Configuration
public class InterceptorConfig  implements WebMvcConfigurer {

    public void addInterceptors(InterceptorRegistry registry){
        InterceptorRegistration inter = registry.addInterceptor(new IsLoginInterceptor());
        inter.addPathPatterns("**");//放行swagger
//        inter.excludePathPatterns("/v2/**");//放行swagger
//        inter.excludePathPatterns("/webjars/**");//放行swagger
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/770910.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号