springboot 打印application.properties的值

springboot 打印springProperty的值

在Spring Boot中,你可以使用@Value注解来打印Spring Property的值。这个注解可以将配置文件中的值注入到Spring管理的Bean的字段中。

以下是一个简单的例子,演示如何打印Spring Property的值:

  1. application.propertiesapplication.yml中定义一个属性:

    # application.properties
    app.greeting=Hello, Spring!

    或者使用YAML格式:

    # application.yml
    app:
      greeting: Hello, Spring!

  2. 创建一个Spring组件,并使用@Value注解来注入属性值:

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
     
    @Component
    public class MyComponent {
     
        @Value("${app.greeting}")
        private String greeting;
     
        public void printGreeting() {
            System.out.println(greeting);
        }
    }

  3. 在你的主应用类或任何其他Spring管理的Bean中调用printGreeting方法:

  4. import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
     
    @Component
    public class MyAppRunner implements CommandLineRunner {
     
        private final MyComponent myComponent;
     
        @Autowired
        public MyAppRunner(MyComponent myComponent) {
            this.myComponent = myComponent;
        }
     
        @Override
        public void run(String... args) {
            myComponent.printGreeting();
        }
    }

    当你运行Spring Boot应用时,MyAppRunner会在命令行中打印出app.greeting属性的值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值