当前位置: 首页> 娱乐> 影视 > @ConfigurationProperties加在方法上

@ConfigurationProperties加在方法上

时间:2025/7/12 4:41:04来源:https://blog.csdn.net/weixin_43349479/article/details/140919634 浏览次数:0次

@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 Setterspublic 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: MyAppversion: 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;@Autowiredpublic 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;@Autowiredpublic AppRunner(AppService appService) {this.appService = appService;}@Overridepublic 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;@Autowiredpublic 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 注解中指定的名称匹配。

关键字:@ConfigurationProperties加在方法上

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: