DEV Community

realNameHidden
realNameHidden

Posted on

@Value Annotaion Example in Spring Boot

Image description

application.properties

spring.application.name=ValueAnnotation
hospital.name=Apollo
hospital.age=30

lab.abPrice=5000
lab.dbPrice=1300
lab.cdPrice=1300

Enter fullscreen mode Exit fullscreen mode

Hospital.java

package com.example.demo.beans;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Hospital {

 @Value("KIMS")//hard Coding
 private String name;

 @Value("30")//hard Coding
 private String rank;

 @Value("${hospital.name}")//collecting from properties file
 private String name1;

 @Value("${hospital.age}")//collecting from properties file
 private int age;

 @Value("${path}")//injecting the env... variables
 private String pathData;

 @Value("${os.name}")//injecting System property value
 private String os;

 @Value("#{lInfo}")//Using SPEL to perform injection to Object Type property
 private LabTestsInfo info;

 @Value("#{lInfo.abPrice+lInfo.dbPrice}")//SPEL based Arithmetic Operation and Injection
 private float labTestsBillAmount;

 @Override
 public String toString() {
  return "Hospital [name=" + name + ", rank=" + rank + ", name1=" + name1 + ", age=" + age + ", pathData="
    + pathData + ", os=" + os + ", info=" + info + ", labTestsBillAmount=" + labTestsBillAmount + "]";
 }
}

Enter fullscreen mode Exit fullscreen mode

LabTestsInfo.java

package com.example.demo.beans;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("lInfo")
public class LabTestsInfo {

 @Value("${lab.abPrice}")
 private float abPrice;

 @Value("${lab.dbPrice}")
 private float dbPrice;

 @Value("${lab.cdPrice}")
 private float cdPrice;

 public float getAbPrice() {
  return abPrice;
 }

 public void setAbPrice(float abPrice) {
  this.abPrice = abPrice;
 }

 public float getDbPrice() {
  return dbPrice;
 }

 public void setDbPrice(float dbPrice) {
  this.dbPrice = dbPrice;
 }

 public float getCdPrice() {
  return cdPrice;
 }

 public void setCdPrice(float cdPrice) {
  this.cdPrice = cdPrice;
 }

 @Override
 public String toString() {
  return "LabTestsInfo [abPrice=" + abPrice + ", dbPrice=" + dbPrice + ", cdPrice=" + cdPrice + "]";
 }

}

Enter fullscreen mode Exit fullscreen mode

ValueAnnotationApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.example.demo.beans.Hospital;

@SpringBootApplication
public class ValueAnnotationApplication {

 public static void main(String[] args) {

  //get IOC Container
  ConfigurableApplicationContext ctx = SpringApplication.run(ValueAnnotationApplication.class, args);

  //get the Spring bean class Object
  Hospital hospital = ctx.getBean("hospital",Hospital.class);
  System.out.println("Spring Bean Class Obj Data :: "+hospital);

  //close the container
  ctx.close();
 }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)