java - Spring引导 Autowiring 在配置类中给出null

标签 java spring spring-boot

我正在尝试在配置类中 Autowiring 一个类,如下所示:

    @Configuration
    public class GemfireConfig {
    @Autowired
    private CloudCacheDataLoader loader;

    @Bean(name = "VariableRangeCache")
            ClientRegionFactoryBean<Long, VariableRange> variableRangeRegion(
                    @Autowired ClientCache gemfireCache) {
                ClientRegionFactoryBean<Long, VariableRange> orderRegion = new ClientRegionFactoryBean<Long, VariableRange>();
                orderRegion.setCache(gemfireCache);
                orderRegion.setClose(false);
                orderRegion.setShortcut(ClientRegionShortcut.CACHING_PROXY);
                orderRegion.setLookupEnabled(true);
                logger.info("Dataloader initialized with - " + loader);
                orderRegion.setCacheLoader(loader);
                return orderRegion;
            }
}

CloudCacheDataLoader 类:

    @Component
    @Qualifier("cloudCacheDataLoader")
    public class CloudCacheDataLoader implements CacheLoader<Long, VariableRange>{


        private static final Logger logger = LoggerFactory
                .getLogger(CloudCacheDataLoader.class);


        @Autowired
        CacheDataService dataService;

        @Autowired
        CacheService cacheService;
        }

当我运行 Spring boot 应用程序时,在 orderRegion 对象中设置加载程序变量时,我得到 NULL。由于我是 Spring 新手,有人可以帮助我吗?

[编辑]我的日志中没有异常(exception),因为如果该对象为 NULL,则不会使用该对象。以下是我在日志文件中看到的日志条目:

2018-02-16T16:01:30.300-05:00 [APP/PROC/WEB/0] [OUT] [info 2018/02/16 21:01:30.296 UTC <Cache Client Updater Thread on localhost(cacheserver-94c0919e-80d5-4675-bb69-741728f64577:8620)<v3>:49152(version:UNKNOWN[ordinal=70]) port 40404> tid=0x25] Cache Client Updater Thread on localhost(cacheserver-94c0919e-80d5-4675-bb69-741728f64577:8620)<v3>:49152(version:UNKNOWN[ordinal=70]) port 40404 (localhost:40404) : ready to process messages.
2018-02-16T16:01:30.300-05:00 [APP/PROC/WEB/0] [OUT] [info 2018/02/16 21:01:30.299 UTC <main> tid=0x1] Pool DEFAULT started with multiuser-authentication=false
2018-02-16T16:01:30.359-05:00 [APP/PROC/WEB/0] [OUT] 2018-02-16 21:01:30.358 INFO 22 --- [ main] c.c.o.e.cloudcache.POCApplication : Dataloader initialized with - null
2018-02-16T16:01:30.371-05:00 [APP/PROC/WEB/0] [OUT] 2018-02-16 21:01:30.371 INFO 22 --- [ main] o.s.d.g.client.ClientRegionFactoryBean : Falling back to creating Region

[编辑]这是主类-

@Import(GemfireConfig.class)
@SpringBootApplication
@Profile("prod")
@EnableGemfireRepositories("cloudcache.repository")
@EnableJpaRepositories("cloudcache.repository")
@EnableDiscoveryClient  
@EnableCircuitBreaker
public class POCApplication {

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

}

最佳答案

使用@ComponentScan(basepackage = "com.corp.api")扫描所有bean(使用@component定义的类并通过@Autowire在main中使用它们)

    @...
    @EnabledAutoConfiguration
    @ComponentScan(basepackage = "com.corp.yourmainpackage")
    public class POCApplication {
    ...
    }

检查 POCApplication 类上的所有注释是否都是必需的,并且没有任何冲突且不是不必要的。

关于java - Spring引导 Autowiring 在配置类中给出null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48835329/

相关文章:

java - 如何在 Eclipse 中解析带有 HTML 标签的注释?

java - Spring Web App - 由 Java.lang.ClassNotFoundException 引起的 CannotLoadBeanClassException

java - 将 hibernate 投影结果映射到 Java POJO 模型

java - ServletRequest.getServletContext() 的 UnsupportedOperationException

java - Guava 的 CharSink 变成支持 StringBuilder?

java - Cosmosdb 保存的数据无法在门户中找到,而是从 azure-documentdb-spring-boot-starter 中找到

java - Gradle Android 依赖产品风格

java - @ControllerAdvice不处理异常

java - JPA创建模式后如何使组件初始化?

java - 对公共(public)和私有(private)资源使用不同的路径 Jersey + Spring boot