springBoot
- 从配置中获取值
- yml中的值
- name: ssw
- java中的代码
- 第一种配置
@Value("${name}")
String abc;@Autowired
Environment environment;
environment.getproperty(“name”);
@ConfigurationProperties
@Component
public class Person {
String name;
}
@Autowired
Person person;
- springboot-mvc自带校验功能
- 例如:
@ConfigurationProperties
@Component
@Validated
public class Person {
@NotNull
String name;
}
- profile配置方式
- 多profile文件方式:提供多个配置文件,每个代表一种环境
- yml多文档方式
- 在yml中使用 ---分割不同配置
- profile激活方式
- 配置文件: 再配置文件中配置:spring.profiles.active=dev
- 虚拟机参数:在VM options 指定: –Dspring.profiles.active=dev
- 命令行参数:java-jar xxx.jar –spring.profiles.active=dev
- file:/config/:当前项目下的/config目录下
- file:/ 当前项目的根目录
- classpath:/config/:classpath的/config目录
- classpath:/classpath的根目录
- 若测试类不在启动类的包或子包下
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Demospringboot2Application.class)
class Demospringboot2ApplicationTests {
@Value("${name}")
private String name;
@Test
void contextLoads() {
System.out.println(name);
}
@RunWith(SpringRunner.class)
@SpringBootTest
class Demospringboot2ApplicationTests {
@Value("${name}")
private String name;
@Test
void contextLoads() {
System.out.println(name);
}
- yml中配置
spring:
datasource:
url: jdbc:mysql:///health?serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:*Mapper.xml
- 如果是注解配置,则接口需要加入@Mapper注解识别 ,也可以在启动类上加入注解
@MapperScan({com.ssw.demospringboot2.mapper})- 如果没有配置,也可以使用redis,默认使用的是本机的redis
- 如果想指定redis,则需要配置
spring:
redis:
host: 127.0.0.1
port: 6379
- 是封装了jedis的工具类,简化jedis的操作
- 引导类需要加入注解@EnableScheduling
- 添加定时类
@Component
public class TestQuartz {
@Scheduled(cron="* * * * * ?")
public void test(){
System.out.println("11111");
}
}
- 内部使用@import来加载配置类
- 配置文件位置META-INF/spring.factories,该配置文件定义了大量的配置类,当springboot启动的时候会自动加载这些配置类,初始化bean
- 并不是所有的bean都会被初始化,再配置类使用condition来加载满足条件的bean
- 定义两个模块
- 第一个模块是配置模块
@Configuration
@EnableConfigurationProperties(RedisConfig.class)
public class JedisDemo {
@Bean
public Jedis jedis(RedisConfig redisConfig){
return new Jedis(redisConfig.getUrl(),redisConfig.getPort());
}
}
@Data
@ConfigurationProperties(prefix = "myredis")
public class RedisConfig {
private String url="127.0.0.1";
private int port=6379;
}
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ssw.config.JedisDemo
- 第二个调用配置模块
- redis-starter模块
- 只要在pom.xml中引入config模块
<dependency>
<groupId>com.ssw</groupId>
<artifactId>config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
- 构造器:
- 判断是否为web环境
- 读取META-INF中的spring.factories中的配置
- 找到main方法
- run方法
- 准备运行环境
- 回调listeren相应的方法
- 初始化spring容器
- 组件,导入起步依赖,判断相应的条件@Conditionxxxx
- 回调applicationRunner和CommandLineRunner接口中的方法
- https://www.jianshu.com/p/4cebe1274226
- 打jar包方式
- 只需要正常打包即可,然后在控制台执行命令java –jar jar包地址
- 打war包方式
- pom
- 启动类改动
<!-- 打包方式 -->
<packaging>war</packaging>
<build>
<!-- 指定war包名称 -->
<finalName>springboot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
@SpringBootApplication
public class SpringbootDeployApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringbootDeployApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootDeployApplication.class);
}
}
浙公网安备 33010602011771号