网站12栅格系统怎么做,ios软件开发工程师,湛江建站服务,成都好玩的地方排行榜目录
1 Profiles
1.1 组件环境隔离
1.1.1 标识环境
1.1.2 激活环境
1.2 配置环境隔离
1.2.1 添加副配置文件
1.2.2 激活环境
2 外部化配置
2.1 配置优先级
2.2 快速部署
3 自定义starter
3.1 基本抽取
3.1.1 导yaml提示包
3…目录
1 Profiles
1.1 组件环境隔离
1.1.1 标识环境
1.1.2 激活环境
1.2 配置环境隔离
1.2.1 添加副配置文件
1.2.2 激活环境
2 外部化配置
2.1 配置优先级
2.2 快速部署
3 自定义starter
3.1 基本抽取
3.1.1 导yaml提示包
3.1.2 【公共模块】抽取
3.1.3 【普通模块】引用
3.2 EnableXxx注解抽取
3.3 自动配置抽取
3.3.1 SPI机制
3.3.2 全自动配置 1 Profiles 用于环境隔离,能够快速切换开发环境(dev),测试环境(test),生产环境(prod)等 1.1 组件环境隔离 针对不同的环境,往SpringIOC容器中注册不同的组件 1.1.1 标识环境 Profile({dev,test})注解可以配合Component等注解使用,也可以配合Bean注解使用,标记当前组件在指定环境下生效 没有标Profile的类,在任何环境下都生效 Profile对于EnableConfigurationProperties({Dog.class,Cat.class})中的Dog类、Cat类不生效(即Dog类用了Profile也会被注册进SpirngIOC)
1.1.2 激活环境 在properties.yaml文件中激活对应的环境,如果没有配置对应的激活环境,则SpringBoot默认激活default环境; 如果配置了激活环境,则default环境失效
spring:profiles:active: dev,test
1.2 配置环境隔离 针对不同的环境,选择生效的副配置文件(主配置文件application.yaml永远生效) 1.2.1 添加副配置文件 例如:application-dev.yaml、application-test.yaml、application-prod.yaml
1.2.2 激活环境 激活方式与1.1.2相同,且只能写在主配置文件中: 例如:spring.profiles.activedev,则application-dev.yaml生效 如果副配置文件和主配置文件配置冲突,以副配置文件为准
2 外部化配置
2.1 配置优先级 命令行 包内外 (例: java -jar demo.jar --server.port9999) jar包外配置优先级 jar包内配置优先级 副配置优先级 主配置优先级
包内:(1)application.yaml #server.port8000(2)application-dev.yaml #server.port8001
包外:(3)application.yaml #server.port9000(4)application-dev.yaml #server.port9001//有(1),以(1)为准
//有(1)(2),以(2)为准
//有(1)(2)(3),以(2)为准
//有(1)(2)(3)(4),以(4)为准//执行的是命令行,以命令行为准
2.2 快速部署 根据配置优先级的规则,当需要修改某一项配置,无需再重新打jar包部署,而只需要新建一个与jar包文件同级别的外部配置文件,然后用java -jar命令再次执行jar包即可
3 自定义starter 当我们在编写多个模块时,往往有许多重复代码,可以考虑将这些重复代码抽取出来作为一个starter,然后让模块去引用此starter即可
3.1 基本抽取
3.1.1 导yaml提示包 在yaml中配置SpringBoot官方给的配置,当输入相关值的时候,会有提示 而一些自定义的配置(例如:属性绑定时),则在yaml文件中不会有提示,而且被属性绑定的类还会被IDEA标红提示 只需要引入spring提供的starter即可解决
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional
/dependency
3.1.2 【公共模块】抽取 (1) 创建公共模块,可以起名为xxx-xxx-spring-boot-starter (2) 导入此模块需要的依赖(jar包和starter),以及其它模块们都需要的公共依赖(这样其它模块就无须引入公共依赖了) (3) 抽取公共代码(例如:问候接口/hello/welcome) (4) 因为SpringBoot默认只会扫描自己主程序的包,外部引入的依赖包不会被扫描到,所以: 在【公共模块】的Spring配置类中,用import或Bean引入需要注册的组件(如Controller,Service,pojo等) 在【普通模块】引入【公共模块】后,在Spring配置类中用Import或Bean引入【公共模块】的Spring配置类
!--新建一个【公共模块】,导入此模块需要的包,导入【普通模块】们都需要的公共依赖--
groupIdstarter.xyz.aboluo/groupId
artifactIdaboluo-hello-spring-boot-starter/artifactId
version1.0/version// 【公共模块】抽取公共代码(以问候接口/hello/welcome为例)
RestController
RequestMapping(/hello)
public class HelloController {Autowiredprivate HelloServiceImpl helloService; //注意:这里Autowired的是ServiceImpl类,并不是Service接口(因为在【普通模块】中加载不到)PostMapping(/welcome)public String hello() {return helloService.hello();}
}//Service 因为在Spring配置类中被用Import引入了,此注解可以不写
public class HelloServiceImpl {Autowiredprivate HelloProperties helloProperties;public String hello() {return helloProperties.getWelcomeMessage() 本服务是由【 helloProperties.getCompanyName() 】公司提供技术支持;}
}//【自动配置类】
//Component 因为在Spring配置类中被用EnableConfigurationProperties引入了,此注解可以不写
ConfigurationProperties(prefix hello)
public class HelloProperties {private String welcomeMessage;private String companyName;
}SpringBootConfiguration
ComponentScan(starter.xyz.aboluo)
//因为这些类在【普通模块】中不会被扫描到,所以全部引入到Spring配置类中
Import({HelloController.class, HelloServiceImpl.class})
EnableConfigurationProperties({Hello.class})
public class AboluoHelloAutoConfiguration {
}
3.1.3 【普通模块】引用
!--【普通模块】引入自定义starter--
dependencygroupIdstarter.xyz.aboluo/groupIdartifactIdaboluo-hello-spring-boot-starter/artifactIdversion1.0/version
/dependency//【普通模块】的Spring配置类中用Import引入【公共模块】的Spring配置类
SpringBootConfiguration
MapperScan(xyz.aboluo.dao)
Import(AboluoHelloAutoConfiguration.class)
public class SpringConfig {
}// 在yaml中对Hello类进行属性绑定
hello:welcome-message: 欢迎您的使用!company-name: 字节跳动//此时依赖【公共模块】的【普通模块】启动后,就可以访问到问候接口/hello/welcome
3.2 EnableXxx注解抽取 使用3.1的抽取方式的弊端:【普通模块】引入【公共模块】后,需要在Spring配置类上用Import引入【公共模块】的Spring配置类 利用Enable机制,可以在【公共模块】中自定义一个EnableXxx注解,【普通模块】在Spring配置类使用【公共模块】的自定义注解即可,无需再使用Import了
//【公共模块】自定义注解
Retention(RetentionPolicy.RUNTIME)
Target({ElementType.TYPE})
Documented
Import({HelloController.class, HelloServiceImpl.class, Hello.class})
public interface EnableHello {
}//【普通模块】的Spring配置类上使用自定义注解
SpringBootConfiguration
MapperScan(xyz.aboluo.dao)
EnableHello
public class SpringConfig {
}
3.3 自动配置抽取
3.3.1 SPI机制 (Java的SPI默认访问META-INF/services/目录下的文件) (SpringBoot的SPI默认访问META-INF/spring/目录下的文件) 项目引入官方starter依赖时,这个starter会继续依赖spring-boot-starter,其中一个核心依赖是spring-boot-autoconfigure,此时SpirngBoot会触发一个行为,自动去找org.springframework.boot:spring-boot-autoconfigure包META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件 此文件中所有的类(自动配置类一般写为XxxAutoConfiguration)会在项目启动时自动加载(被注册进SpringIOC),主要作用就是向SpringIOC容器中添加一些组件(用Import或Bean的方式) 并且会用EnableConfigurationProperties指定对应的配置属性类,开启属性绑定
3.3.2 全自动配置 使用3.2的抽取方式,还需要在【普通模块】的Spring配置类写自定义注解,有没有可能连自定义注解也不用写,直接引入【公共模块】就可以用呢? 利用SPI机制,在【公共模块】类资源文件夹resource下放META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,并写入Spring配置类(主要是要其中的Import)的全限定类名
//新建resource/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件
starter.xyz.aboluo.boot.AboluoHelloAutoConfigurationSpringBootConfiguration
ComponentScan(starter.xyz.aboluo)
Import({HelloController.class, HelloServiceImpl.class})
EnableConfigurationProperties({HelloProperties.class})
public class AboluoHelloAutoConfiguration {
}