Spring框架注解开发(IDEA)——关于@Value解读(超详细)

本文介绍了如何在Spring Boot中使用@Value注解从注释直接、SpEL表达式以及配置文件中获取值,为Bean的属性进行快速赋值。通过示例展示了创建Bean、配置类和测试类的过程,并演示了不同方式的赋值效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@Value注释最简单的就是利用备注快速给Bean赋值

一.基本数值

二.可以写SpEL #{ }

三  ${} 取出配置文件的值

1.1新建类Person

package com.Bean;

public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

1.2新建Config类

package com.Config;

import com.Bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfigValue {


    @Bean
    public Person person(){
        return new Person();
    }

}

1.3新建测试类

package com;

import com.Bean.Person;
import com.Config.MyConfigValue;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestIOC {
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfigValue.class);
@Test
    public void test() {
        printBean(applicationContext);
        System.out.println("===============");
        Person person =(Person)applicationContext.getBean("person");
        System.out.println(person);

}

    private void printBean(AnnotationConfigApplicationContext annotationConfigApplicationContext) {
        String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }
    }
}

1.4 运行后

我们发现现在的Person属性都是为null 

我们给Person类添加Value,并赋值

@Value("kangkang")
private String name;
@Value("#{20-2}")
private int age;

运行后 

成功用Value实现快速赋值 

 

我们在来使用第三种方法赋值用${},利用配置文件赋值

在Resouce下 新建配置文件

 在Config中添加@PropertySource添加配置文件来源

package com.Config;

import com.Bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value={"classpath:/person.properties"})
public class MyConfigValue {


    @Bean
    public Person person(){
        return new Person();
    }

}

给Perosn类用${Person.name}赋值

package com.Bean;

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

public class Person {
    @Value("${Person.name}")
    private String name;
    @Value("#{20-2}")
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

运行后

赋值成功

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值