Well such questions with different flavors are asked during the interviews.
Funny thing is that interviewers would also have got the answers after searching over the
internet when they got such issues/requirements during their work & such questions are not
about common sense also which you can expect most people should be aware about.
But all these interviewers want to show their fake intelligence & want to show that the
candidate is lower than them intellectually but the reality can be quite opposite.
Well I am here trying to put all the ways set/define the properties while using SpringBoot, as
per my understanding & search over the internet.
So solutions will not be the concrete or final ones but these are more like pointers which you
can look further, as per your requirements.
Refer also : Properties with Spring and Spring Boot | Baeldung
Configuring a DataSource Programmatically in Spring Boot | Baeldung
1) We automatically get default file to provide the application level properties-
application.properties
Give the general & non-confidential properties here. And check the constraints as you will
need to provide these properties before the start of the application & if you change then to use that you will need to restart the application.
2) To provide the confidential properties like passwords etc or you don't want to have the
properties in your code base of the application, then look for how to extrernalize the
properties, as one solution there to externalize the properties file in Git. So look for Git
actions/workflows to use the secrets in the properties file. Here we can have the
encrypted values.
3) If you want to create a separate properties file with your custom name & want to use its
properties also, may be you want to create separate properties files for different systems to
be used in your application. So for this case look for @PropertySource
& @PropertySources. And I think it is better to use these annotations in your application's
main Java class. If you want to specifically refer these properties in your code then check
@Value
4) If you are using Profile concept to use different proeprties files for different environments
like Dev, Test, Stage, Prod etc, then it is better to start the application in respective
environments with the required properties mentioned in command line arguments like if
you want to start the application on port 3000 in Prod environment & for other
environments respective people can decide on the port but you don't want to change Prod
port by anyone by mistake, so better use such properties as VM argurments to start the
application as properties used in VM arguments will take the preference over other ways, I
think, so better check this behavior also.
we can give port using : -Dserver.port=3000
5) Suppose you want to decide the port programatically & don't want to hard code in
properties file/s. Taken from StackOverflow
For this also there are different ways-
a) SpringApplication application = new SpringApplication(<Root class>);
Properties properties = new Properties();
properties.put("server.port", 1024);
application.setDefaultProperties(properties);
application.run(args);
b) new SpringApplicationBuilder(TheatreApplication.class)
.properties(properties) // provide the properties like this or make call to some method
.build()
.run(args);
c) Implement ApplicationListener<ApplicationEnvironmentPreparedEvent> & provide
implementation for onApplicationEvent(ApplicationEnvironmentPreparedEvent event)
Then register the class in src/main/resources/META-INF/spring.factories:
org.springframework.context.ApplicationListener=<My pkg.My Listener class>
You can see a suggestion to use SocketUtils for your test class, but SocketUtils is deprecated
so don't use it. Check SocketUtils (Spring Framework 5.3.20 API), as it also suggest to let
Spring find the available port.
Now you may ask that, if server is being started on random available port then how will I know
that port in my code, as if we do fix the port then we can read that port value via-
a) @Value("${server.port}")
b) @Autowired private ServerProperties serverProperties;
serverProperties.getPort();
Otherwise, how to get the port value in our code?
So for this also there are 2 ways as given on
Get the Running Port in Spring Boot | Baeldung
a) @Autowired
private ServletWebServerApplicationContext webServerAppCtxt;
webServerAppCtxt.getWebServer().getPort();
b) Using ServletWebServerInitializedEvent event
event.getWebServer().getPort();
But in above all ways, I am hard coding the port, but if I don't want it.
Either I can use an updated list of available ports or can pick one in the current system
One interesting solution is given on : Find available Port
So one can try this solution to use the random available port on the system to use in the
application. So this way you will not be hard coding any port but check for the security while
using any such random port.
Funny thing is that interviewers would also have got the answers after searching over the
internet when they got such issues/requirements during their work & such questions are not
about common sense also which you can expect most people should be aware about.
But all these interviewers want to show their fake intelligence & want to show that the
candidate is lower than them intellectually but the reality can be quite opposite.
Well I am here trying to put all the ways set/define the properties while using SpringBoot, as
per my understanding & search over the internet.
So solutions will not be the concrete or final ones but these are more like pointers which you
can look further, as per your requirements.
Refer also : Properties with Spring and Spring Boot | Baeldung
Configuring a DataSource Programmatically in Spring Boot | Baeldung
1) We automatically get default file to provide the application level properties-
application.properties
Give the general & non-confidential properties here. And check the constraints as you will
need to provide these properties before the start of the application & if you change then to use that you will need to restart the application.
2) To provide the confidential properties like passwords etc or you don't want to have the
properties in your code base of the application, then look for how to extrernalize the
properties, as one solution there to externalize the properties file in Git. So look for Git
actions/workflows to use the secrets in the properties file. Here we can have the
encrypted values.
3) If you want to create a separate properties file with your custom name & want to use its
properties also, may be you want to create separate properties files for different systems to
be used in your application. So for this case look for @PropertySource
& @PropertySources. And I think it is better to use these annotations in your application's
main Java class. If you want to specifically refer these properties in your code then check
@Value
4) If you are using Profile concept to use different proeprties files for different environments
like Dev, Test, Stage, Prod etc, then it is better to start the application in respective
environments with the required properties mentioned in command line arguments like if
you want to start the application on port 3000 in Prod environment & for other
environments respective people can decide on the port but you don't want to change Prod
port by anyone by mistake, so better use such properties as VM argurments to start the
application as properties used in VM arguments will take the preference over other ways, I
think, so better check this behavior also.
we can give port using : -Dserver.port=3000
5) Suppose you want to decide the port programatically & don't want to hard code in
properties file/s. Taken from StackOverflow
For this also there are different ways-
a) SpringApplication application = new SpringApplication(<Root class>);
Properties properties = new Properties();
properties.put("server.port", 1024);
application.setDefaultProperties(properties);
application.run(args);
b) new SpringApplicationBuilder(TheatreApplication.class)
.properties(properties) // provide the properties like this or make call to some method
.build()
.run(args);
c) Implement ApplicationListener<ApplicationEnvironmentPreparedEvent> & provide
implementation for onApplicationEvent(ApplicationEnvironmentPreparedEvent event)
Then register the class in src/main/resources/META-INF/spring.factories:
org.springframework.context.ApplicationListener=<My pkg.My Listener class>
You can see a suggestion to use SocketUtils for your test class, but SocketUtils is deprecated
so don't use it. Check SocketUtils (Spring Framework 5.3.20 API), as it also suggest to let
Spring find the available port.
Now you may ask that, if server is being started on random available port then how will I know
that port in my code, as if we do fix the port then we can read that port value via-
a) @Value("${server.port}")
b) @Autowired private ServerProperties serverProperties;
serverProperties.getPort();
Otherwise, how to get the port value in our code?
So for this also there are 2 ways as given on
Get the Running Port in Spring Boot | Baeldung
a) @Autowired
private ServletWebServerApplicationContext webServerAppCtxt;
webServerAppCtxt.getWebServer().getPort();
b) Using ServletWebServerInitializedEvent event
event.getWebServer().getPort();
But in above all ways, I am hard coding the port, but if I don't want it.
Either I can use an updated list of available ports or can pick one in the current system
One interesting solution is given on : Find available Port
So one can try this solution to use the random available port on the system to use in the
application. So this way you will not be hard coding any port but check for the security while
using any such random port.