spring系列-注解驱动原理及源码-属性赋值

44 阅读1分钟

目录

一、bean属性赋值

1、bean属性使用@Value赋值

2、bean属性通过配置文件赋值


一、bean属性赋值

1、bean属性使用@Value赋值

(1)编写bean类

package com.xiang.spring.bean;

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

public class Person {

    /**
     * 使用@Value赋值:
     * ① 、基本数值
     * ② 、可以写SpEL、#{}
     * ③ 、可以写${} 取出配置文件中的值(在运行环境变量里面的值)
      */
    @Value("zhangsan")
    private String name;
    @Value("#{20-2}")
    private Integer age;
    
    ...get set toString...
}

(2)配置类

package com.xiang.spring.config;

import com.xiang.spring.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MainConfigOfPropertyValues {

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

(3)测试方法测试结果

package com.xiang.spring.test;

import com.xiang.spring.bean.Person;
import com.xiang.spring.config.MainConfigOfPropertyValues;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class IOCTest_PropertyValues {

    @Test
    public void test01() {
        // 创建ioc容器,容器创建时,默认会将单例的bean都创建出来
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
        System.out.println("容器创建完成");
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : definitionNames) {
            System.out.println(name);
        }
        Person person = applicationContext.getBean("person", Person.class);
        System.out.println(person);
    }
}

// 运行结果
容器创建完成
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfigOfPropertyValues
person
Person{name='zhangsan', age=18}

2、bean属性通过配置文件赋值

(1)bean类

package com.xiang.spring.bean;

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

public class Person {

    /**
     * 使用@Value赋值:
     * ① 、基本数值
     * ② 、可以写SpEL、#{}
     * ③ 、可以写${} 取出配置文件中的值(在运行环境变量里面的值)
      */
    @Value("zhangsan")
    private String name;
    @Value("#{20-2}")
    private Integer age;
    @Value("${person.trueName}")
    private String trueName;

    ...get set toString...
}

(2)在resources目录下新增配置文件person.properties

person.trueName=zhangsanfeng

(3)配置类

package com.xiang.spring.config;

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

/**
* 使用@PropertySource读取外部配置文件中k/v保存到运行环境变量中,加载完外部配置文件之后,使用@Value("${}")获取
* 也可以使用@PropertySources指定多个@PropertySource
*/
@PropertySource(value = {"classpath:/person.properties"})
@Configuration
public class MainConfigOfPropertyValues {

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

(4)测试程序查看结果

@Test
public void test01() {
    // 创建ioc容器,容器创建时,默认会将单例的bean都创建出来
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
    System.out.println("容器创建完成");
    String[] definitionNames = applicationContext.getBeanDefinitionNames();
    for (String name : definitionNames) {
        System.out.println(name);
    }
    Person person = applicationContext.getBean("person", Person.class);
    System.out.println(person);

    // 在环境变量中也可以获取到配置文件中的值
    String property = applicationContext.getEnvironment().getProperty("person.trueName");
    System.out.println(property);
}

// 运行结果
容器创建完成
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfigOfPropertyValues
person
Person{name='zhangsan', age=18, trueName='zhangsanfeng'}
zhangsanfeng