Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Advanced applications of propeties configuration for springboot

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

One: the use of the EnvironmentPostProcessor interface, using this interface, you can load properties files dynamically (that is, according to business logic).

@ Component

Public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

@ Override

Public void postProcessEnvironment (ConfigurableEnvironment environment, SpringApplication application) {

Try (InputStream inputStream = new FileInputStream ("E:/tmp/springboottest.properties")) {

Properties source = new Properties ()

Source.load (inputStream)

PropertiesPropertySource propertySource = new PropertiesPropertySource ("my", source)

Environment.getPropertySources () addLast (propertySource)

} catch (IOException e) {

E.printStackTrace ()

}

}

}

Then get the contents of the configuration file:

Public static void main (String [] args) {

ConfigurableApplicationContext context = SpringApplication.run (App.class, args)

Context.getBean (Runnable.class) .run ()

/ / String property = context.getEnvironment () .getProperty ("local.ip")

/ / System.out.println ("the parameter obtained is:" + property)

/ context.getBean (UserConfig.class) .show ()

/ context.getBean (Dbconfig.class) .show ()

/ context.getBean (DataSourceProperties.class) .show ()

System.out.println (context.getBean (TomcatProperties.class))

System.out.println (context.getEnvironment () .getProperty ("spring.boot.name")

Context.close ()

}

Springboot supports the configuration of arrays and collections

Package com.zcp.springstart

Import java.util.ArrayList

Import java.util.Arrays

Import java.util.List

Import org.springframework.boot.context.properties.ConfigurationProperties

Import org.springframework.stereotype.Component

/ * *

* support getting arrays and collections

* configuration method: name [index] = value

* how to write it in application.properties file:

* data.url=127.0.0.1

Data.port=1234

Data.name=123

Data.password=root.post

Ds.hosts [0] = 128.128.128.0

Ds.hosts [1] = 128.128.128.1

Ds.hosts [2] = 128.128.128.2

Ds.hosts [3] = 128.128.128.3

Ds.ports [0] = 8888

Ds.ports [1] = 8889

Ds.ports [2] = 8890

Ds.ports [3] = 8891

Ds.ports [4] = 8892

*

Title: TomcatProperties

*

Description:

*

Company: www.itcast.cn

* @ version 1.0

, /

@ Component

@ ConfigurationProperties ("ds")

Public class TomcatProperties {

Private List hosts = new ArrayList ()

Private String [] ports

Public String [] getPorts () {

Return ports

}

Public void setPorts (String [] ports) {

This.ports = ports

}

Public List getHosts () {

Return hosts

}

Public void setHosts (List hosts) {

This.hosts = hosts

}

@ Override

Public String toString () {

Return "TomcatProperties [hosts=" + hosts + ", ports=" + Arrays.toString (ports) + "]"

}

}

3: read application-*.properties configuration information

Package com.zcp.springstart

Import org.springframework.boot.SpringApplication

Import org.springframework.boot.autoconfigure.SpringBootApplication

Import org.springframework.context.ConfigurableApplicationContext

/ * method 1:

* start parameters to control the effective profile. Multiple profile can be activated simultaneously

*-- spring.profiles.active=dev,test

*

* method 2:

*

* SpringApplication springApplication = new SpringApplication (App2.class)

/ / springApplication.setAdditionalProfiles ("dev")

SpringApplication.setAdditionalProfiles ("dev", "test")

Description:

Application-dev.properties

Application-test.properties

The naming of

*

Title: App2

*

Description:

*

Company: www.itcast.cn

* @ version 1.0

, /

@ SpringBootApplication

Public class App2 {

Public static void main (String [] args) {

SpringApplication springApplication = new SpringApplication (App2.class)

/ / springApplication.setAdditionalProfiles ("dev")

SpringApplication.setAdditionalProfiles ("dev", "test")

ConfigurableApplicationContext context = springApplication.run (args)

System.out.println (">")

System.out.println (context.getEnvironment () .getProperty ("db123.url")

System.out.println (context.getEnvironment () .getProperty ("db123.url2")

System.out.println (">.")

Context.close ()

}

}

If it is a class or method, you can use it by annotating @ Profile ("test")

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report