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 :
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?
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.
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.
add bellow annotation in FileStorageProperties class:
@Component
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With