资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用Spring怎么对组件进行赋值

使用Spring怎么对组件进行赋值?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

在宜秀等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站制作、网站设计 网站设计制作定制网站开发,公司网站建设,企业网站建设,成都品牌网站建设,成都全网营销推广,外贸营销网站建设,宜秀网站建设费用合理。

1. @Value & @PropertySource

1.1 使用方式

@PropertySource:读取外部配置文件中的 k/v 保存到运行的环境变量中;加载完外部的配置文件以后使用 ${} 取出配置文件的值

@Value:赋值

  • 基本数值

  • 可以写 SpEL,#{}

  • 可以写 ${};取出配置文件【properties】中的值(在运行环境变量里面的值)

1.2 代码

1. Person

@Data
@Slf4j
@ToString
public class Person {

  @Value("#{001+001}")
  private Long id;

  @Value("zs")
  private String name;

  @Value("${person.address}")
  private String address;

  @Value("${person.age:19}")
  private Integer age;

}

2. SpringConfig

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

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

}

3. PropertiesTest

public class PropertiesTest {

  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    Person person = context.getBean(Person.class);
    System.out.println(person);
    //Person(id=2, name=zs, address=上海市, age=19)
  }

}

2. @Profile

2.1 使用方式

@Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件

加了环境标识的 bean,只有这个环境被激活的时候才能注册到容器中。默认是 default 环境

写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效

没有标注环境标识的 bean 在任何环境下都是加载的

2.2 代码

1. Message

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Message {

  private String label = null;

}

2. SpringConfig

@Configuration
public class SpringConfig {

  @Profile("default")
  @Bean
  public Message defaultMessage() {
    return new Message("default");
  }

  @Profile("dev")
  @Bean
  public Message devMessage() {
    return new Message("dev");
  }

  @Profile("test")
  @Bean
  public Message testMessage() {
    return new Message("test");
  }

  @Profile("prod")
  @Bean
  public Message prodMessage() {
    return new Message("prod");
  }

}

3. 激活方式 1

激活 profile:设置虚拟机参数 -Dspring.profiles.active=dev,test

public class ProfileTest {

  public static void main(String[] args) {
    
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    String[] names = context.getBeanNamesForType(Message.class);
    for (String name : names) {
      System.out.println(name);
    }
    //devMessage
    //prodMessage
  }

}

4. 激活方式 2

public class ProfileTest {

  public static void main(String[] args) {
    //1. 创建一个 AnnotationConfigApplicationContext
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    //2. 设置需要激活的环境
    context.getEnvironment().setActiveProfiles("dev", "prod");
    //3. 注册主配置类
    context.register(SpringConfig.class);
    //4. 刷新容器
    context.refresh();
    String[] names = context.getBeanNamesForType(Message.class);
    for (String name : names) {
      System.out.println(name);
    }
    //testMessage
    //prodMessage
  }

}

看完上述内容,你们掌握使用Spring怎么对组件进行赋值的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网站名称:使用Spring怎么对组件进行赋值
网页网址:http://cdkjz.cn/article/pdggpj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220