0

I am encountering a baffling @Autowired issue, which only occurred after I added a dependency to one of my own projects.

Here is the situation:

I am extending a service, which has autowired repositories. Here's the simplified version:

package com.opt.custom.domain;

import com.opt.repo.RepositoryOne;
import com.opt.repo.RepositoryTwo;

    @Primary
    @Service("CustomDomainServiceImpl")
    public class CustomDomainServiceImpl extends DomainServiceImpl {

        private RepositoryOne repo1;
        private RepositorTwo repo2;

        @Autowired
        public CustomDomainServiceImpl(RepositoryOne repo1
                , RepositorTwo repo2) {
            super(repo1, repo2);
        }
        ....
    }

This has been working fine - the @Autowired tags grab the repositories fine, whether or not I include them as attributes, as I don't use them except to feed into the parent service.

However, I have created another service (with its own service, repositories, etc.). When I add this new service as a dependency of the above project (in the POM file), the @Autowired annotations in the above code stop working, even if I don't reference any of the services, repos, etc. in this class. Specifically, the error is:

Parameter 0 of constructor in com.opt.custom.domain.CustomDomainServiceImpl required a bean of type 'com.opt.repo.RepositoryOne' that could not be found.
Action:
Consider defining a bean of type 'com.opt.repo.RepositoryOne' in your configuration.

I don't know how simply adding a dependency (while not using anything from it) can cause this issue.

I have tried adding a @ComponentScan to the above class:

@ComponentScan(basePackages = {"com.opt.repo"})

But this has not helped.

If it helps, this is the top-level class in the Maven project that I am adding as a dependency:

package com.opt.new.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@SpringBootApplication
@EntityScan(basePackages = { "com.someotherpackage.persistence.*" })
public class PersistenceClasses {

    public static void main(String[] args) {
        SpringApplication.run(PersistenceClasses .class, args);
    }

}

Thank you for any insights you can provide.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Aphorism44
  • 67
  • 11

1 Answers1

0

@ComponentScan annotation should be added to the spring boot application class. In your case, its PersistenceClasses. Also, make sure to have a @Repository annotation on your RepositoryOne class

@Repository is a spring stereotype, identifying spring components in an application. More information on it can be found here

priteshbaviskar
  • 2,139
  • 2
  • 20
  • 27