Constructor base dependency injection not working?

90 Views Asked by At

It's my first time encountering this error that says

`Description:

Parameter 0 of constructor in com.team.zah.blog.service.Impl.PostServiceImpl required a bean of type 'com.team.zah.blog.repository.PostRepository' that could not be found.

Action:

Consider defining a bean of type 'com.team.zah.blog.repository.PostRepository' in your configuration.`

I can't fathom the problem here as I've clearly used the lombok annotation: @AllArgsConstructor in my class

Class code here:

package com.team.zah.blog.service.Impl;

import com.team.zah.blog.dto.PostDTO;
import com.team.zah.blog.model.Post;
import com.team.zah.blog.repository.PostRepository;
import com.team.zah.blog.service.PostService;
import lombok.AllArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class PostServiceImpl implements PostService {

    private PostRepository postRepository;
    private ModelMapper modelMapper;

    @Override
    public PostDTO create(PostDTO postDTO) {
        Post post = modelMapper.map(postDTO, Post.class);
        Post savedPost = postRepository.save(post);
        return modelMapper.map(savedPost, PostDTO.class);
    }
}

I tried to manually do it like this

    @Autowired
    public void setPostRepository(PostRepository postRepository) {
        this.postRepository = postRepository;
    }
    
    @Autowired
    public void setModelMapper(ModelMapper modelMapper) {
        this.modelMapper = modelMapper;
    }

but I still get the problem of missing bean.

I wonder what the problem here is?

3

There are 3 best solutions below

0
DONGMO BERNARD GERAUD On BEST ANSWER

It works with @RequiredArgsConstructor and final fields.

  1. Repository
package com.team.zah.blog.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.team.zah.blog.model.Post;

public interface PosRepository extends JpaRepository<Post, Long> {

}
  1. Service
package com.team.zah.blog.service.Impl;

import com.team.zah.blog.dto.PostDTO;
import com.team.zah.blog.model.Post;
import com.team.zah.blog.repository.PostRepository;
import com.team.zah.blog.service.PostService;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class PostServiceImpl implements PostService {

    private final PostRepository postRepository;
    private final ModelMapper modelMapper;

    @Override
    public PostDTO create(PostDTO postDTO) {
        Post post = modelMapper.map(postDTO, Post.class);
        Post savedPost = postRepository.save(post);
        return modelMapper.map(savedPost, PostDTO.class);
    }
}
1
Joao Darwin On

Maybe this error is because on construct from lombok dont has que annotation @Autowired.

Why do you try this:

    @Service
public class PostServiceImpl implements PostService {

    private final PostRepository postRepository;
    private final ModelMapper modelMapper;

    @Autowired
    public PostServiceImpl(PostRepository postRepository, ModelMapper, modelMapper) {
        this.postRepository = postRepository;
        this.modelMapper = modelMapper;
    }
...
}
0
devash On

So I think I resolved the problem here. But I can't pin point exactly the issue that made me post this amateurish question. I'm suspecting dependency issue though I'm not sure. Or it could be because of my application.properties incorrect configuration.

Here is my application.properties:

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.database=postgresql

spring.datasource.url = jdbc:postgresql://localhost:5432/blog?autoReconnect=true
spring.datasource.username = postgres
spring.datasource.password = @@@@@@@@

spring.datasource.driverClassName = org.postgresql.Driver

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update

Thank you all for your help!