Skip to content

Instantly share code, notes, and snippets.

@rainoko
Last active March 9, 2019 11:20
Show Gist options
  • Save rainoko/c948a5c5b63fb33ba2d65361ca425512 to your computer and use it in GitHub Desktop.
Save rainoko/c948a5c5b63fb33ba2d65361ca425512 to your computer and use it in GitHub Desktop.
Simple spring boot application
package bytetrain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application implements CommandLineRunner {
private final EnvirontmentPrinter printer;
private static Logger LOG = LoggerFactory.getLogger(Application.class);
@Autowired
public Application(EnvirontmentPrinter printer) {
this.printer = printer;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) {
printer.print();
}
}
app:
param1: param1
package bytetrain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class EnvironmentPrinter implements EnvironmentAware {
private static Logger LOG = LoggerFactory.getLogger(Application.class);
private Environment environment;
private final Properties properties;
@Autowired
public EnvironmentPrinter(Properties properties) {
this.properties = properties;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
void print() {
String activeProfiles = String.join(",", environment.getActiveProfiles());
LOG.info("Active profiles: {}", activeProfiles);
LOG.info("Properties: {}", properties);
}
}
package bytetrain;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "app")
@Data
public class Properties {
private String param1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment