jlefebure / spring-boot-starter-minio

Minio starter for Spring Boot
Apache License 2.0
149 stars 70 forks source link

No Beans MinioService found #27

Open jbcbezerra opened 1 year ago

jbcbezerra commented 1 year ago

When i am trying to run your example

@RestController
@RequestMapping("/files")
public class TestController {

    @Autowired
    private MinioService minioService;

    @GetMapping
    public List<Item> testMinio() throws MinioException {
        return minioService.list();
    }

    @GetMapping("/{object}")
    public void getObject(@PathVariable("object") String object, HttpServletResponse response) throws MinioException, IOException {
        InputStream inputStream = minioService.get(Path.of(object));
        InputStreamResource inputStreamResource = new InputStreamResource(inputStream);

        // Set the content type and attachment header.
        response.addHeader("Content-disposition", "attachment;filename=" + object);
        response.setContentType(URLConnection.guessContentTypeFromName(object));

        // Copy the stream to the response's output stream.
        IOUtils.copy(inputStream, response.getOutputStream());
        response.flushBuffer();
    }

    @PostMapping
    public void addAttachement(@RequestParam("file") MultipartFile file) {
        Path path = Path.of(file.getOriginalFilename());
        try {
            minioService.upload(path, file.getInputStream(), file.getContentType());
        } catch (MinioException e) {
            throw new IllegalStateException("The file cannot be upload on the internal storage. Please retry later", e);
        } catch (IOException e) {
            throw new IllegalStateException("The file cannot be read", e);
        }
    }
}

Spring does not start cause it cant find the Bean MinioService.

***************************
APPLICATION FAILED TO START
***************************

Description:

Field minioService in de.jbcbezerra.craftylab.test.TestController required a bean of type 'com.jlefebure.spring.boot.minio.MinioService' 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.jlefebure.spring.boot.minio.MinioService' in your configuration.
sployad commented 1 year ago

Same thing :(

Silvisgg commented 1 year ago

Hi all , @sployad @jbcbezerra I don't know how you are using the service, but consider to put an annotation like @Component. This way the application notices that this service exists.

Regards!

Liruwei commented 1 year ago

Same thing :( @Silvisgg
can you give me a full demo

mikopos commented 1 year ago

ComponentScan actually did the job for me

@SpringBootApplication
@ComponentScan({"com.jlefebure.spring.boot.minio.*"})
public class TestApplication {

  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}