繁体   English   中英

如何使用 Spring @Value 注解设置值?

[英]How to use Spring @Value annotation to set values?

最近开始学习Spring核心框架。 我对使用 spring 和 XML 充满信心,但只想尝试基于注释的程序,但现在我被困在这个 @Value 注释上。

我有一个 class 线扩展形状接口,它使用点 object 作为依赖项。 object 点有两个整数 x 和 y,我尝试使用 @Value 设置值,但在运行程序时,值始终为 null。 我尝试在驱动程序 class 或 appconfig 中进行很多更改,但仍然没有得到它。

点.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 + "]";
    }   
}

线.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(); }   
         
    }

绘图App.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);
        
    }

即使我将 x 和 y 的值分别设置为 10 和 20,我的 output 也总是这样:

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

如何使用注释设置点 object 的值?

@价值

在字段或方法/构造函数参数级别使用的注释,指示注释元素的默认值表达式。 通常用于表达式驱动或属性驱动的依赖注入。 还支持处理程序方法 arguments 的动态解析 - 例如,在 Spring MVC 中。

一个常见的用例是使用 #{systemProperties.myProp} 样式的 SpEL(Spring 表达式语言)表达式注入值。 或者,

实际值表达式,例如 #{systemProperties.myProp} 或属性占位符 > 例如 ${my.app.myProp}。

用法

您可以使用 application.properties 或 application.yml 定义属性的值,并且可以使用@Value访问该值

应用程序属性

point.x = 10
point.y = 20

使用@Value访问值,如下所示

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

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

首先,在代码块中使用硬编码值不是一个好习惯。 您可以将其作为软件/应用程序的外部来源提供。

您在@Value注释中确定的值应该是从另一个来源获得的,例如application.yml。 但是,您也可以硬编码所需的值,但作为针对外部源的后备默认值。

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

在这里,如果您提供外部源,它将优先于硬编码的默认值!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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