Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 596
Maifee Ul Asad Avatar asked Sep 16 '19 04:09

Maifee Ul Asad


3 Answers

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.

like image 65
Prashant Avatar answered Nov 20 '22 08:11

Prashant


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.

like image 20
Dulaj Kulathunga Avatar answered Nov 20 '22 07:11

Dulaj Kulathunga


add bellow annotation in FileStorageProperties class:

@Component
like image 2
David Avatar answered Nov 20 '22 08:11

David