0

I've already read these questions and none of them worked:

Spring boot MVC - Unable to Autowire Repository in the service class

Why can't @Autowired a JPA repository - Spring boot + JPA

JpaRepository getting Null at service class

And also this one: https://www.baeldung.com/spring-autowired-field-null

Unfortunately, none of them worked.

What I have is:

Service interface:

@Service
public interface DayTradeService {
    public List<DayTrade> getDayTrades(List<NotaDeCorretagem> corretagens);
}

Service Implementation:

public class DayTradeServiceImpl implements DayTradeService {
    
    @Autowired
    private DayTradeRepository dayTradeRepository;
    
    @Override
    public List<DayTrade> getDayTrades(List<NotaDeCorretagem> corretagens) {
        // Several lines of code and some of them is trying to use dayTradeRepository.
    }
}

My DayTradeRepository:

@Repository
public interface DayTradeRepository extends JpaRepository<DayTrade, Integer> {}

Inside my DayTradeController (annotated with @Controller), I can use a dayTradeRepository with @Autowired. But inside a service class, I cannot use. I get this message:

Cannot invoke "meca.irpf.Repositories.DayTradeRepository.getDayTrades()" because "this.dayTradeRepository" is null"

How can I make it possible?

EDIT after I accepted Nikita's answer:

I didn't post the Controller code, but it didn't have the @Autowired for the service class DayTradeServiceImpl. That was the point I was missing. After Nikita pointing that, I could solve the problem.

Paulo
  • 1,458
  • 2
  • 12
  • 26

1 Answers1

3

You not need create new object. You have to call like this:

@Controller
@RequestMapping("/test")
public class TestController {

    @Autowired
    private DayTradeServiceImpl dayTradeService;


    @GetMapping(value = "/get")
    public void getTrades() {
        dayTradeService.getDayTrades(...);
    }

}

And set annotation @Service for DayTradeServiceImpl.

@Service
public class DayTradeServiceImpl implements DayTradeService {
    
    @Autowired
    private DayTradeRepository dayTradeRepository;
    
    @Override
    public List<DayTrade> getDayTrades(List<NotaDeCorretagem> corretagens) {
        // Several lines of code and some of them is trying to use dayTradeRepository.
    }
}

Spring framework use inversion of control, which has container for beans. For detect beans use annotation like: @Service, @Component, @Repository.

K.Nikita
  • 591
  • 4
  • 10
  • 2
    Moreover, it is called _Separation of Concerns_. You don't want to create a "Swiss Army knife" with your code. If you think about it, Swiss Army knives are good for small jobs. If the job gets to be too robust, most likely it will break. The same with code. You don't want a single class to do a bunch of jobs. You want classes to be responsible for one job and collaborate with other classes to execute multiple tasks. Good answer, K.Nikita! – hfontanez Jul 08 '22 at 01:51
  • hfontanez you can be sure this is not the case for my codes. And thank you for the advice. – Paulo Jul 08 '22 at 11:53
  • Nikita, thank you for you answer. It worked. So, what I was missing was injecting the Service class into the Controller class. After that, I can use the Repository class inside the Service class. – Paulo Jul 08 '22 at 12:19