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
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
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);
}
}
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;