gpt4 book ai didi

spring-boot - 无法在 Spring 引导应用程序中禁用 ContextCredentialsAutoConfiguration

转载 作者:行者123 更新时间:2023-12-04 02:59:04 26 4
gpt4 key购买 nike

我正在开发一个 Spring Batch 应用程序,它应该只对特定的配置文件使用 aws cloud。目前我有一个使用 aws 的配置文件和另一个不应该使用的配置文件,因为它使用本地数据库、本地文件等在本地运行应用程序。(同时 AWS 配置文件将使用 RDS、S3 等)

对于使用 AWS 配置文件的配置,我有以下内容:

@Configuration
@Profile("!localDev")

public class FileReaderConfigAWS {

@Value("${cloud.aws.s3.bucket}")
private String amazonS3Bucket;

@Autowired
private ResourceLoader resourceLoader;

private static final Logger logger = LoggerFactory.getLogger(FileReaderConfigAWS.class);


@Bean
@StepScope
public FlatFileItemReader<Object> flatFileReader(@Value("#{jobParameters['inputFile']}") String inputFile, LineMapper
lineMapper) {
FlatFileItemReader<Object> flatFileItemReader = new FlatFileItemReader<>();

flatFileItemReader.setResource(resourceLoader.getResource("s3://" + this.amazonS3Bucket + "/" + inputFile));

flatFileItemReader.setLineMapper(lineMapper);

return flatFileItemReader;
}


@Bean
public AbstractFileValidator inputFileValidator() {
InputS3Validator inputS3Validator = new InputS3Validator();
inputS3Validator.setRequiredKeys(new String[]{InputFileSystemValidator.INPUT_FILE});
return inputS3Validator;
}

}

对于我的 localDev 配置文件,我有以下内容:

@Profile("localDev")
@Configuration
public class FileReaderConfigLocalDev {


@Bean
@StepScope
public FlatFileItemReader<Object> flatFileReader(@Value("#{jobParameters['inputFile']}") String inputFile, LineMapper lineMapper) {
FlatFileItemReader<Object> flatFileItemReader = new FlatFileItemReader<>();
flatFileItemReader.setResource(new FileSystemResource(inputFile));

flatFileItemReader.setLineMapper(lineMapper);

return flatFileItemReader;
}

@Bean
public AbstractFileValidator inputFileValidator() {
InputFileSystemValidator inputFileValidator = new InputFileSystemValidator();
inputFileValidator.setRequiredKeys(new String[]{InputFileSystemValidator.INPUT_FILE});
return inputFileValidator;
}

}

当我尝试使用 localDev 配置文件 (-Dspring.profiles.active=localDev) 运行 Spring Boot Main 类时,出现以下错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonS3': Invocation of init method failed; nested exception is java.lang.IllegalStateException: There is not EC2 meta data available, because the application is not running in the EC2 environment. Region detection is only possible if the application is running on a EC2 instance
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 18 common frames omitted
Caused by: java.lang.IllegalStateException: There is not EC2 meta data available, because the application is not running in the EC2 environment. Region detection is only possible if the application is running on a EC2 instance
at org.springframework.util.Assert.state(Assert.java:70) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.cloud.aws.core.region.Ec2MetadataRegionProvider.getRegion(Ec2MetadataRegionProvider.java:39) ~[spring-cloud-aws-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:98) ~[spring-cloud-aws-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:44) ~[spring-cloud-aws-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 25 common frames omitted

当我开始调试时,我发现一旦将 spring-cloud-aws-autoconfigure 添加到 pom.xml 中,似乎就无法在运行时为非 aws 环境禁用 aws autoconfigure。
我尝试了以下方法:

@EnableAutoConfiguration(exclude = {ContextCredentialsAutoConfiguration.class, ContextStackAutoConfiguration.class})

但还是不行,有什么想法吗?

提前致谢!

最佳答案

至于 spring cloud Brixton.SR7 的版本,要手动配置您将在 application.properties 中设置的区域(或 .xml、.yml,无论您使用什么)。此参数应该仅适用于您的开发环境,除非您确实需要指定它。

cloud.aws.region.static=us-east-1

我还必须为开发人员设置下一个属性,但也许您不需要它。

cloud.aws.stack.auto=false

更多引用信息在http://cloud.spring.io/spring-cloud-static/spring-cloud-aws/1.1.4.RELEASE/#_region_configuration

关于spring-boot - 无法在 Spring 引导应用程序中禁用 ContextCredentialsAutoConfiguration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44374346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com