如何在Java Spring框架中登录到控制台工作

huangapple go评论73阅读模式
英文:

How does logging into the console works in the Java Spring framework

问题

以下是翻译的内容:

我有一个非常简单的Spring应用程序,但是我无法将System.out.println语句打印到控制台。

这是我在主应用程序文件中打印在.yml文件中设置的环境变量的地方:

import path.config.Config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {

	@Autowired
	private Config config;

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(MainApplication.class);
		app.run();
	}

	public void run(String... args) throws Exception {
		System.out.println("env: " + config.getEnv());
	}

}

配置文件如下:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class Config {
    private String env;

    public void setEnv(String env) {
        this.env = env;
    }

    public String getEnv() {
        return this.env;
    }
}

最后是属性的yml文件:

spring: 
    profiles.active: dev
    h2:
        console:
            enabled: true
---
spring:
    profiles: dev
env: dev
---
spring:
    profiles: test
env: test
---
spring:
    profiles: prod
env: prod

Spring应用程序可以构建并正常运行,然而,我无法在终端中看到env变量的显示。我看过一些人使用带有请求端点的控制器来在浏览器中调试环境变量。那是唯一的选择吗?

英文:

I have a very simple Spring application, but I can't not get a System.out.println statement to print into the console.

This is the main app file where I am printing an env variable set in a .yml file

import path.config.Config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {

	@Autowired
	private Config config;

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(MainApplication.class);
		app.run();
	}

	public void run(String... args) throws Exception {
		System.out.println("env: " + config.getEnv());
	}

}

The configuration file looks like this:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class Config {
    private String env;

    public void setEnv(String env) {
        this.env = env;
    }

    public String getEnv() {
        return this.env;
    }
}

Finally the properties yml file

spring: 
    profiles.active: dev
    h2:
        console:
            enabled: true
---
spring:
    profiles: dev
env: dev
---
spring:
    profiles: test
env: test
---
spring:
    profiles: prod
env: prod

The Spring app builds a runs fine, however, I can't see the env variable to show in the terminal. I have seen examples of people using Controllers with a Request endpoint just to debug the environment variables in the browser. Is that the only option?

答案1

得分: 2

对你的代码做了一些更改,你不需要使用 new 关键字来启动 Spring 应用程序,你可以直接使用静态 run 方法

public static void main(String[] args) {
    SpringApplication.run(MainApplication.class);
}

其次,在 MainApplication 中的 run 方法只有在该类实现了 CommandLineRunner 接口时才会执行

@SpringBootApplication
public class MainApplication implements CommandLineRunner {

    @Autowired
    private Config config;

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class);
    }

    public void run(String... args) throws Exception {
        System.out.println("env: " + config.getEnv());
    }
}
英文:

Couple of changes to your code, you don't need to use new keyword for starting spring application, you can directly use static run method

 public static void main(String[] args) {
    SpringApplication.run(MainApplication.class);
  
}

Second thing the run method in MainApplication will only execute if that class implements CommandLineRunner

@SpringBootApplication
public class MainApplication implements CommandLineRunner {

    @Autowired
    private Config config;

    public static void main(String[] args) {
          SpringApplication.run(MainApplication.class);
    }

    public void run(String... args) throws Exception {
          System.out.println("env: " + config.getEnv());
     }

  }

huangapple
  • 本文由 发表于 2020年8月21日 02:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63510847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定