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

Three ways for springboot to read configuration files

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Method 1:

@ Configuration

/ / @ PropertySource ("classpath:jdbc.properties")

/ / @ PropertySource ("classpath:config/jdbc222222.properties")

@ PropertySources ({@ PropertySource ("classpath:jdbc.properties"), @ PropertySource ("classpath:config/jdbc222222.properties")})

Public class FileConfig {

}

@ Component

Public class Dbconfig {

@ Value ("${db.url}")

Private String dbUrl

@ Value ("${db.port}")

Private Integer dbPort

@ Value ("${db.password}")

Private String dbPassword

@ Value ("${db.username}")

Private String dbUserName

Public void show () {

System.out.println ("> parameters obtained in Dbconfigz")

System.out.println ("dbUrl=" + dbUrl)

System.out.println ("dbPort" + "=" + dbPort)

System.out.println ("dbPassword" + "=" + dbPassword)

System.out.println ("dbUserName" + "=" + dbUserName)

}

}

Information about the configuration file, whose name is jdbc.properties. The path is under resources

Db.password=root

Db.username=root

Data.url=127.0.0.12

Data.port=12343

Data.name=1233

Data.password=root3

The main way to test is:

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 ()

Context.close ()

}

Method 2: obtain external configuration information through environment

@ Component

Public class UserConfig {

@ Autowired

Environment environment

@ Value ("${local.port}")

Private String localPort

@ Value ("${local.port}")

Private Integer localPort2

@ Value ("${tomcat.port:9090}")

Private String tomcatPort

Public void show () {

System.out.println ("value of local.ip in application.properties:" + environment.getProperty ("local.ip"))

System.out.println ("value of local.port in application.properties:" + environment.getProperty ("local.port"))

System.out.println ("value of local.port in application.properties:" + environment.getProperty ("local.port", Integer.class))

System.out.println ("value of name in application.properties:" + environment.getProperty ("name"))

System.out.println ("value of app.name in application.properties:" + environment.getProperty ("app.name"))

System.out.println ("value of tomcat.port in application.properties:" + environment.getProperty ("tomcat.port"))

System.out.println ("value of tomcat.port in application.properties:" + tomcatPort)

}

}

Method 3:

@ Component

@ ConfigurationProperties (prefix= "data")

Public class DataSourceProperties {

Private String url

Private String port

Private String name

Private String password

Public String getUrl () {

Return url

}

Public void setUrl (String url) {

This.url = url

}

Public String getPort () {

Return port

}

Public void setPort (String port) {

This.port = port

}

Public String getName () {

Return name

}

Public void setName (String name) {

This.name = name

}

Public String getPassword () {

Return password

}

Public void setPassword (String password) {

This.password = password

}

Public void show () {

System.out.println ("> url" + url)

System.out.println ("> port" + port)

System.out.println ("> name" + name)

System.out.println ("> password" + password)

}

}

General description:

* two methods to obtain the information in the configuration information

* the file name of the configuration file is application.properties

* the default location is in the classpath root directory or classpath:/config file:/ file:config/

*

* the default configuration file name can be specified using-- spring.config.name. You only need to specify the name of the file, and the file extension can be omitted.

* the default profile path can be specified using-- spring.config.location

* configuration files need to specify the full path, including directories and file names, and you can specify multiple ones, separated by commas

*-- spring.config.location=classpath:config/app.properties (if it is a file, file: the path to the file)

*

Spring boot 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) + "]"

}

}

Enter the configuration information in the application.properties file below:

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

The main way to test is:

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))

Context.close ()

}

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