自动连线注解不适用于customClass扩展SimpleJpaRepository的其他包的变量

ubof19bj  于 8个月前  发布在  Spring
关注(0)|答案(1)|浏览(73)

我有Spring Boot 应用程序服务,在这个应用程序服务的处理程序类中使用包Utils的IDGenerator类,Autowired注解在初始化它时为IDGenerator的变量工作。我正在使用另一个名为custom-utils的包,在这个包中,类JtJPARepositoryImpl也使用IDGenerator,我在注解Autowired下定义了这个变量。但是当应用服务被启动或被访问时,这并不被初始化,这保持为空。想知道为什么它没有被初始化。
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);
          }

包:com.example.serviceapplication,类: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();
    }
}

软件包:com.example.utils,类: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;
    }
}
vwkv1x7d

vwkv1x7d1#

您的包裹:customer-utils,class:JtJPARepositoryImpl,但是在组件扫描中您指定了com.example.customutils

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

要解决这个问题,您需要确保正确的包包含在组件扫描的basePackages中,并且JtJPARepositoryImpl使用@Repository进行注解

相关问题