新增域名网站建设方案,有关做美食的网站,登陆wordpress,怎么做一元购网站一、外部配置及优先级SpringBoot的外部配置属性值官方给出了很多种方式#xff0c;以便可以在不同的环境中使用相同的代码。其使用了非常特别的PropertySource命令#xff0c;旨在允许合理的覆盖值。当然#xff0c;如果属性值不同#xff0c;则这些配置方式中的属性值都会…一、外部配置及优先级SpringBoot的外部配置属性值官方给出了很多种方式以便可以在不同的环境中使用相同的代码。其使用了非常特别的PropertySource命令旨在允许合理的覆盖值。当然如果属性值不同则这些配置方式中的属性值都会被加载按照从高到低的排序如下1、在您的HOME目录设置的Devtools全局属性~/.spring-boot-devtools.properties。2、单元测试中的 TestPropertySource 注解。3、单元测试中的 SpringBootTest#properties 注解属性4、命令行参数。SPRING_APPLICATION_JSON‘{foo:{bar:spam}}‘ java -jar myapp.jar6、ServletConfig 初始化参数。7、ServletContext 初始化参数。8、来自 java:comp/env 的JNDI属性。9、Java系统属性System.getProperties()。10、操作系统环境变量。11、RandomValuePropertySource只有随机的属性 random.* 中。12、jar包外面的 Profile-specific application properties application- {profile} .properties和YAML13、jar包内的 Profile-specific application properties application-{profile}.properties和YAML14、jar包外的应用属性文件application.properties和YAML。15、jar包内的应用属性文件application.properties和YAML。16、在Configuration上的PropertySource注解。17、默认属性使用SpringApplication.setDefaultProperties设置。在具体的讲解这些配置的时候我们先来做一些准备工作1、书写一个ControllerRestController
Slf4j
public class MyController {Value(${name})private String name;GetMapping(/getDefaultProperties)public String getDefaultProperties() {return name;}
1、使用SpringApplication.setDefaultProperties设置默认属性在应用程序主类main方法中在调用run方法之前设置默认值SpringBootApplication
public class Application {public static void main(String[] args) {//SpringApplication.run(Application.class, args);Properties properties new Properties();properties.setProperty(name, 17、默认属性使用SpringApplication.setDefaultProperties设置);SpringApplication application new SpringApplication(Application.class);application.setDefaultProperties(properties);application.run(args);//new SpringApplicationBuilder()// .sources(Application.class)// .bannerMode(Banner.Mode.OFF)// .properties(properties)// .run(args);}
}
此时你访问http://localhost:8080/getDefaultProperties在页面上输出的内容为备注如果你想把你项目中的所有的配置放到配置中心Apollo的话这种方式也是可以很方法的实现的。2、在Configuration上的PropertySource注解SpringBootApplication
PropertySource(value {classpath:test/propertySource.properties}, encoding UTF-8)
public class Application {public static void main(String[] args) {Properties properties new Properties();properties.setProperty(name, 17、默认属性使用SpringApplication.setDefaultProperties设置);SpringApplication application new SpringApplication(Application.class);application.setDefaultProperties(properties);application.run(args);}
}
3、jar包内的应用属性文件即默认的配置文件name15、jar包内的应用属性文件
4、jar包外的应用属性文件1、将你的应用程序打包成可运行的jar在jar包的同级目录中放置一个application.properties文件,里面的内容如下name14、jar包外的应用属性文件
5、jar包内的 Profile-specific application properties新建application-test.properties文件里面的内容如下name13、jar包内的 Profile-specific application properties
设置启动参数访问链接得到下图结果6、jar包外面的 Profile-specific application properties1、将你的应用程序打包成可运行的jar在jar包的同级目录中放置一个application-test.properties文件,里面的内容如下name14、jar包外的应用属性文件
2、java -jar -Dspring.profiles.activetest ***.jarspringboot读取外部和内部配置文件的方法如下优先级第一种是在执行命令的目录下建config文件夹。在jar包的同一目录下建config文件夹执行命令需要在jar包目录下才行然后把配置文件放到这个文件夹下。第二种是直接把配置文件放到jar包的同级目录。第三种在classpath下建一个config文件夹然后把配置文件放进去。第四种是在classpath下直接放配置文件。springboot默认是优先读取它本身同级目录下的一个config/application.properties 文件的。在src/main/resources 文件夹下创建的application.properties 文件的优先级是最低的7、命令行属性默认情况下SpringApplication将任何命令行选项参数以-- 开头例如--server.port9000转换为属性并将其添加到Spring环境中。 如上所述命令行属性始终优先于其他属性来源。如果不希望将命令行属性添加到环境中可以使用SpringApplication.setAddCommandLineProperties(false)禁用它们。二、properties文件中的占位符application.properties中的值在使用时通过已有的环境进行过滤以便可以引用之前已经定义好的值app.nameMyApp
app.description${app.name} is a Spring Boot application
三、使用YAML替代 PropertiesYAML是JSON的超集因此这是分层配置数据一种非常方便的格式。 每当您的类路径中都有SnakeYAML库时SpringApplication类将自动支持YAML作为 properties 的替代方法。如果您使用“Starters”SnakeYAML将通过spring-boot-starter自动提供。
1、加载yamlSpring Framework提供了两个方便的类可用于加载YAML文档。 YamlPropertiesFactoryBean将YAML作为Properties加载YamlMapFactoryBean将YAML作为Map加载。例如下面这个YAML文档environments:dev:url: http://dev.bar.comname: Developer Setupprod:url: http://foo.bar.comname: My Cool App
将转换为属性environments.dev.urlhttp://dev.bar.com
environments.dev.nameDeveloper Setup
environments.prod.urlhttp://foo.bar.com
environments.prod.nameMy Cool App
YAML列表表示为具有[index] dereferencers的属性键例如YAMLmy:servers:- dev.bar.com- foo.bar.com
将转化为属性my.servers[0]dev.bar.com
my.servers[1]foo.bar.com
2、通过ConfigurationProperties将配置绑定到Bean1、配置bean的书写Getter
Setter
Component
ConfigurationProperties(prefix my)
public class MyServerProperties {private String name;private ListString servers new ArrayList();
}
2、application.yaml文件书写my:servers:- dev.bar.com- foo.bar.comname: myName
3、YAML的缺点YAML文件无法通过PropertySource注解加载。 因此在需要以这种方式加载值的情况下需要使用properties文件。四、类型安全的配置属性使用Value(“${property}”)注释来注入配置属性有时可能很麻烦类型常规配置特别是如果您正在使用多个层次结构的属性或数据时。 Spring Boot提供了一种处理属性的替代方法允许强类型Bean管理并验证应用程序的配置。1、Bean的定义/*** 类型安全的配置属性** Author YUBIN* create 2019-06-16*/
ConfigurationProperties(foo)
public class FooProperties {private boolean enabled;private InetAddress remoteAddress;private final Security security new Security();public boolean isEnabled() {return enabled;}public void setEnabled(boolean enabled) {this.enabled enabled;}public InetAddress getRemoteAddress() {return remoteAddress;}public void setRemoteAddress(InetAddress remoteAddress) {this.remoteAddress remoteAddress;}public Security getSecurity() {return security;}public static class Security {private String username;private String password;private ListString roles new ArrayList(Collections.singleton(USER));public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public ListString getRoles() {return roles;}public void setRoles(ListString roles) {this.roles roles;}}
}
2、YAML文件中的配置foo:remote-address: 192.168.1.1security:username: fooroles:- USER- ADMIN
3、应用程序主类上加上必要的注解EnableConfigurationProperties({FooProperties.class})
public class Application {
这样在程序中就可以使用Autowired的形式注入配置类了RestController
Slf4j
public class MyController {Autowiredprivate FooProperties fooProperties;GetMapping(/getFooProperties)public String getFooProperties() {return JSON.toJSONString(fooProperties);}
五、第三方配置现在我们在构建一个名为yubin-common类型为jar的maven项目1、书写一个配置类ConfigurationProperties(prefix bar)
public class BarComponent {private String name;public String getName() {return name;}public void setName(String name) {this.name name;}
}
在之前的项目中引入此项目的依赖这时如果在之前的项目中需要使用BarComponent这个属性类的话则需要通过EnableConfigurationProperties(BarComponent.class)这个注解来引入类但是这样做是否合理呢一个项目这么做当有其它的项目也需要这个类的话是不是也要这么做呢public class BarComponent {private String name;public String getName() {return name;}public void setName(String name) {this.name name;}
}
Configuration
public class ConfigurationBean {BeanConfigurationProperties(prefix bar)public BarComponent barComponent() {return new BarComponent();}
}
2、小结ConfigurationProperties导入外部属性填充到这个Bean的实例有三种方式1、ConfigurationProperties Component 注解到bean定义类上2、ConfigurationProperties Bean注解在配置类的bean定义方法上3、ConfigurationProperties注解到普通类然后通过EnableConfigurationProperties定义为bean六、宽松的绑定Spring Boot使用一些宽松的规则将环境属性绑定到ConfigurationProperties bean因此不需要在Environment属性名称和bean属性名称之间进行完全匹配。 常用的例子是这样有用的虚分离例如上下文路径绑定到contextPath和大写例如PORT绑定到端口环境属性。配置bean/*** 属性bean宽松绑定演示** Author YUBIN* create 2019-06-16*/
Component
ConfigurationProperties(prefix person)
Getter
Setter
public class OwnerProperties {private String firstName; //person.firstName 标准骆峰命名法。private String secondName; // person.second-name 虚线符号推荐用于.properties和.yml文件。private String thirdName; // person.third_name 下划线符号用于.properties和.yml文件的替代格式。private String fourName; // PERSON_FOUR_NAME 大写格式 推荐使用系统环境变量时。
}
测试类RestController
Slf4j
public class MyController {Autowiredprivate OwnerProperties ownerProperties;GetMapping(/getOwnerProperties)public String getOwnerProperties() {return JSON.toJSONString(ownerProperties);}
}
七、ConfigurationProperties验证如果你需要验证ConfigurationProperties类。 您可以直接在配置类上使用JSR-303 javax.validation约束注解 Validated。 只需确保您的类路径中符合JSR-303实现然后在您的字段中添加约束注释ConfigurationProperties(foo)
Validated
public class FooProperties {private boolean enabled;NotNullprivate InetAddress remoteAddress;
// ... getters and setters
为了验证嵌套属性的值您必须将关联字段注释为Valid以触发其验证。 例如基于上述FooProperties示例ConfigurationProperties(prefixconnection)
Validated
public class FooProperties {NotNullprivate InetAddress remoteAddress;Validprivate final Security security new Security();// ... getters and setterspublic static class Security {NotEmptypublic String username;// ... getters and setters}
}
ConfigurationProperties 对比 ValueValue是核心容器功能它不提供与类型安全配置属性相同的功能。 下表总结了ConfigurationProperties和Value支持的功能八、配置文件Profiles1、代码层面区分环境Spring 配置文件提供了将应用程序配置隔离的方法使其仅在某些环境中可用。 任何Component或Configuration都可以使用Profile进行标记以限制其在什么时候加载1、测试环境Service
Profile({test})
public class TestProfileServiceImpl implements ProfileService {Overridepublic String getEnvironment() {return test环境;}
}
2、dev环境Service
Profile({dev})
public class DevProfileServiceImpl implements ProfileService {Value(${spring.profiles.active})private String profileActive;Overridepublic String getEnvironment() {return dev环境;}
}
3、测试类RestController
Slf4j
public class MyController {Autowiredprivate ProfileService profileService;GetMapping(/getProfileService)public String getProfileService() {return profileService.getEnvironment();}
}
当设置启动参数为 spring.profiles.activedev时,页面展示效果:image.png当设置启动参数为 spring.profiles.activetest时,页面展示效果: