Skip to main content

Properties and Primitive Insertion in Spring:

 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):
  1. Using Environment class object which is provided by spring
  2. 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.
How to load the properties file based on environment?
  • 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)
  1.  compile the code ,
  2.  execute the junit testcases,
  3.  execute code quality,
  4.  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
    1. a)dev, 
    2. b)test,
    3. 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
    1. sample.properties,
    2. sample-dev. properties,
    3. sample-test.properties,
    4. 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
    1. System.getProperty("")
    2.  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

    Popular posts from this blog

    Java Spring Framework:

    CORE: BASICS:           The new technology I had learned is the spring Framework core which is the base of the Spring technology or framework in Java. The main feature of the spring is that it is light weight where the run time is very less. Then the next advantage is the framework use the dependency injection in the object reference in Java where java is a OOPs programming language. The spring framework was found by rod johnson in the year of 2002 and licensed in the apache company in 2003. The other advantages of the Spring is that it can be used to build every application or software which is currently in the market. The framework supports: AOP Spring Boot Spring JDBC Spring MVC Spring EJB Spring Hibernate.           The diagram from the official website of the Spring is a good representation of the Spring Framework architecture which relies on the Spring Core. Fig 1: Spring Framework       ...

    Annotations and Scope in Spring:

     Basic Annotations: Component: It is used to create the bean without the configuration files like xml and java configurations The auto wired is used along with it to do the dependency injection Autowired: The Autowired annotation is used for the dependency injection the main types are the: Autowired in constructor Autowired in setter Autowired in the field Note : Using the field injection is not recommended. Fig 1. Autowired in constructor. Fig 2. Autowired in the field. Import: It is used to import other configuration files into the class file.  This will be helpful in the scenario of multiple configuration files are there. Fig 3. Import method usage Primary: The @Primary annotation can be present at the starting of a class where it represent the primary class. For example if we implement two classes using a interface and we gave two classes the @Component annotation the spring will be confused.  So when we add the primary tag on any one of them then that one will be cho...