码迷,mamicode.com
首页 > 编程语言 > 详细

(006)Spring Boot之配置文件中可以使用$引用变量

时间:2019-11-10 19:37:11      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:red   eve   void   配置   component   toc   ble   scope   close   

  springboot的配置文件中可以使用$引用变量,如下:

  pom.xml

技术图片
<?xml version="1.0" encoding="UTF-8"?>

<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>com.edu.spring</groupId>
    <artifactId>springboot</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>springboot</name>
    <!-- FIXME change it to the project‘s website -->
    <url>http://www.example.com</url>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.6.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

</project>
View Code

  UserConfig.java

技术图片
package com.edu.spring.springboot;

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 UserConfig {

    @Autowired
    private Environment environment;
    
    @Value("${name}")
    private String name;
    
    @Value("${app.name}")
    private String app_name;
    
    public void show(){
        System.out.println("name= "+environment.getProperty("name"));
        System.out.println("name= "+name);
        System.out.println("app.name= "+environment.getProperty("app.name"));
        System.out.println("app.name= "+app_name);
    }
}
View Code

  App.java

技术图片
package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        
        ConfigurableApplicationContext context=SpringApplication.run(App.class, args);
        context.getBean(UserConfig.class).show();
        context.close();
    }
}
View Code

  运行结果如下:

技术图片

 

(006)Spring Boot之配置文件中可以使用$引用变量

标签:red   eve   void   配置   component   toc   ble   scope   close   

原文地址:https://www.cnblogs.com/javasl/p/11827835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!