Maifee Ul Asad
Maifee Ul Asad

Reputation: 4579

Spring Boot can't autowire @ConfigurationProperties

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?

Upvotes: 44

Views: 57256

Answers (6)

prnv
prnv

Reputation: 11

Currently using spring boot version 3.2.6 Always use @Component with @ConfigurationProperties otherwise you will get "Consider defining a bean of type .." suggestion in logs as an action. Try to check if default constructor is added or not sometimes people forget to add it who is not using lombok.

Upvotes: 1

Prashant
Prashant

Reputation: 5423

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.

Upvotes: 91

Musa Baloyi
Musa Baloyi

Reputation: 617

You are missing an @EnableConfigurationProperties(FileStorageProperties.class); on your main class

Upvotes: 2

Siddharth
Siddharth

Reputation: 91

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.

Upvotes: 5

Dulaj Kulathunga
Dulaj Kulathunga

Reputation: 1250

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.

Upvotes: 4

David
David

Reputation: 80

add bellow annotation in FileStorageProperties class:

@Component

Upvotes: 2

Related Questions