简体   繁体   中英

How to use Spring @Value annotation to set values?

I recently started learning Spring core framework. I feel confident using spring with XML but wanted to try only annotation-based programs but now I am stuck on this @Value annotation.

I have a class Line extending Shape interface which uses the Point object as a dependency. The Point object has two ints x and y which I am trying to set values using @Value but on running the program the values always come as null. I have tried making a lot of changes in driver class or appconfig but still not getting it.

Point.class

    @Component
    public class Point {
    @Value("10")
    int x;
    @Value("20")
    int y;      
        
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getX() {
        return x;
    }       
    public void setX(int x) {
        this.x = x;
    }       
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }       
    @Override
    public String toString() {
        return "Point [x=" + x + ", y=" + y + "]";
    }   
}

Line.class

@Component
public class Line implements Shape {
    private int size;
    private String type;
    @Autowired
    Point pointA;  
    @Autowired
    Point pointB;       
    
    public Line() {
    }
    public Line(int size, String type, Point pointA, Point pointB) {
        super();
        this.size = size;
        this.type = type;
        this.pointA = pointA;
        this.pointB = pointB;
    }

    @Override
    public String toString() {
        return "Triangle [size=" + size + ", type=" + type + ", pointA=" + pointA + ", pointB=" + pointB + "]";
    }

    /*
         getters and setters.....
    */
}

ApplicationConfig.class

    @ComponentScan
    @Configuration
    public class ApplicationConfig {    
          
          @Bean 
          public Line line() { return new Line(); }
          
          @Bean 
          public Point point() { return new Point(); }   
         
    }

DrawingApp.class

public class DrawingApp {

    public static void main(String[] args) {
                
        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class); 

        Shape line = context.getBean(Line.class);
        System.out.println(line);
        
    }

My output is always like this even though i have set values of x & y as 10 and 20 respectively:

Line [size=0, type=null, pointA=Point [x=0, y=0], pointB=Point [x=0, y=0]]

How can I set the values of the point object Using annotations?

@Value

Annotation used at the field or method/constructor parameter level that indicates a default value expression for the annotated element. Typically used for expression-driven or property-driven dependency injection. Also supported for dynamic resolution of handler method arguments — for example, in Spring MVC.

A common use case is to inject values using #{systemProperties.myProp} style SpEL (Spring Expression Language) expressions. Alternatively,

The actual value expression such as #{systemProperties.myProp} or property placeholder > such as ${my.app.myProp}.

Usage

You can define the value of your property using the application.properties or application.yml and you can access the value using the @Value

application.properties

point.x = 10
point.y = 20

Access value using the @Value as below

@Value("${point.x}")
private String x;

@Value("${point.y}")
private String y;

First, it is not a good practice to use harcoded values within the code block. You may provide it as an external source to your software/application.

The value you determine in @Value annotation is supposed to be obtained from another source, eg, application.yml. However, you can hardcode a desired value as well, but as a fallback default value against the external source.

@Value("${point.x:12}")
private int x;
@Value("${point.y:14}")
private int y;

Here, if you provide the external source, it will have precedence over the hardcoded default value!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM