Spring 如何以及在何处在Jhipster生成的Sping应用程序中添加新bean
我使用Jhipster生成了我的Spring应用程序。现在我想为文件上传添加控制器,并为其添加StorageService。但当我运行我的应用程序时,它会向我发送以下消息 描述:com.kongresspring.myapp.web.rest.FileUploadResource中构造函数的参数0需要找不到类型为“com.kongresspring.myapp.service.StorageService”的bean。 措施:考虑在配置中定义一个“com.kongresspring.myapp.service.StorageService”类型的bean 我找不到beans.xml来添加新bean。我是春天的新手,所以可能有其他的方法来配置bean,我对whit不太熟悉。以下是我上传文件控制器的代码:Spring 如何以及在何处在Jhipster生成的Sping应用程序中添加新bean,spring,jhipster,Spring,Jhipster,我使用Jhipster生成了我的Spring应用程序。现在我想为文件上传添加控制器,并为其添加StorageService。但当我运行我的应用程序时,它会向我发送以下消息 描述:com.kongresspring.myapp.web.rest.FileUploadResource中构造函数的参数0需要找不到类型为“com.kongresspring.myapp.service.StorageService”的bean。 措施:考虑在配置中定义一个“com.kongresspring.myapp.
package com.kongresspring.myapp.web.rest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.kongresspring.myapp.service.StorageService;
@RestController
@RequestMapping("/api")
public class FileUploadResource {
private final Logger log = LoggerFactory.getLogger(FileUploadResource.class);
private final StorageService storageService;
@Autowired
public FileUploadResource(StorageService storageService) {
this.storageService = storageService;
}
/**
* POST uploadFile
*/
@PostMapping("/upload-file")
public String uploadFile(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
storageService.store(file);
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
return "success";
}
/**
* GET preview
*/
@GetMapping("/preview")
public String preview() {
return "preview";
}
}
这是我的StorageService代码:
package com.kongresspring.myapp.service;
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);
}
package com.kongresspring.myapp.service;
导入org.springframework.core.io.Resource;
导入org.springframework.web.multipart.MultipartFile;
导入java.nio.file.Path;
导入java.util.stream.stream;
公共接口存储服务{
void init();
无效存储(多部分文件);
流加载全部();
路径加载(字符串文件名);
资源loadAsResource(字符串文件名);
}
您可以为StorageService创建一个实现,并将其注释为@Service/@Component,spring将自动发现bean:
@Service
public class StorageServiceImpl implements StorageService {
void init(){// You code goes here/}
void store(MultipartFile file){///}
Stream<Path> loadAll(){///}
Path load(String filename){//}
Resource loadAsResource(String filename){///}
}
@服务
公共类StorageServiceImpl实现StorageService{
void init()
无效存储(多部分文件){/}
流loadAll(){//}
路径加载(字符串文件名){/}
资源加载ASResource(字符串文件名){/}
}