In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article takes a look at "how springboot yml profile values are injected". The content is detailed and easy to understand, and friends who are interested in "what is the way of injecting springboot yml profile values" can follow the editor's train of thought to read it in depth. I hope it will be helpful to you after reading. Let's learn more about "how to inject springboot yml profile values" with the editor.
Yml profile values are injected into the build project
Refer to IDEA to quickly build a spring-boot project (Spring initializr)
Pom.xml
After you create the project, you also need to add the dependency to the tag in pom.xml.
Org.springframework.boot spring-boot-configuration-processor true creates the entity class package com.demo.demo;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties (prefix = "person") public class Person {private String name; private int age @ Override public String toString () {return "person {" + "name='" + name +'\'+ ", age=" + age +'}';} public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age } public void setAge (int age) {this.age = age;}} spring boot Core profile application.yml
1. Change the application.properties file suffix to .yml, and the content is:
Server: port: 8080person: name: puppy age: 21 test class package com.demo.demo;import com.demo.demo.Person;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith (SpringRunner.class) @ SpringBootTestpublic class DemoApplicationTests {@ Autowired Person person=new Person () @ Test public void contextLoads () {System.out.println (person);}} run
Results:
Automatically inject yml files and properties files
In springboot, if you need to use the data in the configuration file, automatic injection is very convenient, but when using the properties in yml, the automatic injection is invalid? It is found that if it is a properties file, it can be injected directly, but yml needs to add a configuration.
Automatic injection class of yml files
Yml files are neat and simple compared with properties, and additional configuration is needed in the use of automatic injection.
Increase pom dependency
Org.springframework.boot spring-boot-configuration-processor true
Add configuration class
Public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {@ Override public PropertySource createPropertySource (String name, EncodedResource resource) throws IOException {if (null = = resource) {super.createPropertySource (name, resource);} return new YamlPropertySourceLoader () .load (resource.getResource () .getFilename (), resource.getResource (), null);}}
Inject the specified yml configuration file into the entity class
/ * * add these three annotations to the class class to be injected * / @ Configuration@PropertySource (value = "classpath:application-quartz.yml", factory = YamlPropertyLoaderFactory.class) @ ConfigurationProperties (prefix = "quartz") public class JobTime {private String articleCarwler; private String earthlySweetSentenceApi; private String rainbowFartApi; private String qqRobotMsgTimer;}
Description:
@ PropertySource (value = "classpath:application-quartz.yml", factory = YamlPropertyLoaderFactory.class)
This annotation indicates which configuration file to inject and specifies the configuration class in step 2
@ ConfigurationProperties (prefix = "quartz")
Indicates what prefix the attribute in the specified configuration file is, and can be ignored, for example: quartz.articleCarwler=0 0Unip 5 *?
Automatic injection of Properties profile
Properties type configuration file is relatively simple, there is no need to add the above dependencies and configuration, you can directly specify the injection
Directly inject attributes into class
@ Configuration@PropertySource ("classpath:/application.properties") @ ConfigurationProperties (prefix = "quartz") public class JobTime {private String task1; private String task2;}
Description:
@ PropertySource ("classpath:/application.properties")
Specify profile name
@ ConfigurationProperties (prefix = "quartz")
Specify the prefix of key in the configuration file, but ignore this comment, for example: quartz.task1=0 3 *?
Direct injection into the code
If you are using separate attributes in your code and you do not need to inject them all into class, you can use annotations to inject them directly into variables and use them directly in your code.
Both yml and properties can be injected directly without other configuration.
You can inject it directly using the annotation: @ Value ("${key}").
For example:
@ Value ("${quartz.taks1}") private String taks1;springboot is a new programming specification for springboot, which is designed to simplify the initial construction and development process of new Spring applications. SpringBoot is also a framework that serves the framework, and the scope of services is to simplify configuration files.
On the springboot yml profile value injection method is shared here, I hope that the above content can make you improve. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.