序文
多くの人は、主に春の強力な機能を考慮し、急速な発展の利便性を達成できるため、スプリングブーツを選択すると思います。この記事では、主に、Spring Bootが開始されたときに外部構成ファイルのロードに関する関連コンテンツを紹介します。以下ではあまり言いません。編集者と学び、学びましょう。
ビジネス要件:
外部構成ファイルをロードすると、展開中に変更が容易になります。
最初にコードをアップロードします。
@SpringBootApplicationPublic Class Application {public static void main(string [] args)throws Exception {springApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.Class); SpringApplicationBuilder.Web(true);プロパティプロパティ= getProperties(); StandardEnvironment Environment = new StandardEnvironment(); Environment.GetPropertySources()。AddLast(new PropertiesPropertySource( "Micro-Service"、Properties)); SpringApplicationBuilder.Environment(環境); SpringApplicationBuilder.run(args); } private static Properties getProperties()throws ioException {propertiesFactoryBean PropertiesFactoryBean = new PropertiesFactoryBean(); ResourcePatterNresolver Resolver = new PathMatchingResourcePatterNresolver(); propertiesFactoryBean.SetignorerESourCenotFound(true); Resource FilesystemResource = Resolver.GetResource( "file:/opt/company/test.properties"); propertiesFactoryBean.SetLocations(FilesystemResource); propertiesFactoryBean.AfterPropertiesset(); return propertiesfactorybean.getObject(); }}変数を使用したツール
@ComponentPublic Class EnvironmentUtil {プライベート静的環境環境; @autowired public void setenvironment(環境環境){Environmentutil.environment =環境; } public static <t> t getProperty(string key、class <t> targetType、t defaultValue){return Environment.getProperty(key、targetType、defaultValue); } public static <t> t getProperty(string key、class <t> targetType){return Environment.getProperty(key、targetType); } public static string getProperty(string key){return Environment.getProperty(key); } public static string getProperty(string key、string defaultValue){return Environment.getProperty(key、defaultValue); } public static Integer getInteger(string key、integer defaultValue){return Environment.getProperty(key、integer.class、defaultValue); }} @Value("${key}")経由で使用することもできます
負荷方法の優先度が高くなっています。 Spring Boot Configurationファイルと同じ名前の場合、 application.propertiesファイルの構成が上書きされます。
要約します
上記は、この記事のコンテンツ全体です。この記事の内容には、すべての人の研究や仕事に特定の参照値があることを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。 wulin.comへのご支援ありがとうございます。