侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 742 篇文章
  • 累计创建 64 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

Spring注解驱动开发(3)之组件赋值

zze
zze
2018-02-07 / 0 评论 / 0 点赞 / 653 阅读 / 4343 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

@Value & @PropertySource

使用 @Value 可以为属性赋基本数值,也可以通过语法 @Value("#{SpEL}") 通过 SpEL 表达式赋值,还可以通过 @Value("${属性名}") 取出配置文件(环境变量)中的值,而 @PropertySource 注解就可以帮我们读取属性文件中的属性到环境变量。

示例

1、创建测试的 properties 文件:

user.testKey=testValue

2、创建测试 Bean:

// com.springanno.pojo.User
import org.springframework.beans.factory.annotation.Value;

public class User {
    public User() {
    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Value("张三")
    private String name;
    @Value("#{19+3}")
    private Integer age;
    @Value("${user.testKey}")
    private String testKey;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

3、注册 bean 到容器:

// com.springanno.config.MainConfig

import com.springanno.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = {"classpath:user.properties"})  // 使用该注解将指定属性文件加载到环境变量
public class MainConfig {
    @Bean
    public User user() {
        return new User();
    }
}

4、测试:

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
User bean = applicationContext.getBean(User.class);
System.out.println(bean);
/*
User{name='张三', age=22, testKey='testValue'}
 */

@Profile

通过使用 @Profile 注解,Spring 可以实现根据当前环境动态的激活和切换一些列组件的功能。

示例

1、使用 @Profile 注解注册测试 bean 到容器:

// com.springanno.config.MainConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class MainConfig {
    /*
    指定环境了的组件只有在指定环境下运行时才会注册到容器中,不指定环境的组件任何环境都能注册到容器中,默认为 default 环境
    这里模拟三个环境下的数据源
     */

    @Profile("dev")
    @Bean("dataSource")
    public String devStr(){
        return "devDataSource";
    }

    @Profile("test")
    @Bean("dataSource")
    public String testStr(){
        return "testDataSource";
    }

    @Profile("prod")
    @Bean("dataSource")
    public String prodStr(){
        return "prodDataSource";
    }
}

2、测试通过 VM-Options 来指定运行环境:

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
Object dataSource = applicationContext.getBean("dataSource");
System.out.println(dataSource);

// 通过 VM-options 来指定运行环境
// -Dspring.profiles.active=dev 时,输出 devDataSource
// -Dspring.profiles.active=test 时,输出 testDataSource
// -Dspring.profiles.active=prod 时,输出 prodDataSource

3、测试通过编码指定运行环境:

String profileStr = "test";
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
// 指定运行环境
applicationContext.getEnvironment().setActiveProfiles(profileStr);
applicationContext.register(MainConfig.class);
applicationContext.refresh();
Object dataSource = applicationContext.getBean("dataSource");
System.out.println(dataSource);

/*
编码指定运行环境
    profileStr="test" 时,输出 testDataSource
    profileStr="prod" 时,输出 prodDataSource
    profileStr="dev" 时,输出 devDataSource
 */
0

评论区