-1

I want to know how to pick spring option arguments like

--server.port , --spring.config.name 

in a java class.

Basically I want to know the value of this argument at run time to load some property

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Somil Aseeja
  • 152
  • 7
  • Possible duplicate of [How to access a value defined in the application.properties file in Spring Boot](https://stackoverflow.com/questions/30528255/how-to-access-a-value-defined-in-the-application-properties-file-in-spring-boot) – dZ. Apr 22 '19 at 13:06
  • show the code that you have – Deadpool Apr 28 '19 at 14:23

2 Answers2

0

You can access them in your application's main() method. A great blog about this topic covers it in detail. Following is how you can do it.

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        for(String arg:args) {
            System.out.println(arg);
        }
        SpringApplication.run(Application.class, args);
    }
}
Adil Khalil
  • 1,903
  • 2
  • 19
  • 31
0

Please try using spring org.springframework.core.env.Environment,

public class MyService {

    @Autowired
    private Environment env;

    public String getPropertyValue(String key) {
            return env.getProperty(key);
    }
}

OR

In application-<env>.propeties (if using spring.profiles) else application.properties

myapp.property=007

In your class :

@Value("${myapp.property}")
private String myProperty;
Akhil S Kamath
  • 994
  • 13
  • 23