Reputation: 191
I'm walking through a pivotal 'guide' project, my first time uploading files in Spring.
I've implemented the code exactly as the guide specifies but still am getting the following error:
Parameter 0 of constructor in hello.storage.FileSystemStorageService required a bean of type 'hello.storage.StorageProperties' that could not be found.
Consider defining a bean of type 'hello.storage.StorageProperties' in your configuration.
I've reviewed many so posts and it seems the common thread is to move my 'application' class to the root package, but its already there. Here is my project: UploadingFiles1Application.java
package hello;
// multiple imports
@SpringBootApplication
public class UploadingFiles1Application {
public static void main(String[] args) {
SpringApplication.run(UploadingFiles1Application.class, args);
}
@Bean
CommandLineRunner init(StorageService storageService) {
return (args) -> {
storageService.deleteAll();
storageService.init();
};
}
}
And here's my Service:
package hello.storage;
import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;
import java.nio.file.Path;
import java.util.stream.Stream;
public interface StorageService {
void init();
void store(MultipartFile file);
Stream<Path> loadAll();
Path load(String filename);
Resource loadAsResource(String filename);
void deleteAll();
}
And its implementation:
package hello.storage;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
@Service
public class FileSystemStorageService implements StorageService {
private final Path rootLocation;
@Autowired
public FileSystemStorageService(StorageProperties properties) {
this.rootLocation = Paths.get(properties.getLocation());
}
@Override
public void store(MultipartFile file) {
String filename = StringUtils.cleanPath(file.getOriginalFilename());
try {
if (file.isEmpty()) {
throw new StorageException("Failed to store empty file " + filename);
}
if (filename.contains("..")) {
// This is a security check
throw new StorageException(
"Cannot store file with relative path outside current directory "
+ filename);
}
try (InputStream inputStream = file.getInputStream()) {
Files.copy(inputStream, this.rootLocation.resolve(filename),
StandardCopyOption.REPLACE_EXISTING);
}
}
catch (IOException e) {
throw new StorageException("Failed to store file " + filename, e);
}
}
@Override
public Stream<Path> loadAll() {
try {
return Files.walk(this.rootLocation, 1)
.filter(path -> !path.equals(this.rootLocation))
.map(this.rootLocation::relativize);
}
catch (IOException e) {
throw new StorageException("Failed to read stored files", e);
}
}
@Override
public Path load(String filename) {
return rootLocation.resolve(filename);
}
@Override
public Resource loadAsResource(String filename) {
try {
Path file = load(filename);
Resource resource = new UrlResource(file.toUri());
if (resource.exists() || resource.isReadable()) {
return resource;
}
else {
throw new StorageFileNotFoundException(
"Could not read file: " + filename);
}
}
catch (MalformedURLException e) {
throw new StorageFileNotFoundException("Could not read file: " + filename, e);
}
}
@Override
public void deleteAll() {
FileSystemUtils.deleteRecursively(rootLocation.toFile());
}
@Override
public void init() {
try {
Files.createDirectories(rootLocation);
}
catch (IOException e) {
throw new StorageException("Could not initialize storage", e);
}
}
}
What am I missing?? Thanks in advance.
Upvotes: 0
Views: 9044
Reputation: 3678
First of all i do not know if your StorageProperties is a spring component or just a simple java class! I am going to suppose that it is a spring component :
@Component
public class StorageProperties {
...
}
In the FileSystemStorageService, you replace the constructor by a postConstruct method :
@Service
public class FileSystemStorageService implements StorageService {
private Path rootLocation;
@Autowired
StorageProperties properties;
@PostConstruct
public void FileSystemStorageService() {
this.rootLocation = Paths.get(properties.getLocation());
....
}
Upvotes: 2