简体   繁体   中英

Spring boot autowiring giving null in configuration class

I am trying to autowire one class in Configuration class like below:

    @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 class:

    @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;
        }

When I am running my Spring boot application, I am getting NULL for loader variable while setting it in orderRegion object. Can someone please help me out here as I am new to Spring.

[EDIT]There is no exception in my logs as this object will not be used in case it is NULL. Below is the log entry I am seeing in log file:

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

[EDIT] Here is the main class-

@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);
    }

}

use @ComponentScan(basepackage = "com.corp.api") to scan all beans( class which sre defined with @component and use them in main by @Autowire)

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

check that your all annotation on POCApplication class are required and has not any conflict and were not unnecessary.

Ok so I know the issue now. When I was autowiring a class level variable, it was initialized as NULL, but when I autowired the method parameter, it was autowired successfully. I used below code to fix it:

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

    @Bean(name = "VariableRangeCache")
            ClientRegionFactoryBean<Long, VariableRange> variableRangeRegion(
                    @Autowired ClientCache gemfireCache, 
                    @Autowired CloudCacheDataLoader loader) {

                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;
            }
}

Below was the original code: I still don't know the issue here but anyways it is working so..

 @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;
                }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM