Spring Boot无法使用@Value进行注入

问题描述 投票:0回答:1

Spring Boot版本<version>2.2.0.RELEASE</version>

错误如下:

说明:

com.shawn.foodrating.service.impl.AdServiceImpl中的构造函数的参数2需要找不到'java.lang.Integer'类型的bean。

动作:

考虑在您的配置中定义类型为'java.lang.Integer'的bean。

我的代码:

@Service
@Transactional(rollbackOn = Exception.class)
@AllArgsConstructor
public class AdServiceImpl implements AdService {
 private AdRepository repository;
 private FileService fileService;
 @Value("${app.ad.DefaultPageSize}")
 private Integer DEFAULT_PageSize;
 @Value("${app.ad.ImagePath}")
 private String AD_IMAGE_PATH;
 @Value("${app.ad.ImageUrl}")
 private String AD_IMAGE_URL;

加载属性文件


@SpringBootApplication
@PropertySource("classpath:app.properties")
public class FoodRatingApplication {
    public static void main(String[] args) {
        SpringApplication.run(FoodRatingApplication.class, args);
    }

}

不确定那是什么问题。

java spring spring-boot
1个回答
0
投票

您可以按以下方式使用。

@Service
@Configuration
@ComponentScan
@PropertySource("classpath:app.properties")
@Transactional(rollbackOn = Exception.class)
@AllArgsConstructor
public class AdServiceImpl implements AdService {
 private AdRepository repository;
 private FileService fileService;
 @Value("${app.ad.DefaultPageSize}")
 private Integer DEFAULT_PageSize;
 @Value("${app.ad.ImagePath}")
 private String AD_IMAGE_PATH;
 @Value("${app.ad.ImageUrl}")
 private String AD_IMAGE_URL;

要使用@Value批注,必须在同一类中使用@Configuration@PropertySource

© www.soinside.com 2019 - 2024. All rights reserved.