我正在尝试创建一个“员工”区域,并将一些数据放入其中。但是,我得到的例外如下:
警告2018/12/27 17:15:46.518在上下文初始化过程中遇到的IST tid=0x1异常-取消刷新尝试: org.springframework.beans.factory.BeanCreationException:错误创建名为“gemfireConfiguration”的bean :注入资源依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:错误创建org.springframework.beans.factory.BeanCreationException:错误,名为“gemfireCache”:FactoryBean抛出了对象创建异常;嵌套异常为java.lang.NoClassDefFoundError: it/unimi/dsi/fastutil/ints/Int2ObjectOpenHashMap。 警告2018/12/27 17:15:46.519 IST tid=0x1调用破坏方法失败了,其名称为“org.apache.geode.cache.CacheClosedException: org.apache.geode.cache.CacheClosedException:缓存尚未创建”。 错误2018/12/27 17:15:46.522 IST org.springframework.test.context.web.ServletTestExecutionListener@c667f46在允许TestExecutionListener tid=0x1准备测试实例com.gemfire.demo.Gemfire1ApplicationTests@48bfb884时捕获了异常
域类
@Region("employee")
public class Employee {
@Id
public String name;
public double salary;
...
}存储库类
@Repository
public interface EmployeeRepository extends CrudRepository<Employee, String> {
Employee findByName(String name);
}配置类
@Configuration
@ComponentScan
@EnableGemfireRepositories(basePackages = "com.gemfire.demo")
public class GemfireConfiguration {
@Autowired
EmployeeRepository employeeRepository;
@Bean
Properties gemfireProperties() {
Properties gemfireProperties = new Properties();
gemfireProperties.setProperty("name", "SpringDataGemFireApplication");
gemfireProperties.setProperty("mcast-port", "0");
gemfireProperties.setProperty("log-level", "config");
return gemfireProperties;
}
@Bean
@Autowired
CacheFactoryBean gemfireCache() {
CacheFactoryBean gemfireCache = new CacheFactoryBean();
gemfireCache.setClose(true);
gemfireCache.setProperties(gemfireProperties());
return gemfireCache;
}
@Bean(name="employee")
@Autowired
LocalRegionFactoryBean<String, Employee> getEmployee(final GemFireCache cache) {
LocalRegionFactoryBean<String, Employee> employeeRegion = new LocalRegionFactoryBean<String, Employee>();
employeeRegion.setCache(cache);
employeeRegion.setClose(false);
employeeRegion.setName("employee");
employeeRegion.setPersistent(false);
employeeRegion.setDataPolicy(DataPolicy.PRELOADED);
return employeeRegion;
}
} POM.XML
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-gemfire</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.0</version>
</dependency> 发布于 2019-02-05 19:56:32
在上面的GemFire/Spring JavaConfig配置类中添加其他提示。
如果您使用的是Spring (通过使用Spring Kay父POM,即org.springframework.boot:spring-boot-dependencies;请参阅这里),那么您可以使用Spring的(相对)新的和方便的基于注释的配置模型。
这样做,您上面的GemfireConfiguration类就会变成.
@PeerCacheApplication
@EnableGemfireRepositories(basePackages = "com.gemfire.demo")
class GemfireConfiguration {
@Bean(name="employee")
LocalRegionFactoryBean<String, Employee> getEmployee(GemFireCache cache) {
LocalRegionFactoryBean<String, Employee> employeeRegion =
new LocalRegionFactoryBean<String, Employee>();
employeeRegion.setCache(cache);
employeeRegion.setClose(false);
employeeRegion.setDataPolicy(DataPolicy.PRELOADED);
return employeeRegion;
}
}有几件事要记住:
@PeerCacheApplication使用@Configuration进行元注释,因此不需要配置类上显式的Spring@Configuration注释。@PeerCacheApplication允许您使用logLevel注释属性调整GemFire日志级别(以及其他日志配置)。类似地,您可以在Spring application.properties文件中使用相应的属性spring.data.gemfire.cache.log-level设置日志级别(参见这里)。还有许多其他属性和相应的属性(例如name),您可以使用它们来调整和自定义其他配置。@EnableGemfireRepositories和类似的注释支持基于字符串的包名,但我们通常倾向于并建议用户使用类型安全的变体basePacakgeClasses。您只需要从保存应用程序库的每个顶级包中引用单个类型即可。@Autowired注释。您不需要在配置类中显式注入EmployeeRepository才能初始化它;只需将其注入将使用它的@Service类即可。employeeRegion.setName("employee")是不必要的。LocalRegionFactoryBean.setPersistent(:boolean)与LocalRegionFactoryBean.setDataPolicy(:DataPolicy)结合起来,因为DataPolicy将优先。@ComponentScan在开发中是完全可以接受的,甚至是方便的,但我通常不喜欢也不推荐用户使用组件扫描。通常情况下最好是直截了当的。<relativePath/>。2.0.8.RELEASE是最新的版本。至于类路径问题,如果您正确地使用了Maven,那么Maven应该处理正确的传递依赖关系。
您可以参考我在这个存储库中的许多示例来进一步澄清。
希望这能有所帮助!
发布于 2018-12-28 05:55:03
正如注释中提到的,错误显示缺少一些依赖项(java.lang.NoClassDefFoundError: it/unimi/dsi/fastutil/ints/Int2ObjectOpenHashMap)。请在pom.xml中添加相应的依赖项
https://stackoverflow.com/questions/53944754
复制相似问题