0
I’m having a problem where a Framework I’m using needs a default constructor, this one calling my Service class:
@Service
public class FuncionarioService {
private FuncionarioDAO dao;
public FuncionarioService(){
}
@Autowired
public FuncionarioService(FuncionarioDAO dao) {
this.dao = dao;
}
When calling the method below, the variable "dao" comes "null". When using another path that does not use the Framework, everything is normal, the variable is different from "null".
public List<Funcionario> obterFuncionarios(Hierarquia hierarquia){
List<Funcionario> listaFuncionarios = new ArrayList<Funcionario>();
listaFuncionarios = dao.obterFuncionarios(hierarquia);
return listaFuncionarios;
}
Thiago, I’ve tried adding @Autowired to the default constructor, but it needs at least 1 parameter, if not the error.
– Kennedy Anderson
Oops, sorry, you don’t need to add it as a constructor attribute. Try using it like this: "@Autowired" private Dao dao; E you can use the dao object freely as long as it is a "@Service" too.
– Thiago Vulcão
On my DAO I use the @Repository notation =\
– Kennedy Anderson
Perfect, that’s right. It works the same way, you must be using Spring Data.
– Thiago Vulcão
It didn’t work, still this like Null.
– Kennedy Anderson
Aren’t you injecting it into the right constructor? You’re taking the instantiated "dao" and using it directly, correct?
– Thiago Vulcão
In the mode I’m using, I’m injecting it into the constructor. But for a new situation, it doesn’t recognize it. @Autowired public Functionservice(Functionat dao) { this.dao = dao; }
– Kennedy Anderson
Abandon this injection in the constructor, if you are already injecting it in your class you already have it available for use, use as previously @Autowired private Dao dao; And use it normally.
– Thiago Vulcão
It didn’t work bro, it keeps going like null. @Autowired private Functiondao;
– Kennedy Anderson
Let’s go continue this discussion in chat.
– Thiago Vulcão
If your class is correct as @repositoy should work. You can update the post with the classes so I can understand how DAO is?
– Thiago Vulcão