我想在 Spring Boot 应用程序中创建一个 Gemfire 区域。按照这个 sample ,它在不添加数据库支持的情况下运行良好。如果我添加数据库,它会显示错误,如“创建名称为‘dataSource’的 bean 时出错”。但是,默认的 gemfire 缓存 bean 与数据源集成配合得很好。

@EnableAutoConfiguration 
// Sprint Boot Auto Configuration 
@ComponentScan(basePackages = "napo.demo") 
@EnableCaching 
@SuppressWarnings("unused") 
public class Application extends SpringBootServletInitializer { 
 
private static final Class<Application> applicationClass = Application.class; 
private static final Logger log = LoggerFactory.getLogger(applicationClass); 
 
public static void main(String[] args) { 
    SpringApplication.run(applicationClass, args); 
} 
 
/* **The commented code works well with database.** 
@Bean 
CacheFactoryBean cacheFactoryBean() { 
    return new CacheFactoryBean(); 
} 
 
@Bean 
ReplicatedRegionFactoryBean<Integer, Integer> replicatedRegionFactoryBean(final Cache cache) { 
     ReplicatedRegionFactoryBean<Integer, Integer> region= new ReplicatedRegionFactoryBean<Integer, Integer>() {{ 
        setCache(cache); 
        setName("demo"); 
 
    }}; 
 
    return region; 
}  */ 
// This configuration will cause issue as beow  
//  

org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class] 中创建名称为“dataSource”的 bean 时出错:通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [javax.sql.DataSource]:工厂方法 'dataSource' 抛出异常;嵌套异常是 java.lang.NullPointerException

@Bean 
GemfireCacheManager cacheManager(final Cache gemfireCache) { 
    return new GemfireCacheManager() { 
        { 
            setCache(gemfireCache); 
        } 
    }; 
} 
 
 
// NOTE ideally, "placeholder" properties used by Spring's PropertyPlaceholderConfigurer would be externalized 
    // in order to avoid re-compilation on property value changes (so... this is just an example)! 
    @Bean 
    public Properties placeholderProperties() { 
        Properties placeholders = new Properties(); 
 
        placeholders.setProperty("app.gemfire.region.eviction.action", "LOCAL_DESTROY"); 
        placeholders.setProperty("app.gemfire.region.eviction.policy-type", "MEMORY_SIZE"); 
        placeholders.setProperty("app.gemfire.region.eviction.threshold", "4096"); 
        placeholders.setProperty("app.gemfire.region.expiration.entry.tti.action", "INVALIDATE"); 
        placeholders.setProperty("app.gemfire.region.expiration.entry.tti.timeout", "300"); 
        placeholders.setProperty("app.gemfire.region.expiration.entry.ttl.action", "DESTROY"); 
        placeholders.setProperty("app.gemfire.region.expiration.entry.ttl.timeout", "60"); 
        placeholders.setProperty("app.gemfire.region.partition.local-max-memory", "16384"); 
        placeholders.setProperty("app.gemfire.region.partition.redundant-copies", "1"); 
        placeholders.setProperty("app.gemfire.region.partition.total-max-memory", "32768"); 
 
        return placeholders; 
    } 
 
    @Bean 
    public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer( 
            @Qualifier("placeholderProperties") Properties placeholders) { 
        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); 
        propertyPlaceholderConfigurer.setProperties(placeholders); 
        return propertyPlaceholderConfigurer; 
    } 
 
    @Bean 
    public Properties gemfireProperties() { 
        Properties gemfireProperties = new Properties(); 
 
        gemfireProperties.setProperty("name", "SpringGemFireJavaConfigTest"); 
        gemfireProperties.setProperty("mcast-port", "0"); 
        gemfireProperties.setProperty("log-level", "config"); 
 
        return gemfireProperties; 
    } 
 
    @Bean 
    @Autowired 
    public CacheFactoryBean gemfireCache(@Qualifier("gemfireProperties") Properties gemfireProperties) throws Exception { 
        CacheFactoryBean cacheFactory = new CacheFactoryBean(); 
        cacheFactory.setProperties(gemfireProperties); 
        return cacheFactory; 
    } 
 
 
    @Bean(name = "ExamplePartition") 
    @Autowired 
    public ReplicatedRegionFactoryBean<Object, Object> examplePartitionRegion(Cache gemfireCache, 
            @Qualifier("partitionRegionAttributes") RegionAttributes<Object, Object> regionAttributes) throws Exception { 
 
        ReplicatedRegionFactoryBean<Object, Object> examplePartitionRegion = 
            new ReplicatedRegionFactoryBean<Object, Object>(); 
 
        examplePartitionRegion.setAttributes(regionAttributes); 
        examplePartitionRegion.setCache(gemfireCache); 
        examplePartitionRegion.setName("demo"); 
 
 
        return examplePartitionRegion; 
    } 
 
    @Bean 
    @Autowired 
    public RegionAttributesFactoryBean partitionRegionAttributes( 
            EvictionAttributes evictionAttributes, 
            @Qualifier("entryTtiExpirationAttributes") ExpirationAttributes entryTti, 
            @Qualifier("entryTtlExpirationAttributes") ExpirationAttributes entryTtl) { 
 
        RegionAttributesFactoryBean regionAttributes = new RegionAttributesFactoryBean(); 
 
        regionAttributes.setEvictionAttributes(evictionAttributes); 
        regionAttributes.setEntryIdleTimeout(entryTti); 
        regionAttributes.setEntryTimeToLive(entryTtl); 
 
 
        return regionAttributes; 
    } 
 
    @Bean 
    public EvictionAttributesFactoryBean defaultEvictionAttributes( 
            @Value("${app.gemfire.region.eviction.action}") String action, 
            @Value("${app.gemfire.region.eviction.policy-type}") String policyType, 
            @Value("${app.gemfire.region.eviction.threshold}") int threshold) { 
 
        EvictionAttributesFactoryBean evictionAttributes = new EvictionAttributesFactoryBean(); 
 
        evictionAttributes.setAction(EvictionActionType.valueOfIgnoreCase(action).getEvictionAction()); 
        evictionAttributes.setThreshold(threshold); 
        evictionAttributes.setType(EvictionPolicyType.valueOfIgnoreCase(policyType)); 
 
        return evictionAttributes; 
    } 
 
    @Bean 
    public ExpirationAttributesFactoryBean entryTtiExpirationAttributes( 
            @Value("${app.gemfire.region.expiration.entry.tti.action}") String action, 
            @Value("${app.gemfire.region.expiration.entry.tti.timeout}") int timeout) { 
 
        ExpirationAttributesFactoryBean expirationAttributes = new ExpirationAttributesFactoryBean(); 
 
        expirationAttributes.setAction(ExpirationActionType.valueOfIgnoreCase(action).getExpirationAction()); 
        expirationAttributes.setTimeout(timeout); 
 
        return expirationAttributes; 
    } 
 
    @Bean 
    public ExpirationAttributesFactoryBean entryTtlExpirationAttributes( 
            @Value("${app.gemfire.region.expiration.entry.ttl.action}") String action, 
            @Value("${app.gemfire.region.expiration.entry.ttl.timeout}") int timeout) { 
 
        ExpirationAttributesFactoryBean expirationAttributes = new ExpirationAttributesFactoryBean(); 
 
        expirationAttributes.setAction(ExpirationActionType.valueOfIgnoreCase(action).getExpirationAction()); 
        expirationAttributes.setTimeout(timeout); 
 
        return expirationAttributes; 
    } 
 
    @Bean 
    public PartitionAttributesFactoryBean defaultPartitionAttributes( 
            @Value("${app.gemfire.region.partition.local-max-memory}") int localMaxMemory, 
            @Value("${app.gemfire.region.partition.redundant-copies}") int redundantCopies, 
            @Value("${app.gemfire.region.partition.total-max-memory}") int totalMaxMemory) { 
 
        PartitionAttributesFactoryBean partitionAttributes = new PartitionAttributesFactoryBean(); 
 
        partitionAttributes.setLocalMaxMemory(localMaxMemory); 
        partitionAttributes.setRedundantCopies(redundantCopies); 
        partitionAttributes.setTotalMaxMemory(totalMaxMemory); 
 
        return partitionAttributes; 
    } 
 
 
@Override 
protected SpringApplicationBuilder configure( 
        SpringApplicationBuilder application) { 
    return application.sources(applicationClass); 
}} 

demoService java代码:

@Service 
public class demoService { 
 
  @Autowired     
private demoMapper demoMapper; 
 
 
@Cacheable("demo") 
public Fund getDemo(String code) { 
    Demo demo= demoMapper.getDemo(Code); 
    return demo; 
 
} 

请您参考如下方法:


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!