1

Can someone tell me please what I'm doing wrong, I'm trying to develop a little application but I just simply can't Autowire a class in the controller, I have been researching and I Know that for Autowire works, my class must be in a child package of my @SpringBootApplication annotation, or I should specify in @ComponentScan annotation the package of my class, but no matters what I do I still getting the same error:

enter image description here

This is my project structure:

enter image description here

package net.spring.auditoria;

import org.springframework.boot.SpringApplication;T
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AuditoriaApplication {

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

}

Controller:

package net.spring.auditoria.bll;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class AuditoriaController {

    @Autowired
    private AuditoriaManagement am;
    
    private final Logger LOGGER = LoggerFactory.getLogger(AuditoriaController.class);
    
    @GetMapping("/auditar")
    public String auditar() {
        LOGGER.info("auditar()");
        return "main";
    }
    
}

https://github.com/jlpm-mex/auditoria

Vy Do
  • 46,709
  • 59
  • 215
  • 313
Jose
  • 116
  • 1
  • 12

1 Answers1

3

Because AuditoriaManagement is not a Spring bean or component.

enter image description here

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • You were right, when should I use @Component annotation? – Jose Oct 03 '20 at 06:27
  • yes, or something like `@Bean` , `@Controller` , `@Component` , `@Repository` etc. – Vy Do Oct 03 '20 at 06:31
  • 2
    @Jose: @Component is a generic stereotype for the components. @Servcie, @Repository, @Controller extends from @Component: `@Service, @Controller, @Repository = {@Component + some more special functionality}` https://stackoverflow.com/a/6897038/8207836 – huytmb Oct 03 '20 at 06:44