32

Here is my FileStorageProperties class:

 @Data
 @ConfigurationProperties(prefix = "file")
 public class FileStorageProperties {
       private String uploadDir;
 }

This gives me saying : not registered via @enableconfigurationproperties or marked as spring component.

And here is my FileStorageService :

@Service
public class FileStorageService {

private final Path fileStorageLocation;

@Autowired
public FileStorageService(FileStorageProperties fileStorageProperties) {
    this.fileStorageLocation = Paths.get(fileStorageProperties.getUploadDir())
            .toAbsolutePath().normalize();

    try {
        Files.createDirectories(this.fileStorageLocation);
    } catch (Exception ex) {
        throw new FileStorageException("Could not create the directory where the uploaded files will be stored.", ex);
    }
}

public String storeFile(MultipartFile file) {
    // Normalize file name
    String fileName = StringUtils.cleanPath(file.getOriginalFilename());

    try {
        // Check if the file's name contains invalid characters
        if(fileName.contains("..")) {
            throw new FileStorageException("Sorry! Filename contains invalid path sequence " + fileName);
        }

        // Copy file to the target location (Replacing existing file with the same name)
        Path targetLocation = this.fileStorageLocation.resolve(fileName);
        Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);

        return fileName;
    } catch (IOException ex) {
        throw new FileStorageException("Could not store file " + fileName + ". Please try again!", ex);
    }
}

public Resource loadFileAsResource(String fileName) {
    try {
        Path filePath = this.fileStorageLocation.resolve(fileName).normalize();
        Resource resource = new UrlResource(filePath.toUri());
        if(resource.exists()) {
            return resource;
        } else {
            throw new MyFileNotFoundException("File not found " + fileName);
        }
    } catch (MalformedURLException ex) {
        throw new MyFileNotFoundException("File not found " + fileName, ex);
    }
}
}

This gives me error saying : could not autowire no beans of type found.

And here is my project structure :

Spring Boot can't autowire @ConfigurationProperties - project structure

And when I try to run it, it gives me :


APPLICATION FAILED TO START

Description:

Parameter 0 of constructor in com.mua.cse616.Service.FileStorageService required a bean of type 'com.mua.cse616.Property.FileStorageProperties' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.mua.cse616.Property.FileStorageProperties' in your configuration.


How can I resolve this?

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86

4 Answers4

71

This is expected as @ConfigurationProperties does not make a class a Spring Component. Mark the class with @Component and it should work. Note that a class can only be injected if it is a Component.

Edit: From Spring 2.2+ (Reference) @ConfigurationProperties scanning Classes annotated with @ConfigurationProperties can now be found via classpath scanning as an alternative to using @EnableConfigurationProperties or @Component. Add @ConfigurationPropertiesScan to your application to enable scanning.

Prashant
  • 4,775
  • 3
  • 28
  • 47
  • 3
    But Baeldung saying that ["As, of Spring Boot 2.2, Spring finds and registers @ConfigurationProperties classes via classpath scanning."](https://www.baeldung.com/configuration-properties-in-spring-boot#1-spring-boot-22) – fuat Jun 30 '20 at 13:21
  • 1
    The documentation does point out that you need to provide `@ConfigurationPropertiesScan`. I am updating the answer accordingly – Prashant Jun 30 '20 at 15:18
  • @Prashant It also points out "In addition, we can use the ConfigurationPropertiesScan annotation to scan custom locations for configuration property classes:". So does that mean if i don't need to scan custom locations, i can omit this scan annotation?? – Nick Wills Jan 29 '22 at 07:57
4

Try to annotate with @ConfigurationProperties and @Component

In here , Spring Boot @ConfigurationProperties is annotation for externalized configuration.if you are trying to inject property value from a property file to a class, you can add @ConfigurationProperties at a class level with stereotype annotations such as @Component or add @ConfigurationProperties to a @Bean method.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
2

add bellow annotation in FileStorageProperties class:

@Component
Prashant
  • 4,775
  • 3
  • 28
  • 47
David
  • 78
  • 3
1

In my case I had @ConfigurationProperties("limits-service") in my class but missed @ConfigurationPropertiesScan in the main application class LimitsServiceApplication

adding @ConfigurationPropertiesScan to LimitsServiceApplication worked.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Siddharth
  • 41
  • 6