Details with @Autowired and Down object

Asked

Viewed 73 times

0

There’s been a conflict of concepts with me.

I have here a service called: AtividadeService

There are some lines that catch my eye:

import com.dendetech.entity.Atividade;

Why import Entity? If it only fits the AtividadeRepository?

@Service
public class AtividadeService {

    @Autowired
    private AtividadeRepository atividadeRepository;

    private AtividadeDAO atividadeRepository = new AtividadeDAO();

Why a new object of type AtividadeDown, if you already have @Autowired?

(Remainder omitted).

1 answer

2

In fact, this isn’t making much sense:

@Autowired
private AtividadeRepository atividadeRepository;

private AtividadeDAO atividadeRepository = new AtividadeDAO();

In fact, you wouldn’t even pass the compilation, since you are declaring two different objects with the same name within the scope of the same class.

Another thing is that you are probably making some confusion of concepts. Either you use a repository, or you use a DAO. Repositories are entities working at the collection level. Daos are entities working at the level of database technology.

Also, the difference between DAO (Data Access Object) and DAL (Data Access Layer) not just a letter: DAO is the object that is part of a DAL. Usually, both the DAO and a repository return objects of type DTO (Data Transfer Object).

Having understood this, we can move on to the @Autowired.

@Autowired is the annotation for Dependency injection. It indicates to the framework that the marked object will be managed by framework (its entire life cycle). It makes sense for a Service, but it doesn’t make much sense for an entity, since usually an entity (most of the time) is an anemic object that doesn’t require life cycle management (definition of DTO).

So for this case, I think it would be interesting to remove the DAO statement.

  • 1

    Doesn’t make any sense @Autowired for entities, at least I can’t find a context that does. I already made this grotesque mistake, obviously the states of the entity was shared among all users.

  • @Danielamorais I think this question is a case of complete confusion of concepts :)

Browser other questions tagged

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