Spring Boot version <version>2.2.0.RELEASE</version>
Description:
Parameter 2 of constructor in
com.shawn.foodrating.service.impl.AdServiceImpl
required a bean of typejava.lang.Integer
that could not be found.
Action:
Consider defining a bean of type 'java.lang.Integer' in your configuration.
@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;
Load property file
@SpringBootApplication
@PropertySource("classpath:app.properties")
public class FoodRatingApplication {
public static void main(String[] args) {
SpringApplication.run(FoodRatingApplication.class, args);
}
}
Not Sure what is wrong with it.
When you use Lombok's @AllArgsConstructor
it must create a constructor for all your fields, those annotated with @Value
and those that aren't.
Now Lombok doesn't even know anything about @Value
annotation of spring. So the generated constructor looks something like this:
public AdServiceImpl(AdRepository repository, FileService fileService, Integer DEFAULT_PageSize, String AD_IMAGE_PATH, String AD_IMAGE_URL) {
this.repository = repository;
....
}
You can run Delombok to see the actually generated code.
Spring on the other hand when sees a single constuctor tries to call it to create the bean ( AdServiceImpl
) in this case, and only after that iterates through its fields and inject data annotated by @Value
.
Now, when spring calls the constructor, it sees an integer (DEFAULT_PageSize), has no clue that its a value (and spring has to inject something brcause its a constructor injection), and throws an Exception.
So in terms of resolution:
Don't use all args constructor of lombok in this case and instead create a non-lombok constructor for AdRepository
and FileService
only)
Alternatively create a constructor with @Value annotated parameters instead of field injection (remove @Value on fields):
public AdServiceImpl(AdRepository repository, FileService fileService, @Value(${app.ad.DefaultPageSize}"} Integer DEFAULT_PageSize, @Value(...) String AD_IMAGE_PATH, @Value(...) String AD_IMAGE_URL) {
this.repository = repository;
....
}
Add to projeck lombok.config file with this lines:
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Value
The root cause is that you are using Lombok @AllArgsConstructor while some of the properties are populated by @Value(..) .
Replace the @AllArgsConstructor with @RequiredArgsConstructor and give it a try.
Note: Adding this comment for future references.
You can use in the following manner.
@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;
To use @Value
annotation, you have to use @Configuration
and @PropertySource
in the same class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.