i'm refactoring a codebase in order to have a single point of access to the repository I was following the guideline of Spring Boot with MongoTemplate
however I'm not able to build the project anymore, I'm thinking the problem being some wrong annotations or most probably an inheritance trouble
The Repository
@Repository
public interface Repository extends MongoRepositoryExtended<CustomType, String>,RepositoryTemplate {
(...
lots of custom queries
...)
}
The RepositoryTemplate(same package of repository):
public interface RepositoryTemplate {
List<String> distinctField(String fieldName);
}
the RepositoryTemplateImpl(same package of ServiceImpl):
public class RepositoryTemplateImpl implements RepositoryTemplate {
@Autowired
private MongoTemplate template;
@Override
public List<String> distinctField(String fieldName) {
List<String> fieldNames = template.findDistinct(fieldName, CustomType.class, String.class);
return fieldNames;
}
}
The ServiceImpl (which needs to implement the service first obv):
@Service
public class ServiceImpl implements Service {
private Repository repository;
@Autowired
ServiceImpl(Repository repository){
this.repository=repository;
}
(...)
@Override
public ResponseEntity<List<SpecialType>> specialMethod(String fieldNames) {
List<String> params = new ArrayList<>();
params = Arrays.asList(fieldNames.split("/"));
List<SpecialType> list = new ArrayList<>();
for (String field : params) {
(..some method..)
}
if (!found) {
continue;
}
result.setField(field);
**result.setValues(repository.distinctField(field));**
list.add(result);
}
No matter different changes I keep getting the same Unsatisfied Dependency Error
rg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Controller': Unsatisfied dependency expressed through field 'cService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ServiceImpl' defined in file [C:\Users\..]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Repository' defined in com.organization.microservice.repository.Repository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property distinctField found for type CustomType!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.3.3.jar:5.3.3]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.3.jar:5.3.3]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.3.jar:5.3.3]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1415) ~[spring-beans-5.3.3.jar:5.3.3]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:608) ~[spring-beans-5.3.3.jar:5.3.3]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531) ~[spring-beans-5.3.3.jar:5.3.3]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.3.jar:5.3.3]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.3.jar:5.3.3]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.3.jar:5.3.3]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.3.jar:5.3.3]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.3.jar:5.3.3]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:923) ~[spring-context-5.3.3.jar:5.3.3]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588) ~[spring-context-5.3.3.jar:5.3.3]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.2.jar:2.4.2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) ~[spring-boot-2.4.2.jar:2.4.2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.4.2.jar:2.4.2]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.2.jar:2.4.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.2.jar:2.4.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.2.jar:2.4.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.2.jar:2.4.2]
at com.com.organization.microservice.ManagerApplication.main(ManagerApplication.java:10) ~[classes/:na]
there is nothing in the CustomTypes similar to Field in naming
I tried changing the name of the custom query distinctField to getDistinctField , FindDistinctField ecc. but at this point I think I might have to look at some other direction. Any Suggestion or tip?