@ConfigurationProperties加在方法上

@ConfigurationProperties注解通常用于将外部配置文件(如application.propertiesapplication.yml)中的属性映射到Java类中。它通常加在类上,但也可以加在方法上。加在方法上时,通常与@Bean注解一起使用,以便将配置属性注入到Spring容器中的Bean中。

示例:将@ConfigurationProperties加在方法上

以下是一个完整的示例,展示如何将@ConfigurationProperties注解加在方法上,并将配置属性注入到Spring容器中的Bean中。

1. 创建配置属性类

首先,创建一个简单的配置属性类,用于映射外部配置文件中的属性。

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private String name;
    private String version;

    // Getters and Setters
    public String getName() {
        return name;
    }

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

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}
2. 创建配置类

创建一个配置类,并在方法上使用@ConfigurationProperties注解和@Bean注解。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@Configuration
@EnableConfigurationProperties
public class AppConfig {

    @Bean
    @ConfigurationProperties(prefix = "app")
    public AppProperties appProperties() {
        return new AppProperties();
    }
}
3. 配置文件

application.propertiesapplication.yml文件中添加配置属性。

application.properties
app.name=MyApp
app.version=1.0.0
application.yml
app:
  name: MyApp
  version: 1.0.0
4. 使用配置属性

在你的应用程序中,你可以通过注入AppProperties来使用这些配置属性。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppService {

    private final AppProperties appProperties;

    @Autowired
    public AppService(AppProperties appProperties) {
        this.appProperties = appProperties;
    }

    public void printAppProperties() {
        System.out.println("App Name: " + appProperties.getName());
        System.out.println("App Version: " + appProperties.getVersion());
    }
}
5. 运行应用

在你的应用程序中调用AppServiceprintAppProperties方法来打印配置属性。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class AppRunner implements CommandLineRunner {

    private final AppService appService;

    @Autowired
    public AppRunner(AppService appService) {
        this.appService = appService;
    }

    @Override
    public void run(String... args) throws Exception {
        appService.printAppProperties();
    }
}

总结

  • 配置属性类:创建一个简单的类,用于映射外部配置文件中的属性。
  • 配置类:在方法上使用@ConfigurationProperties注解和@Bean注解,将配置属性注入到Spring容器中的Bean中。
  • 配置文件:在application.propertiesapplication.yml文件中添加配置属性。
  • 使用配置属性:通过注入配置属性类来使用这些配置属性。

在Spring框架中,@Autowired 注解通常用于自动注入依赖。虽然它通常用于类的字段或构造函数上,但也可以用于方法参数上。@Autowired 加在形参上的用法也是合法的,并且可以与 @Qualifier 注解一起使用,以指定具体的bean。

以下是一个示例,展示了如何在方法参数上使用 @Autowired@Qualifier 注解:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.KafkaProperties;
import org.springframework.stereotype.Service;

@Service
public class KafkaService {

    private final KafkaTemplate<String, String> kafkaTemplate;
    private final KafkaProperties kafkaProperties;

    @Autowired
    public KafkaService(KafkaTemplate<String, String> kafkaTemplate,
                        @Qualifier("kafkaProperties") KafkaProperties kafkaProperties) {
        this.kafkaTemplate = kafkaTemplate;
        this.kafkaProperties = kafkaProperties;
    }

    // 其他方法
}

在这个示例中,KafkaService 的构造函数使用了 @Autowired 注解来自动注入 KafkaTemplateKafkaProperties。其中,KafkaProperties 使用了 @Qualifier 注解来指定具体的bean。

注意:在使用 @Autowired@Qualifier 注解时,确保 Spring 容器中存在相应的bean,并且bean的名称与 @Qualifier 注解中指定的名称匹配。

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你这个代码我看不懂

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

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

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

打赏作者

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

抵扣说明:

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

余额充值