Spring Boot+AWS SDK v2项目s3Config Bean创建失败求助
Spring Boot整合AWS SDK v2启动报错:BeanCreationException
问题描述
基于Java 17、Maven开发Spring Boot项目,整合AWS SDK v2时启动抛出以下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's3Config': Injection of autowired dependencies failed
相关代码与配置
DemoApplication.java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
S3Config.java
package com.example.demo.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; @Configuration public class S3Config { @Value("${region}") private String region; @Value("${accessKey}") private String accessKey; @Value("${secretKey}") private String secretKey; @Bean S3Client s3Client() { AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKey, secretKey); return S3Client.builder() .credentialsProvider(StaticCredentialsProvider.create(credentials)) .region(Region.of(region)) .build(); } }
S3Controller.java
package com.example.demo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import com.example.demo.services.S3Service; public class S3Controller { @Autowired private S3Service s3Service; @GetMapping("/listAllObjects") public ResponseEntity<List<String>> viewObjects() { return new ResponseEntity<>(s3Service.listObjects(),HttpStatus.OK); } }
S3Service.java
package com.example.demo.services; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.ListObjectsResponse; import software.amazon.awssdk.services.s3.model.S3Object; import java.util.List; import java.util.stream.Collectors; @Service public class S3Service { @Value("${bucketName}") private String bucketName; private final S3Client s3Client; public S3Service(S3Client s3Client) { this.s3Client = s3Client; } public List<String> listObjects() { ListObjectsResponse listObjectsResponse = s3Client.listObjects(b -> b.bucket(bucketName)); return listObjectsResponse.contents().stream() .map(S3Object::key) .collect(Collectors.toList()); } }
application.properties
spring.application.name=demo aws.region=`${region}` aws.accessKey=`${accessKey}` aws.secretKey=`${secretKey}` aws.bucketName=`${bucketName}` spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
修复方案
1. 修正配置键名不匹配问题
S3Config和S3Service中@Value注解引用的键名,与application.properties中的实际键名不一致,导致Spring无法注入属性。
修改S3Config:
@Value("${aws.region}") private String region; @Value("${aws.accessKey}") private String accessKey; @Value("${aws.secretKey}") private String secretKey;
修改S3Service:
@Value("${aws.bucketName}") private String bucketName;
2. 修复配置文件变量格式错误
application.properties中使用了反引号包裹环境变量,这会导致Spring无法正确解析,需去掉反引号:
spring.application.name=demo aws.region=${region} aws.accessKey=${accessKey} aws.secretKey=${secretKey} aws.bucketName=${bucketName} spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
如果不需要读取环境变量,直接填写实际AWS配置值即可,例如:
spring.application.name=demo aws.region=us-east-1 aws.accessKey=你的AWS访问密钥 aws.secretKey=你的AWS秘密密钥 aws.bucketName=你的S3存储桶名称 spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
3. 补充控制器注解
S3Controller缺少@RestController注解,Spring无法识别为控制器Bean,需添加:
@RestController public class S3Controller { // 原有代码不变 }
4. 确认依赖完整性
确保pom.xml中已引入正确的AWS SDK v2和Spring Boot依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3</artifactId> <version>2.20.0</version> <!-- 使用最新稳定版本 --> </dependency> </dependencies>
内容的提问来源于stack exchange,提问作者Aswin Shaji
相关产品推荐
相关产品推荐

