JPA annotation @Autowired

Asked

Viewed 1,023 times

0

@Autowired is the same as extends?

Let’s take a small example:

package com.dendetech.services;

import com.dendetech.Entity.Participant; import com.dendetech.repositories.Participanterepository; import org.springframework.Beans.factory.Annotation.Autowired; import org.springframework.stereotype.Service;

@Service public class Participanteservice {

@Autowired
private ParticipanteRepository participanteRepository;

/*
 * Crud Operations
 */

<(Remainder omitted). >

1 answer

1


No, everything is one thing.

This annotation is automatically injecting an instance of ParticipanteRepository.

So you don’t need to use new ParticipanteRepository() or other method of rearing.

Extends is Heritage, in this case the Service would inherit from Repository.

  • On the line: >"@Autowired private Participantepository participantRepository;" Is the participating variable receiving the instance of the Participanterepository class object? Another question, it means that being the instantiated object in the Participanterepository class, I can use it in the service via @Autowired, as it is not possible to use an instantiated object elsewhere in the code?

  • 1 - Yes, in this line and variable it takes the instance of the class autamticamente. 2 - The object will be instantiated in the class that is declared. In this case it is declared within the Participanteservice class. If you need this instance elsewhere, you can enter public getter methods to then call parrticipanteService.getParticipanteRepository(). It does not make much sense to use this second way. In case of doubts post on another question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.