gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:24 25 4
gpt4 key购买 nike

我正在尝试在配置类中 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com