-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
  • 732,580
  • 175
  • 1,330
  • 1,459
Somil Aseeja
  • 150
  • 8
  • 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 – Ryuzaki L 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
  • 2,073
  • 3
  • 21
  • 33
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
  • 1,012
  • 13
  • 23