Autowired annotation not working on variable of another package for customClass extending SimpleJpaRepository

78 views Asked by At

I have spring boot application service, In the handler class of this application service using IDGenerator class of package Utils, Autowired annotations works for Variable of the IDGenerator in initialising it. I am using another package called custom-utils, in which class JtJPARepositoryImpl, which uses IDGenerator as well, i have defined this variable under the annotation Autowired. but this is not getting initialised when application-service is started or when accessed, this is being remained null. Want to know why is it not getting initialised.

package: com.example.customutils, class: JtJPARepositoryImpl

      public class JtJPARepositoryImpl<T extends BaseEntity, ID extends Serializable>
              extends SimpleJpaRepository<T, ID> implements JtJPARepository<T, ID>, Serializable {
      
          @Autowired
          private IDGenerator idGenerator;
      
          @Autowired
          public JtJPARepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) {
              super(entityInformation, entityManager);
          } 

package: com.example.serviceapplication, class: ServiceApplication

@Slf4j
@SpringBootApplication
@ImportResource({"classpath:applicationContext.xml"})
@ComponentScan(basePackages = {
        "com.example.serviceapplication",
        "com.example.utils",
        "com.example.customutils"
})
@EnableJpaRepositories(basePackages = "com.jumbotail.serviceapplication.repository",
        repositoryBaseClass = JtJPARepositoryImpl.class)
public class ServiceApplication {

    @Inject
    private ServiceProperties ServiceProperties;

    private final ApplicationContext applicationContext;

    public ServiceApplication(ApplicationContext applicationContext) {

        ServiceProperties baseConfiguration = new ServiceProperties();
        baseConfiguration.setDynamoDBConfiguration(new DynamoDBConfiguration(10000,10000));

        this.applicationContext = applicationContext;
        DynamoDBClient dynamoDBClient = new DynamoDBClient(baseConfiguration);

     ((GenericWebApplicationContext)this.applicationContext).registerBean(BaseConfiguration.class,baseConfiguration);
        ((GenericWebApplicationContext)this.applicationContext).registerBean(DynamoDBClient.class,baseConfiguration);
    }

    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
        log.info("Started Service");
    }

    @PostConstruct
    public void init() {
        log.info("Initializing application with Service properties- {}", ServiceProperties);
        ApplicationConfiguration.initialize(ServiceProperties.getEnvironment());
    }

    @Bean
    public BaseConfiguration getBaseConfiguration() {
        BaseConfiguration baseConfiguration = new BaseConfiguration();
        baseConfiguration.setEnvironment(ServiceProperties.getEnvironment());
        return baseConfiguration;
    }

    @Bean
    public HealthCheckConfiguration healthCheckConfiguration() {
        return serviceProperties.getHealthCheckConfiguration();
    }
}

package: com.example.utils, class: IDGenerator

@Slf4j
@Component
@Qualifier("IDGenerator")
public class IDGenerator {

    private static final String IDCOUNTER_TABLE = "IDCounter";
    private static final String IDCOUNTER_KEY_NAME = "namespace";
    private static final String IDCOUNTER_VALUE_NAME = "counter";
    private static final String IDCOUNTER_VERSION_NAME = "version";
    private final DynamoDBClient dynamoDBClient;

    public IDGenerator(DynamoDBClient dynamoDBClient) {
        this.dynamoDBClient = dynamoDBClient;
    }
}
1

There are 1 answers

2
mmeqbel On

your package: customer-utils, class: JtJPARepositoryImpl, however in component scan you specified com.example.customutils

@ComponentScan(basePackages = {
        "com.example.serviceapplication",
        "com.example.utils",
        "com.example.customutils"
})

to fix this you need to make sure that the right packages are included in basePackages of the component scan, and the JtJPARepositoryImpl is annotated with @Repository