我正在尝试创建一个“雇员”区域并将一些数据放入其中。但是,我在下面出现异常:
[警告2018/12/27 17:15:46.518 IST tid = 0x1]异常 上下文初始化期间遇到-取消刷新 尝试:org.springframework.beans.factory.BeanCreationException: 创建名称为'gemfireConfiguration'的bean时出错:注入 资源依赖性失败;嵌套异常为 org.springframework.beans.factory.BeanCreationException:错误 创建名称为'gemfireCache'的bean:FactoryBean在 对象创建;嵌套的异常是java.lang.NoClassDefFoundError: 它/ unimi / dsi / fastutil / ints / Int2ObjectOpenHashMap
[warn 2018/12/27 17:15:46.519 IST tid = 0x1]的调用 在名称为'gemfireCache'的bean上的destroy方法失败: org.apache.geode.cache.CacheClosedException:尚未缓存 已创建。
[错误2018/12/27 17:15:46.522 IST tid = 0x1]被捕获的异常 同时允许TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@c667f46] 准备测试实例 [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>
答案 0 :(得分:1)
在上面的GemFire / Spring JavaConfig配置类中添加其他提示。
假设您使用的是 Spring数据 Kay
(表示您使用的是Spring Boot 2.0.x
父POM,即org.springframework.boot:spring-boot-dependencies
;请参见{{3} }),那么您可以(相对)使用 Spring Data GemFire的新的便捷here。
这样做,您上面的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 Boot spring.data.gemfire.cache.log-level
文件中的相应属性application.properties
设置日志级别(请参阅Annotation-based configuration model)。您还可以使用许多其他属性和相应的属性(例如name
)来调整和自定义其他配置。
尽管@EnableGemfireRepositories
和类似的注释支持基于字符串的程序包名称,但我们通常更喜欢并建议用户使用类型安全的变量basePacakgeClasses
。您只需要从保存应用程序存储库的每个顶级包中引用一种类型。
在您的bean定义上不需要显式@Autowired
注释。您无需在配置类中显式注入EmployeeRepository
即可对其进行初始化。只需将其注入将要使用的@Service
类中即可。
为方便起见,您的本地“员工”区域上的Region bean定义的名称(“员工”)也将用作该区域的名称,因此employeeRegion.setName("employee")
是不必要的。 / p>
您不应将LocalRegionFactoryBean.setPersistent(:boolean)
与LocalRegionFactoryBean.setDataPolicy(:DataPolicy)
结合使用,因为DataPolicy将优先。
尽管@ComponentScan
在开发中完全可以接受甚至方便,但我通常不喜欢也不建议用户使用组件扫描。通常最好总是明确一些。
如评论所述,您应该从应用程序Maven POM文件的父定义中删除<relativePath/>
。
最后的提示,截至本文, Spring Boot 2.0.8.RELEASE
是最新版本。
对于类路径问题,如果正确使用了Maven,则Maven应该注意获取正确的传递依赖项。
您可以参考我在此here中拥有的许多示例,以进行进一步的说明。
希望这会有所帮助!
答案 1 :(得分:0)
如注释中所述,该错误表明缺少某些依赖项(java.lang.NoClassDefFoundError: it/unimi/dsi/fastutil/ints/Int2ObjectOpenHashMap
)。请在您的pom.xml中添加相应的依赖项