zoukankan      html  css  js  c++  java
  • SpringBoot获得application.properties中数据的几种方式

    一、通过上下文

    @SpringBootApplication  
    public class SpringBoot01Application {  
      
        public static void main(String[] args) {  
            ConfigurableApplicationContext  context=SpringApplication.run(SpringBoot01Application.class, args);  
            String str1=context.getEnvironment().getProperty("aaa");  
            System.out.println(str1);  
        }  
    }  

    二、自动装配倒bean

    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.beans.factory.annotation.Value;  
    import org.springframework.core.env.Environment;  
    import org.springframework.stereotype.Component;  
      
    @Component  
    public class Student {  
        @Autowired  
        private Environment env;  
      
        public void speak() {  
            System.out.println("=========>" + env.getProperty("aaa"));  
        }  
    }  

    三、使用@Value注解

    package com.example.demo.entity;    
        
    import org.springframework.beans.factory.annotation.Value;    
    import org.springframework.context.annotation.PropertySource;    
    import org.springframework.stereotype.Component;    
        
    @Component    
    @PropertySource("classpath:jdbc.properties")//如果是application.properties,就不用写@PropertySource("application.properties"),其他名字用些    
    public class Jdbc {    
            
        @Value("${jdbc.user}")  
        private String user;    
            
        @Value("${jdbc.password}")   
        private String password;    
            
        public void speack(){    
            System.out.println("username:"+user+"------"+"password:"+password);    
        }    
    } 
  • 相关阅读:
    阿里巴巴java开发手册学习
    策略模式
    windows常用技巧
    nginx学习
    Tomcat入门
    ThreadPoolTaskExecutor的简单使用
    linux服务器测试性能
    HAProxy1.5.x tcp example
    Haproxy开启日志
    Fix rpmdb: Thread died in Berkeley DB library
  • 原文地址:https://www.cnblogs.com/fnlingnzb-learner/p/15386447.html
Copyright © 2011-2022 走看看