在Jhipster生成的Sping应用程序中添加新bean的方式和位置

时间:2018-02-14 11:21:51

标签: spring jhipster

我使用Jhipster生成了我的Spring应用程序。现在我想为FilesUpload和StorageService添加控制器。但是,当我运行我的应用程序时,它会收到我的消息

  

描述:com.kongresspring.myapp.web.rest.FileUploadResource中构造函数的参数0需要一个类型为' com.kongresspring.myapp.service.StorageService'的bean。无法找到。   行动:考虑定义类型为' com.kongresspring.myapp.service.StorageService'的bean。在你的配置中。

我无法找到beans.xml来添加新bean。我是春天的新人,所以也许有其他一些配置bean的方法,我不熟悉。这是我上传文件控制器的代码:

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);



}

1 个答案:

答案 0 :(得分:1)

您可以为StorageService创建一个实现,并将其注释为@Service / @ Component,spring将自动发现该bean:

jar -tf boot-module/target/boot-module-0.0.1-SNAPSHOT.jar | grep changelog | wc -l
       0