Home » Java » Spring – NoSuchBeanDefinitionException when autowiring Repository into Service

Spring – NoSuchBeanDefinitionException when autowiring Repository into Service

Posted by: admin June 30, 2018 Leave a comment

Questions:

I am trying to Autowire a repository into a Service that’s called from a Controller, but I keep getting the following error message:

Error creating bean with name 'mktPlace2PedidoService' defined in VFS resource [\"/home/davidgarcia/wildfly-11.0.0.Final/standalone/deployments/issuer.war/WEB-INF/classes/br/com/sinergico/service/mktplace2/MktPlace2PedidoService.class\"]: Unsatisfied dependency expressed through constructor argument with index 0 of type [br.com.sinergico.repository.mktplace2.MktPlace2PedidoRepository]: No qualifying bean of type [br.com.sinergico.repository.mktplace2.MktPlace2PedidoRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [br.com.sinergico.repository.mktplace2.MktPlace2PedidoRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

The odd thing is that I have many other repositories being used in the exact same way, but not giving this problem…

Here is my call to the repository in the Service constructor

package br.com.sinergico.service.mktplace2;

@Service
public class MktPlace2PedidoService extends GenericService<MktPlace2Pedido, Long> {

    private MktPlace2PedidoRepository repository;

    @Autowired
    public MktPlace2PedidoService(MktPlace2PedidoRepository repository) {
        super(repository);
        this.repository = repository;
    }

    //other methods
}

Here is the Repository

package br.com.sinergico.repository.mktplace2;

public interface MktPlace2PedidoRepository extends JpaRepository<MktPlace2Pedido, Long> {
    //some methods   
}

And here is the Entity declaration

@Entity
@Table(name="pedido", schema = "marketplace")
@SequenceGenerator(name="hibernate_sequence",    sequenceName="marketplace.hibernate_sequence",initialValue=1, allocationSize=1)
public class MktPlace2Pedido implements Serializable{

    private static final long serialVersionUID = 45381343130121532L;

    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hibernate_sequence")
    private Long id;

    //other columns

}

I have tried including the @Repository annotation to the repository, but it didn’t do anything.

Can anyone help? I’ve spent hours on this and can’t figure it out…

Answers: