zoukankan      html  css  js  c++  java
  • SpringBoot读取配置文件

    项目结构

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>test.demo</groupId>
    	<artifactId>springboot</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>springboot</name>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.3.0.RELEASE</version>
    		<relativePath /> 
    	</parent>
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    	</dependencies>
    </project>
    

      

    @Configurationproperties注解和@Value注解

    @ConfigurationProperties加在有@Configuration的类或者由@Bean注解的方法上,

    另外一个类似的注解@EnableConfigurationProperties

    用于允许@ConfigurationProperties

    使用示例:

        

    package hello;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class App
    {
        public static void main(String[] args)
        {
            SpringApplication.run(App.class, args);
        }
    }
    package hello.bean;
    
    //@Configuration
    //@ConfigurationProperties("user.properties")
    public class TestBean
    {
    	private String name;
    	private int 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;
    	}
    }
    

      

    package hello.config;
    
    import hello.bean.TestBean;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class BeanConfig
    {
    	@ConfigurationProperties("user.properties")
    	@Bean
    	public TestBean getBean()
    	{
    		return new TestBean();
    	}
    
    }
    

      

    package hello.controller;
    
    import hello.bean.TestBean;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class Controller
    {
    	@Autowired
    	TestBean bean;
    	@Value("${user.properties.name}")
    	String name;
    	
    	@RequestMapping(value = "/test", method = RequestMethod.GET)
    	public String get()
    	{
    		System.out.println(name);
    		return bean.getName();
    	}
    
    	@RequestMapping(value = "/post", method = RequestMethod.POST)
    	public String post()
    	{
    		return "post";
    	}
    
    }
    

      

    application.yml

    user:
      properties:
        age: 18
        name: zzzz

    单个属性使用@Value

  • 相关阅读:
    Springboot整合MongoDB的Docker开发,其它应用也类似
    Docker可视化工具Portainer
    Mac上使用Docker Desktop启动Kubernetes,踩坑后终于搞掂
    Docker入门——理解Docker的核心概念
    删库吧,Bug浪——我们在同一家摸鱼的公司
    一键下载网页所有图片,把美丽存下来
    序列推荐(transformer)
    [论文笔记] An introduction to ROC analysis
    【推荐算法工程师技术栈系列】分布式&数据库--tensorflow
    [论文翻译]Practical Diversified Recommendations on YouTube with Determinantal Point Processes
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/5143593.html
Copyright © 2011-2022 走看看