我正在开发一个 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 自动配置。
我尝试了以下方法:
@EnableAutoConfiguration(exclude = {ContextCredentialsAutoConfiguration.class, ContextStackAutoConfiguration.class})
但仍然无法正常工作,有什么想法吗?
提前致谢!