In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use springboot configuration and placeholders to get the values in the configuration file. I hope you will get something after reading this article. Let's discuss it together.
Springboot configuration and placeholder get profile value @ PropertySource& load the specified configuration file package com.example.springbootdemo.pojo; import com.alibaba.fastjson.JSON;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component / * * * GOOD LUCK**** @ Author: Wukn * @ Date: 2018 Universe 6 / * * sets the value of each attribute in the configuration file Map to this build * @ ConfigurationProperties * prefix = "persion" specifies that the configuration properties of persion need to be mapped to this entity class in the configuration file * / / * get the specified configuration file * @ PropertySource (value = {"classpath:coms.properties"}) * / @ Component/** * @ ConfigurationProperties (prefix = "persion") Get the value * / @ ConfigurationProperties (prefix = "persion") public class Persion {private String name under the root directory by default Private Integer id; private Boolean bool; public Persion () {} public String getName () {return name;} public void setName (String name) {this.name = name;} public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public Boolean getBool () {return bool } public void setBool (Boolean bool) {this.bool = bool;} @ Override public String toString () {return JSON.toJSONString (this);}}
@ ImportResource imports the specified configuration file
The above methods are too troublesome. Springboot recommends adding components through full annotations.
Declare a configuration class by the annotation @ Configration, which can be used on the method by the annotation @ Bean, and declare the generation of a component. If placed on the method, it indicates that the return value of the method is placed in the ioc container
Package com.example.springbootdemo.configration;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;/** * Created with IntelliJ IDEA. * Description: some settings for Cms data sources * Date: 2018-06-08 * Time: 5:50 PM * * @ author: wukn * / @ Configurationpublic class DataConfig {@ Bean public RedisTemplate redisTemplate (RedisConnectionFactory factory) {RedisTemplate template = new RedisTemplate (); template.setConnectionFactory (factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class); ObjectMapper om = new ObjectMapper (); om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY) Om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper (om); template.setValueSerializer (jackson2JsonRedisSerializer); template.afterPropertiesSet (); return template;}}
Get the value through the placeholder
# by using placeholders to assign persion.name= Zhang San ${random.value} persion.bool=falsepersion.id=12 ${random.int} person.last-name= Zhang San ${random.uuid} person.age=$ {random.int} person.birth=2017/12/15 person.boss=false person.maps.k1=v1 person.maps.k2=14 person.lists=a,b,c person.dog.name=$ {person.hello:hello} _ dog person.dog.age=15springboot configuration file, the use of placeholders should first be annotated
Make the user class available for instantiation through the configuration file
Package com.example.springdemo.entity;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;import org.springframework.validation.annotation.Validated; import java.util.List @ Component// adds User to the container @ Data/** * @ ConfigurationProperties * to change the value of each property in the configuration file Map to this component * tell springboot to bind all properties in Benlei and related properties in the configuration file first * prefix = "com" binding configuration file properties under the com level to map one by one * only containers can be used so add the annotation @ Component * / @ ConfigurationProperties (prefix = "com") public class User {private Long id Private String name; private Integer age; private Listlist; public Long getId () {return id;} public void setId (Long id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} public Integer getAge (int I) {return age } public void setAge (Integer age) {this.age = age;} public String getEmail () {return email;} public void setEmail (String email) {this.email = email;} private String email; public Integer getAge () {return age;} public List getList () {return list } public void setList (List list) {this.list = list;}} profile com.email=99@dfp.comcom.name=newDFP$ {com.cc: there is no default value} com.age=$ {random.int}
The first thing is to take a random number from age, and then get the data of the object from name.
Run the last test class package com.example.springdemo; import com.example.springdemo.entity.User;import com.example.springdemo.mapper.UserMapper;import com.example.springdemo.properties.Myproperties;import org.junit.jupiter.api.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.context.ApplicationContext;import org.springframework.test.context.junit4.SpringRunner; import javax.sql.DataSource;import java.sql.SQLException Import java.util.List; @ SpringBootTest@RunWith (SpringRunner.class) class SpringdemoApplicationTests {/ / if the test class does not match the startup entry class package name, you must specify the startup entry class with this annotation attribute classes, otherwise you cannot start SpringBoot @ Autowired private DataSource dataSource; @ Test public void dataSource () {try {System.out.println (dataSource.getConnection ()) } catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} @ Autowired Myproperties myproperties; @ Test void test () {System.out.println ("-"); System.out.println (myproperties.getMes ()) } @ Autowired UserMapper userMapper; @ Test void testMybatisPlus () {List users=userMapper.selectList (null); for (User user:users) {System.out.println (user);} System.out.println ("query successful!") ; User aduuser=new User (); / aduuser.setName ("DFP"); / / aduuser.setAge (18); / / aduuser.setEmail ("DFP19053025@qq.com"); / / aduuser.setId (19053065L); int i=userMapper.insert (aduuser); if (I > 0) {System.out.println ("join record successfully!") ;} else {System.out.println ("failed to join record!") ;} for (User user:users) {System.out.println (user);} @ Autowired User user; @ Test public void contextlodes () {System.out.println ("Test result output:" + user);}}
Result
Because com.cc does not exist, go back: the later default value
If com.cc exists, the value of com.cc will be taken.
Test the following com.email=99@dfp.comcom.name=newDFP+++$ {com.email: there is no default value} com.age=$ {random.int}
This time the value is no longer the default value. Com.email has data.
After reading this article, I believe you have a certain understanding of "how to use springboot configuration and placeholders to obtain the values in the configuration file". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
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.