Spring @Value 注解与示例
Spring 中最重要的注解之一是 @Value 注解,它用于为变量和方法参数分配默认值。我们可以使用@Value注解读取spring环境变量和系统变量。它还支持 Spring 表达式语言 (SpEL)。它通常用于将值注入配置变量中,我们将在下面的示例中展示和解释。
实施:项目
Prerequisites:
- Spring – Injecting Literal Values By Setter Injection
- Spring – How to Load Literal Values from Properties File
第 1 步:首先,让我们创建一个简单的 Spring Application 并通过 setter 注入来注入字面量值。因此,创建一个简单的 Student 类,它具有三个属性 rollNo、name 和 age。为这两个属性创建 setter 方法和打印学生详细信息的简单方法。
档案:学生。Java
Java
// Java Program to Illustrate Student Class
// Class
public class Student {
// Class data members
private int rollNo;
private String name;
private int age;
// Setter
public void setRollNo(int rollNo)
{
// this keyword refers to current instance itself
this.rollNo = rollNo;
}
// Setter
public void setName(String name) { this.name = name; }
// Setter
public void setAge(int age) { this.age = age; }
// Method
public void display()
{
// Printing attributes of corresponding student
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
XML
Java
// Java Program to Illustrate Application Class
// Importing require classes
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// Application class
public class Main {
// Main driver method
public static void main(String[] args)
{
// Using ApplicationContext tom implement Spring IoC
ApplicationContext context
= new ClassPathXmlApplicationContext(
"beans.xml");
// Getting the bean student
Student student
= context.getBean("student", Student.class);
// Calling display() method
// inside main() method
student.display();
}
}
Java
// Java Program to Illustrate Student Class
// Importing required classes
import org.springframework.beans.factory.annotation.Value;
// Class
public class Student {
// Class data members
private int rollNo;
private String name;
private int age;
@Value("101")
// Setter
public void setRollNo(int rollNo)
{
this.rollNo = rollNo;
}
@Value("Anshul")
// Setter
public void setName(String name)
{
this.name = name;
}
@Value("25")
// Setter
public void setAge(int age)
{
this.age = age;
}
// Method
public void display()
{
// Print statement
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
XML
Java
// Java Program to Illustrate Student Class
// Importing required classes
import org.springframework.beans.factory.annotation.Value;
// Class
public class Student {
// Class data members
private int rollNo;
private String name;
private int age;
@Value("${student.rollNo}")
// Setter
public void setRollNo(int rollNo)
{
// this keyword refers to current instance itself
this.rollNo = rollNo;
}
@Value("${student.name}")
// Setter
public void setName(String name)
{
this.name = name;
}
@Value("${student.age}")
// Setter
public void setAge(int age)
{
this.age = age;
}
// Method
public void display()
{
// Printing attributes corresponding to student
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
XML
Java
// Java Program to Illustrate Student Class
// Importing required classes
import org.springframework.beans.factory.annotation.Value;
// Class
public class Student {
// Class data members
@Value("${student.rollNo}") private int rollNo;
@Value("${student.name}") private String name;
@Value("${student.age}") private int age;
// Method
public void display()
{
// Printing attributes corresponding to student
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
第2步:让我们在你的类路径中创建一个属性文件,并将文件命名为student-info.properties (对于这个例子我们这样命名,你可以根据你的需要命名)。在这个文件中,我们将编写如下内容:
student.rollNo = 101
student.name = Sagar
student.age = 20
第 3 步:现在让我们在 beans.xml 文件中创建一个 Student Bean,在 bean 中,您必须在
文件:bean.xml
XML
第4步:现在我们的bean准备好了。现在让我们创建一个类并在该类中定义 main() 方法。假设我们创建了一个名为 Main 的类,并且我们在该类中定义了 main() 方法。
文件:主要。Java
Java
// Java Program to Illustrate Application Class
// Importing require classes
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// Application class
public class Main {
// Main driver method
public static void main(String[] args)
{
// Using ApplicationContext tom implement Spring IoC
ApplicationContext context
= new ClassPathXmlApplicationContext(
"beans.xml");
// Getting the bean student
Student student
= context.getBean("student", Student.class);
// Calling display() method
// inside main() method
student.display();
}
}
第 5 步:现在运行您的 main() 方法,输出将如下所示。
输出:
Roll No: 101
Name: Sagar
Age: 20
代码说明:所以应用程序工作正常。现在再次来到beans.xml文件。在这个文件中,我们不想像我们所做的那样设置值。我们想使用一些注释来做同样的事情。这里@Value Annotation 出现了。所以我们可以修改我们的学生。 Java文件是这样的
档案:学生。Java
Java
// Java Program to Illustrate Student Class
// Importing required classes
import org.springframework.beans.factory.annotation.Value;
// Class
public class Student {
// Class data members
private int rollNo;
private String name;
private int age;
@Value("101")
// Setter
public void setRollNo(int rollNo)
{
this.rollNo = rollNo;
}
@Value("Anshul")
// Setter
public void setName(String name)
{
this.name = name;
}
@Value("25")
// Setter
public void setAge(int age)
{
this.age = age;
}
// Method
public void display()
{
// Print statement
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
第 6 步:在beans.xml文件中添加以下行
文件:bean.xml
XML
第 7 步:运行您的 main() 方法,输出将如下所示。
输出:
Roll No: 101
Name: Anshul
Age: 25
Note: Or you can also set the values dynamically from the properties file. We can modify our Student.java file something like this
档案:学生。Java
Java
// Java Program to Illustrate Student Class
// Importing required classes
import org.springframework.beans.factory.annotation.Value;
// Class
public class Student {
// Class data members
private int rollNo;
private String name;
private int age;
@Value("${student.rollNo}")
// Setter
public void setRollNo(int rollNo)
{
// this keyword refers to current instance itself
this.rollNo = rollNo;
}
@Value("${student.name}")
// Setter
public void setName(String name)
{
this.name = name;
}
@Value("${student.age}")
// Setter
public void setAge(int age)
{
this.age = age;
}
// Method
public void display()
{
// Printing attributes corresponding to student
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Tip: Don’t forget to add the below line inside your beans.xml file
文件:bean.xml
XML
运行你的 main() 方法,输出将是这样的。
输出:
Roll No: 101
Name: Sagar
Age: 20
现在让我们讨论一个关于 Spring @Value Annotation 的更有趣的概念。我们还可以在字段之前使用@Value 注释。无需创建 setter 方法。我们可以修改我们的学生。 Java文件是这样的
档案:学生。Java
Java
// Java Program to Illustrate Student Class
// Importing required classes
import org.springframework.beans.factory.annotation.Value;
// Class
public class Student {
// Class data members
@Value("${student.rollNo}") private int rollNo;
@Value("${student.name}") private String name;
@Value("${student.age}") private int age;
// Method
public void display()
{
// Printing attributes corresponding to student
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
所有其他的东西都应该保持不变。再次运行您的 main() 方法,输出将是这样的。
输出:
Roll No: 101
Name: Sagar
Age: 20