SpringBoot
入门
配置文件
起步依赖
自动配置
自定义starter
SpringBoot是Spring提供的一个子项目,用于快速构建Spring应用程序
Spring Framework 核心功能
Spring Data 数据获取
Spring AMQP 消息传递
Spring Security 认证授权
Spring Cloud 服务治理
传统方式构建spring应用程序,
缺点
导入依赖繁琐
项目配置繁琐
web.xml 、applicationContext.xml、SpringMVC.xml
SpringBoot 项目构建
特性
1、起步依赖
本质上就是一个Maven坐标,整合了完成一个功能需要的所有坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
相当把web开发的所有坐标都引入了
2、自动配置
遵循约定大于配置的原则,在boot程序启动后,一些bean对象会自动注入到ioc容器,不需要手动声明,简化开发。
3、可以快速构建项目,还有其他特性
内嵌Tomcat 、Jetty 无需部署War文件
外部化配置
不需要XML配置(properties/yml)
SpringBoot入门
只需要引入一个web依赖,就相当把刚才的所有包都引入了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1、创建Maven工程
2、导入spring-boot-starter-web起步依赖
3、编写controller
4、提供启动类
yml配置文件的书写和获取
@Value(“${键名}”) 第一种方式读取SpringBoot的配置文件,
在实体类里的每一个属性上都加一个注解。
使用@ConfigurationProperties(prefix=“前缀”)
实体类里的属性名要与配置项里的字段名字一致,springboot会自动帮我们取配置项里的字段值
Bean扫描
Bean注册
注册条件
自动配置原理
自定义starter
Bean对象的管理
Bean扫描
在spring的时候使用了
标签:<context:component-scan base-package=“com.mall”/>
注解:@ComponentScan(basePackages=“com.mall”)
@SpringBootApplication注解 下面包含三个子注解
@ComponentScan 这个注解,相当在启动类上添加了这个注解,不需手动添加了,可以自动扫描的Controller、Service,不指定包路径,默认扫描添加的这个类的包及子包,SpringBoot默认扫描启动类所在的包及其子包
Bean的注册
@Component
@Controller
@Service
@Repository
如果要注册的bean对象来自第三方(不是自定义的),是无法用@Component及衍生注解声明Bean的
可以使用
@Bean
@Import
如果要注册第三方bean,建议在配置类中集中注册
@Configuration
public class CommonConfig{
//对象默认的名字是:方法名
//如果方法的内部需要使用到ioc容器中已经存在的bean对象,那么只需要在方法上声明即可,spring会自动的注入。
@Bean
public Country country(){
return new Country();
}
}
@Import
导入 配置类
导入 ImportSelector 接口实现类
自定义组合注解,然后在springboot启动类注解下面直接使用@EnableCommonConfig
SpringBoot的自动配置原理
自动配置-源码分析
程序引入spring-boot-starter-web起步依赖,启动后,会自动往ioc容器中注入DispatcherServlet,引入这个包会自动注DispatcherServlet
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在启动类里获取这个bean,就可以获取到
@SpringBootApplication
public class SpringbootAutoConfigApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringbootAutoConfigApplication.class, args);
System.out.println(context.getBean("dispatcherServlet"));
}
}
@SpringBootConfiguration
@EnableAutoConfiguration
在SpringBoot2.7之前读取的配置文件名字是spring.factories,现在SpringBoot2.7之后的读取的配置文件名字是imports,2.7-3.0两个配置文件都是兼容的,在3.0以后的话只读取imports
这个类上有一个注解,@ConditionalOnClass含义是如果存在DispatcherServlet.class这个类,就生效,会自动配置,当引入了starter-web坐标,就会引入DispatcherServlet这个类,所以就自动注入到IOC中了。
自动配置的原理:就是提供一个自动配置类,然后把类名写到指定的配置文件中。
@ComponentScan 扫描这个类所在的包和它的子包
1、在主启动类上添加了SpringBootApplication注解,这个注解组合了EnableAutoConfiguration注解
2、EnableAutoConfiguration注解又组合了Import注解,导入了AutoConfigurationImportSelector类
3、实现selectImports方法,这个方法经过层层调用,最终会读取META-INF 目录下的 后缀名 为imorts的文件,boot2.7以前的版本,读取的是spring.factories文件
4、读取到全类名了之后,会解析注册条件,也就是@Conditional及其衍生注解,把满足注册条件的Bean对象自动注入到IOC容器中。
自定义starter
自动配置功能 xxxx autoconfigure
依赖管理功能 xxxx starter
自定义mybatis 的starter
1、创建dmybatis-spring-boot-autoconfigure模块,提供自动配置功能,并自定义配置文件META-INF/spring/xxx.imports
2、创建dmybatis-spring-boot-starter模块,在starter中引入自动配置模块。
将来别人要使用的话,就直接引入starter这个工程。
@AutoConfiguration//表示当前类是一个自动配置类
public class MyBatisAutoConfig {
//SqlSessionFactoryBean
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
//MapperScannerConfigure
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(BeanFactory beanFactory){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
//扫描的包:启动类所在的包及其子包
List<String> packages = AutoConfigurationPackages.get(beanFactory);
String p = packages.get(0);
mapperScannerConfigurer.setBasePackage(p);
//扫描的注解
mapperScannerConfigurer.setAnnotationClass(Mapper.class);
return mapperScannerConfigurer;
}
}