[英]How to use Spring @Value annotation to set values?
I recently started learning Spring core framework.最近开始学习Spring核心框架。 I feel confident using spring with XML but wanted to try only annotation-based programs but now I am stuck on this @Value annotation.
我对使用 spring 和 XML 充满信心,但只想尝试基于注释的程序,但现在我被困在这个 @Value 注释上。
I have a class Line extending Shape interface which uses the Point object as a dependency.我有一个 class 线扩展形状接口,它使用点 object 作为依赖项。 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.
object 点有两个整数 x 和 y,我尝试使用 @Value 设置值,但在运行程序时,值始终为 null。 I have tried making a lot of changes in driver class or appconfig but still not getting it.
我尝试在驱动程序 class 或 appconfig 中进行很多更改,但仍然没有得到它。
Point.class点.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线.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 ApplicationConfig.class
@ComponentScan
@Configuration
public class ApplicationConfig {
@Bean
public Line line() { return new Line(); }
@Bean
public Point point() { return new Point(); }
}
DrawingApp.class绘图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);
}
My output is always like this even though i have set values of x & y as 10 and 20 respectively:即使我将 x 和 y 的值分别设置为 10 和 20,我的 output 也总是这样:
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?如何使用注释设置点 object 的值?
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.
还支持处理程序方法 arguments 的动态解析 - 例如,在 Spring MVC 中。
A common use case is to inject values using #{systemProperties.myProp} style SpEL (Spring Expression Language) expressions.
一个常见的用例是使用 #{systemProperties.myProp} 样式的 SpEL(Spring 表达式语言)表达式注入值。 Alternatively,
或者,
The actual value expression such as #{systemProperties.myProp} or property placeholder > such as ${my.app.myProp}.
实际值表达式,例如 #{systemProperties.myProp} 或属性占位符 > 例如 ${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 或 application.yml 定义属性的值,并且可以使用
@Value
访问该值
application.properties应用程序属性
point.x = 10
point.y = 20
Access value using the @Value
as below使用
@Value
访问值,如下所示
@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.您在
@Value
注释中确定的值应该是从另一个来源获得的,例如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!在这里,如果您提供外部源,它将优先于硬编码的默认值!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.