SpringBoot知识点简单规整

文章目录

SpringBoot知识点简单规整

-----------------------------01、spring的注解---------------------

1、 @PropertySource

作用:加载*.properties,等价于context:property-placeholder/

属性:

value:指定*.properties文件的位置

2、 @Bean

作用:把实体bean放到ioc容器里,等于

属性:

value:指定IOC容器的key

3、 @ComponentScan

作用:指定spring要扫描的包,等价于<context:component-scan base-package="com.by.service">

属性:

value:指定要扫描的包

4、 @Import

作用:导入配置类,等价于

属性:

value:配置类的字节码

5、 @Configuration

作用:等价于,spring读取到该注解会创建一个ioc容器

-------------------------------02、Spring Boot入门---------------------------------

一、springboot介绍

springboot(spring+springmvc):不是对spring功能的增强,而是提供了一种快速开发spring应用的方式

特点:

简化xml配置

简化maven配置

内嵌tomcat

二、springboot项目搭建

1、创建maven工程,并继承springboot父工程

org.springframework.boot

spring-boot-starter-parent

2.3.2.RELEASE

2、添加启动器

org.springframework.boot

spring-boot-starter-web

3、启动类

package com.by;//(controller、service、mapper的上级目录)

复制代码
    @SpringbootApplication //标识当前类是springboot的启动类
    public class SpringbootHelloworldApp{
        pulic static void main(String[] args){
            SpringApplication.run(SpringbootHelloworldApp.class, agrs);
        }
    }

三、springboot的starter

1、starter是什么?

starter(启动器):是一堆依赖和配置类的集合

2、stareter的命名规范

官方:

前缀:spring-boot-starter-

规范:spring-boot-starter-模块名

举例:spring-boot-starter-web

第三方:

前缀:-spring-boot-starter

规范:模块名-spring-boot-starter

举例:mybatis-spring-boot-starter

四、springboot的配置文件

1、application.properties

server.port=9999

server.servlet.context-path=/springboot-helloworld

2、application.yml(树状接口)

server:

port: 9999

servlet:

context-path: /springboot-helloworld

yml语法:

①"."------->":"

②"="------->":空格"

③空格缩进

五、springboot的两种发布方式

1、jar方式

1)添加spring-boot-maven-plugin插件(自动检测main函数)

2)打jar包

3)java -jar xxx.jar

2、war方式

1)设置打包方式:war

2)设置tomcat启动器的依赖范围:provided

3)修改启动类(告诉tomcat启动类在哪)

public class SpringbootHelloworldApp extends SpringBootServletInitializer {

复制代码
            @Override
            protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
                return builder.sources(SpringbootHelloworldApp.class);
            }
        }
    4)打war包

-----------------------03、springboot整合junit-------------------

一、main方法启动spring

new ClasspathXmlApplicationContext("applicationContext.xml");

二、spring整合junit

//@RunWith(SpringJUnit4ClassRunner.class)

@RunWith(SpringRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

三、springboot整合junit

@RunWith(SpringJUnit4ClassRunner.class)

@SpringBootTest(classes={SpringbootJunit.class})

---------------------springboot整合全局异常处理器-------------------------

一、非ajax

public class GloableExceptionHandler implements HandlerExceptionResolver {

@Override

public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

ModelAndView mv = new ModelAndView();

if(e instanceof NullPointerException){

mv.setViewName("error1");

}else if(e instanceof ArithmeticException){

mv.setViewName("error2");

}

mv.addObject("msg", e.toString());

return mv;

}

}

二、ajax

@ControllerAdvice

public class AjaxGlobalExceptionHandler {

复制代码
    @ResponseBody
    @ExceptionHandler
    public Map errorHandler(Exception e){
        Map<String, Object> map = new HashMap<>();
        map.put("status", 500);
        map.put("msg", e.toString());
        return map;//{status:500, msg:异常信息}
    }
}

-----------------------04、springboot整合mybatis-----------------------

1、pom.xml

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.0.1

mysql

mysql-connector-java

5.1.47

com.alibaba

druid

1.0.9

org.springframework.boot

spring-boot-starter-test

2、application.yml

spring:

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3306/springboot

username: root

password: 1111

type: com.alibaba.druid.pool.DruidDataSource

3、App

@SpringBootApplication

@MapperScan("com.by.mapper") //设置mybatis扫描的包

public class SpringbootMybatisApp {

public static void main(String[] args) {

SpringApplication.run(SpringbootMybatisApp.class, args);

}

}

------------------------05、springboot的多环境配置--------------------------

1、多环境配置的语法

application-环境名称.yml

2、创建多套环境

application-dev.yml:

server:

port: 8090

application-test.yml:

server:

port: 8091

...

3、激活环境

application.yml:

spring:

profiles:

active: test

---------------------------------06、springboot自动配置原理----------------------------------------

@SpringBootApplication

@SpringBootConfiguration:标识启动类是一个IOC容器的配置类

@EnableAutoConfiguration:

@AutoConfigurationPackage:扫描启动类所在包及子包中所有的组件,生成实体bean并交给IOC容器管理

@Import({AutoConfigurationImportSelector.class}):会加载META-INF/spring.factories文件,并调用该文件中的自动配置类完成自动配置工作,

所以我们只需再application.yml中提供mysql的url、用户名、密码等信息即可完成mybatis的自动配置

@ComponentScan:配置springboot要扫描的包

相关推荐
互联网全栈架构15 分钟前
遨游Spring AI:第一盘菜Hello World
java·人工智能·后端·spring
优秀的颜1 小时前
计算机基础知识(第五篇)
java·开发语言·分布式
BillKu1 小时前
Java严格模式withResolverStyle解析日期错误及解决方案
java
网安INF1 小时前
ElGamal加密算法:离散对数难题的安全基石
java·网络安全·密码学
AWS官方合作商2 小时前
在CSDN发布AWS Proton解决方案:实现云原生应用的标准化部署
java·云原生·aws
gadiaola3 小时前
【JVM】Java虚拟机(二)——垃圾回收
java·jvm
coderSong25686 小时前
Java高级 |【实验八】springboot 使用Websocket
java·spring boot·后端·websocket
Mr_Air_Boy7 小时前
SpringBoot使用dynamic配置多数据源时使用@Transactional事务在非primary的数据源上遇到的问题
java·spring boot·后端
豆沙沙包?7 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
年老体衰按不动键盘8 小时前
快速部署和启动Vue3项目
java·javascript·vue