SpringBOOt概念
SpringBoot提供了一种快速使用Spring的方式,基于约定大于配置的思想,让开发人员专注于逻辑业务,从而提高开发效率
SpringBoot快速入门
搭建SpringBoot工程,定义HelloChontroller.hello()方法,返回”Hello SpringBoot”
- SpringBoot在创建项目时,使用jar打包
- SpringBoot的引导类是整个项目的入口,运行main方法即可启动
- 使用SpringBoot和Spring构建的项目,业务代码方式完全一样
SpringBoot配置
SpringBoot是基于约定的,很多配置有默认值,可以使用application.properties和application.yaml(yml)进行自定义配置
- properties:
server.port=8080
- yml:
server:
port: 8080
- SpringBoot提供了2种配置文件类型:properties和yml/yaml
- 配置文件名称必须是application
- 在同一级目录下的优先级顺序:properties>yml>yaml
yaml是一种能够直观的被电脑识别的数据序列化格式,并且更容易被人类阅读,其文件的扩展名可以使用.yml或者.yaml
- yaml格式
server:
port: 8080
address: 127.0.0.1
简洁,以数据为核心
- 对象(map):键值对的集合
person:
name: zhangsan
#行内写法
person: {name: zhangsan}
address:
- beijing
- shanghai
行内写法
address: [beijing,shanghai]
- 纯量:单个值,不可再分
msg1: 'hello \n world' # 单引号忽略转义字符
msg2: "hello \n world" # 双引号识别转义字符
name: zhangsan
person:
name: ${name}
- @value
@Value("${name}")
private String name;
- Enviroment
@Autowired
private Environment environment;
System.out.println(environment.getProperty("person.name"));
- @ConfigurationProperties
@Component
@ConfigurationProperties(prefix = "person")
public class Person{}
SpringBoot开发的应用通常会被安装到不同的环境(开发、测试、生产),其中数据库地址、服务器端口等等配置需要在打包时进行修改。profile能够对配置环境进行动态切换。
- 多profile文件方式(创建多个profile文件如application-dev.properties/application-pro.properties/application-test.properties)
- yml多文档方式(利用
---
将文档分割)
- 配置文件
spring:
profiles:
active: dev #dev/pro/test
- 虚拟机参数:
-Dspring.profiles.active=test
- 命令行参数:
--spring.profiles.active=pro
SpringBoot程序启动时,会从以下为主加载配置文件:
SpringBoot整合其他框架
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes=启动类.class)
@SpringBootTest(classes = SpringBootStudyApplication.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void test(){
userService.add();
}
}
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>
spring:
redis:
host:127.0.0.1
port:6379
@Autowired
private RedisTemplate redistemplate;
<dependency>
<groupid>org.mybatis.spring.boot</groupid>
<artifactid>mybatis-spring-boot-starter</artifactid>
<version>2.2.2</version>
</dependency>
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<scope>runtime</scope>
</dependency>
datasource
spring:
datasource:
url: jdbc:mysql:///test_db
username: root
password:
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.rsk.springboot1.entity
@Mapper
public interface UserMapper {
public List<user> findAll();
}
@Autowired
private UserMapper userMapper;
</user>
SpringBoot原理分析
Condition是Spring4.0增加的条件判断功能,可实现选择性的Bean创建,可自定义扩展
- 自定义扩展步骤:
public class MyCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<string, object> annotationAttributes = metadata.getAnnotationAttributes(ConditionOnClass.class.getName());
//获取注解传入的元数据中的键值对map
String[] value = (String[]) annotationAttributes.get("value");
//自定义条件判断函数
if (value[0].equals("0")){
return true;
}
return false;
}
}
</string,>
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(MyCondition.class)
public @interface ConditionOnClass {
String[] value();
}
@Configuration
public class UserConfig {
@Bean
@ConditionOnClass(value = "1")
public User user(){
return new User();
}
}
- SpringBoot提供的常用条件注解:
@Enable*注解用于动态启动某些功能,底层原理是使用@Import注解导入一些配置类,实现Bean的动态加载
@Enable*底层依赖@Import注解导入所需要的类,使用@Import注解导入的类会被Spring容器加载到IOC容器中,以下有4种用法:
- 导入Bean
- 导入配置类
- 导入ImportSelector实现类,一般用于加载配置文件中的类
-
导入ImportBeanDefinitionRegistrar实现类
-
@EnableAutoConfiguration注解内部使用
@Import({AutoConfigurationImportSelector.class})
来加载配置类 - 配置文件位置:\META-INF\spring.factories,该配置文件中定义了大量的配置类,当SpringBoot启动时会自动加载这些配置类,初始化Bean
- 不是所有Bean都会被初始化,在配置类中使用@Configuration注解来加载满足条件的Bean
需求:(自定义redis-starter,要求导入该坐标时,SpringBoot能够自动创建Bean)
步骤:
Original: https://www.cnblogs.com/rskblog/p/16328057.html
Author: 小小的脑袋
Title: SpringBoot学习笔记
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/569475/
转载文章受原作者版权保护。转载请注明原作者出处!