Property Files and Values:
- The Properties is a subclass of Hashtable class and it represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string. .properties is a file extension for files mainly used in Java-related technologies to store the configurable parameters of an application
- The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties.load() method of Properties class is convenient to load .properties file in the form of key-value pairs.
- In normal java syntax we will use the util.*. to load the properties, URL, etc.
- Then we use the load method to load any file and we can use that to retrieve properties.
- But in spring we can use only one annotation to load the whole properties file and the Annotation is @PropertySource:(file path).
- The main important job of the property file is to reduce the hardcoding of the values. In the example of Ink we define it as black ink but if we had a property file we can use that file for storing this information in a ordered manner and use it else where in our code.
Reading the values from Property(Environment class):
- Using Environment class object which is provided by spring
- Using @Value annotation.
- There is a class named as Environment which is a bean where we autowire it and this will start during application startup.
- We are reading from the properties file and not hardcoding.
- The java configuration file should have a class named as environment and should be autowired.
![]() |
Fig 1. The java config file. |
- The java config file is done with this thing then the method to return the final dependency is done with the environment object.
Fig 2. The final dependency. - The @PropertySource("classmate:") is used to import or read the file of the desired file and use that values as properties.
Value Annotation for same Property reading:
- We can directly use the @Value(${blackink.name}) in the code where it will directly take the value without the Environment class also.
- The Property Source is common we use it to load the properties file then we use either environment or the value annotation to access it.
- In Values if we want to set a default value then we can put ':' next to "blackink.name:value" and this for if the value is not present in the property file.
![]() |
Fig 3. @Value annotation in the class file. |
- The PropertySourcePlaceholderConfigurer class is a must class that must be present in the java configuration class so that we can use the values.
![]() |
Fig 4. The PropertySourcePlaceholderConfigurer class. |
Total Annotation Configuration for properties:
- All other classes in java configuration can be annotated but the PropertySourcePlaceholderConfigurer should be declared as a bean only in the java config in annotation.
![]() |
Fig 5. The Annotation Java Configuration file. |
Primitive Insertion:
- In insertion of primitive data using the xml or java or annotation configuration.
- In setter injection we use the <property name ="absolute name of variable in class" value = "value"> in the xml configuration
- But in the constructor injection we should use type as the parameter for example: <constructor-arg type = "int" value =111> and the order of the primitives in the dependent class should be also followed in the bean.
- When we want to do the collections it is easy using the setter injection in XML configuration.
![]() |
Fig 6. The DI of set. |
- In real time we will have different environments or profiles.
- Consider a developer had developed a application and deployed it. The jenkins will read the code form the source code from the git repository and internally use the maven and JDK.
- It will:(CI)
- compile the code ,
- execute the junit testcases,
- execute code quality,
- execute sonar reports.
- And if all the things are ok then it build the jar file
- Then it build the image in docker
- Then push the image into docker hub or ec2
- (CD)Then deploy the image into
- a)dev,
- b)test,
- c)uat.
- Then deploy into production.
If the image is deployed into the dev then it will read dev environment properties file and connect to dev environment Data Base.(We need devurl, username, password).
Similarly to connect test environment database it uses the test environment properties file.
- Therefore we have many environment properties files for many environments. For example
- sample.properties,
- sample-dev. properties,
- sample-test.properties,
- sample-prod.properties.
- These are the mandatory things in any project not only in the java.
How to find environment based properties file?
- Without spring we need to configure profile name in the server configuration. During the application startup all the values are set to JVM (Eg: System.setProperty("environment",dev))
- The example of application code is String env = System.getProperty("env");
- To do this much of code we had @Profile annotation in the Spring Framework.
- If an application is standalone we have options to set the environment using the :
- System.setProperty();
- spring.profiles.active=dev.
- If it is web application we can use:
- Tom cat -- catalina.properties ==> env = dev
- Jboss -- server.xml ==> env=dev
- Whenever we set the environment value during application startup it would be set to JVM.
- Types of value retrieval from the JVM
- System.getProperty("")
- Using @Profile annotation, this will internally call the the System.getProperty only.
- If the properties file is available under resource we can directly mention it in the @PropertySource but if it is in another folder inside source we should also mention that folder name also.
- The @Profile can be used at the class level also the method level.
- The main point to remember is that if we use the @Profile at class level then we need to declare as many class for as many profiles.
- But in the method level we can have only one configuration class where we can declare many methods for many profiles.
Comments
Post a Comment