最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用springboot配置和占位符獲取配置文件中的值

 更新時間:2022年02月25日 14:45:01   作者:托尼吳  
這篇文章主要介紹了使用springboot配置和占位符獲取配置文件中的值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springboot配置和占位符獲取配置文件值

@PropertySource& 加載指定的配置文件

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/6/
 *
 * 將配置文件中的的每一個屬性的值,映射到這個組建中
 *@ConfigurationProperties
 * prefix = "persion"   指定在配置文件中需要將persion的配置屬性映射到這個實體類中
 */
 
/**
 * 獲取指定配置文件
 * @PropertySource( value = {"classpath:coms.properties"})
 */
 
@Component
/**
 * @ConfigurationProperties(prefix = "persion"),默認(rèn)獲取根目錄下的值
 */
@ConfigurationProperties(prefix = "persion")
public class Persion {  
    private String name;
    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 導(dǎo)入指定的配置文件

以上方式過于麻煩,springboot推薦通過全注解方式,添加組件的方式

通過注解@Configration申明一個配置類,通過注解@Bean可以使用在方法上面,申明一個組件的生成,要是放在方法上,表明這個方法的返回值放在ioc容器中

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: Cms數(shù)據(jù)源的一些設(shè)置
 * Date: 2018-06-08
 * Time: 5:50 PM
 *
 * @author: wukn
 */
@Configuration
public 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;
    } 
}

通過占位符獲取值

 
#通過使用占位符賦值
persion.name=張三${random.value}
persion.bool=false
persion.id=12${random.int}
 
person.last‐name=張三${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=15

springboot配置文件,占位符的使用

首先要給到注解

讓user類可用通過配置文件進行實例化

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//把User加到容器中
@Data
/**
 * @ConfigurationProperties
 * 可以將配置文件中的每一個屬性的值,映射到這個組件中
 * 告訴springboot將奔雷中的所有屬性和配置文件中的相關(guān)屬性先綁定
 * prefix = "com"綁定配置文件com層級下的屬性進行一一映射
 * 只有是容器才能使用所以要添加注解@Component
 */
@ConfigurationProperties(prefix = "com")
public class User {
    private Long id;
    private String name;
    private Integer age;
    private List<Object>list;
    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<Object> getList() {
        return list;
    }
 
    public void setList(List<Object> list) {
        this.list = list;
    }
}

配置文件

com.email=99@dfp.com
com.name=newDFP${com.cc:不存在給默認(rèn)值}
com.age=${random.int}

首先就是對age取隨機數(shù)然后對name獲取對象的數(shù)據(jù)

運行最后一個測試類

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 {
 
//如果測試類與啟動入口類包名不一致,必須加該注解屬性classes指定啟動入口類,否則無法啟動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<User> users=userMapper.selectList(null);
        for (User user:users){
            System.out.println(user);
        }
        System.out.println("查詢成功!");
        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("成功加入記錄!");
        }else{ System.out.println("失敗加入記錄!");}
        for (User user:users){
            System.out.println(user);
        } 
    }
 
    @Autowired
    User user;
    @Test
    public void contextlodes(){
        System.out.println("測試結(jié)果輸出:"+user);
    }
}

結(jié)果

因為com.cc是不存在的就回去:后面的默認(rèn)值

如果com.cc存在就會取com.cc的值

測試如下

com.email=99@dfp.com
com.name=newDFP+++${com.email:不存在給默認(rèn)值}
com.age=${random.int}

這次的值不再是默認(rèn)值了com.email是存在數(shù)據(jù)的

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA2020如何打開Run Dashboard的方法步驟

    IDEA2020如何打開Run Dashboard的方法步驟

    這篇文章主要介紹了IDEA2020如何打開Run Dashboard的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 為何修改equals方法時還要重寫hashcode方法的原因分析

    為何修改equals方法時還要重寫hashcode方法的原因分析

    這篇文章主要介紹了為何修改equals方法時還要重寫hashcode方法的原因分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • SpringBoot中@ConfigurationProperties 配置綁定

    SpringBoot中@ConfigurationProperties 配置綁定

    本文主要介紹了SpringBoot中@ConfigurationProperties 配置綁定,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 最優(yōu)雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企業(yè)級應(yīng)用(附源碼)

    最優(yōu)雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企業(yè)級應(yīng)用(附源碼)

    這篇文章主要介紹了最優(yōu)雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企業(yè)級應(yīng)用(附源碼),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • SpringBoot實現(xiàn)定時發(fā)送郵件的三種方法案例詳解

    SpringBoot實現(xiàn)定時發(fā)送郵件的三種方法案例詳解

    這篇文章主要介紹了SpringBoot三種方法實現(xiàn)定時發(fā)送郵件的案例,Spring框架的定時任務(wù)調(diào)度功能支持配置和注解兩種方式Spring?Boot在Spring框架的基礎(chǔ)上實現(xiàn)了繼承,并對其中基于注解方式的定時任務(wù)實現(xiàn)了非常好的支持,本文給大家詳細(xì)講解,需要的朋友可以參考下
    2023-03-03
  • Java正則表達(dá)式匹配不到結(jié)果的解決

    Java正則表達(dá)式匹配不到結(jié)果的解決

    這篇文章主要介紹了Java正則表達(dá)式匹配不到結(jié)果的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 聊聊Spring循環(huán)依賴三級緩存是否可以減少為二級緩存的情況

    聊聊Spring循環(huán)依賴三級緩存是否可以減少為二級緩存的情況

    這篇文章主要介紹了聊聊Spring循環(huán)依賴三級緩存是否可以減少為二級緩存的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 詳解Java的線程的優(yōu)先級以及死鎖

    詳解Java的線程的優(yōu)先級以及死鎖

    這篇文章主要介紹了詳解Java的線程的優(yōu)先級以及死鎖,線程是Java編程學(xué)習(xí)中的重要知識,需要的朋友可以參考下
    2015-09-09
  • 基于Springboot疫苗接種行程管理系統(tǒng)的設(shè)計與實現(xiàn)

    基于Springboot疫苗接種行程管理系統(tǒng)的設(shè)計與實現(xiàn)

    本文主要介紹了基于Springboot實現(xiàn)的疫苗接種行程管理系統(tǒng)的示例代碼,系統(tǒng)主要實現(xiàn)個人疫苗接種管理、行程管理、病史管理、風(fēng)險地區(qū)管理、核酸檢測報告結(jié)果上報、疫情新聞管理等功能,需要的可以參考一下
    2022-03-03
  • Java生成唯一id的幾種實現(xiàn)方式

    Java生成唯一id的幾種實現(xiàn)方式

    本文主要介紹了Java生成唯一id的幾種實現(xiàn)方式,主要介紹了5種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01

最新評論

鸡东县| 岗巴县| 揭西县| 张家港市| 田东县| 历史| 水城县| 建水县| 大姚县| 英山县| 古蔺县| 台山市| 信丰县| 洞头县| 科技| 县级市| 山东省| 三江| 措勤县| 胶南市| 瑞丽市| 永安市| 马龙县| 东辽县| 云南省| 冀州市| 东平县| 本溪| 泾川县| 东至县| 静宁县| 南汇区| 越西县| 乌恰县| 新邵县| 刚察县| 卢氏县| 夹江县| 富源县| 乌什县| 葵青区|