@Value的使用

本文通过实例展示了Spring Boot中使用@Value注解进行常量注入、SpEL表达式计算以及从properties文件中读取属性值的方法。在Bird实体类中,分别注入了名称、年龄和颜色属性,并在测试类中展示了如何通过配置类和属性文件创建并打印Bird对象。测试结果显示,Bird对象的属性被正确地注入和计算。
部署运行你感兴趣的模型镜像
①常量注入
@Value("喜鹊")
private String name;
②SpEL表达式
@Value("#{20-8}")
private Integer age;
③从properties文件中读取属性值
@Value("${bird.colour}")
private  String colour;

1、实体类

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

public class Bird {
    @Value("喜鹊")
    private String name;

    @Value("#{20-8}")
    private Integer age;

    @Value("${bird.colour}")
    private  String colour;

    @Override
    public String toString() {
        return "Bird{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", colour='" + colour + '\'' +
                '}';
    }
}

 2、test.properties

bird.colour = red

3、配置类

import com.it.huaxin.vo.Bird;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@PropertySource(value = {"classpath:/test.properties"})
@Configuration
public class BirdConfig {
    @Bean
    public Bird bird () {
        return new Bird();
    }
}

4、测试类

import com.it.huaxin.config.BirdConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class BirdTest {
    public static void main(String [] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BirdConfig.class);
        Object bird = applicationContext.getBean("bird");
        System.out.println(bird);
        System.out.println("容器创建完成");
        applicationContext.close();
    }
}

5、测试结果

Bird{name='喜鹊', age=12, colour='red'}
容器创建完成

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

`@Value`是Spring框架中的一个注解,用于将值注入到bean的属性中。它可以注入静态值、来自配置文件的值,甚至可以使用Spring表达式语言(SpEL)进行复杂的表达式计算[^2]。以下是其常见的使用方法: ### 注入静态值 可以直接在`@Value`注解中指定一个静态值,将其注入到bean的属性中。 ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class StaticValueExample { @Value("Hello, World!") private String message; public String getMessage() { return message; } } ``` ### 注入配置文件中的值 通常与Spring的配置文件(如`application.properties`或`application.yml`)一起使用,将配置文件中的值注入到Spring Bean中。 #### `application.properties`示例 ```properties app.name=My Application ``` ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class PropertyValueExample { @Value("${app.name}") private String appName; public String getAppName() { return appName; } } ``` #### `application.yml`示例 ```yaml app: name: My Application ``` ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class YamlValueExample { @Value("${app.name}") private String appName; public String getAppName() { return appName; } } ``` ### 使用Spring表达式语言(SpEL) `@Value`注解支持使用SpEL进行复杂的表达式计算。 ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class SpelValueExample { @Value("#{2 + 3}") private int sum; public int getSum() { return sum; } } ``` ### 设置默认值 当配置文件中没有指定某个属性时,可以为`@Value`注解设置默认值。 ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class DefaultValueExample { @Value("${unknown.property:Default Value}") private String defaultValue; public String getDefaultValue() { return defaultValue; } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青春1314

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值