Spring Data Neo4j ClassFileProcessor不加载任何类

时间:2016-07-21 07:38:18

标签: java spring neo4j spring-boot spring-data-neo4j

我有一个使用Spring Data Neo4j的Spring Boot项目。当我从我的IDE(Intellij)运行项目时,一切正常。当我从命令行(java -jar ...)运行项目时,我收到的消息是我的实体不是可持久类的实例。

我将basePackages传递给SessionFactory:

@Bean
public SessionFactory getSessionFactory() {
    return new SessionFactory(getConfiguration(), basePackages);
}

我在主要课上注明了:

@EnableNeo4jRepositories(basePackages = "org.sgh.persistence")

信息日志显示以下内容:

从Intellij开始:

2016-07-20 15:58:30.435  INFO 67581 --- [           main] o.s.d.neo4j.config.Neo4jConfiguration    : Initialising Neo4jSession
2016-07-20 15:58:30.443 DEBUG 67581 --- [           main] org.neo4j.ogm.scanner.ClassPathScanner   : Classpath elements:
2016-07-20 15:58:30.443 DEBUG 67581 --- [           main] org.neo4j.ogm.scanner.ClassPathScanner   : /Users/sebastianglahn/workspace/sandbox/spring-data-neo4j-example/build/classes/main/org/sgh/persistence
2016-07-20 15:58:30.447 DEBUG 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Processing: org.sgh.persistence.ExampleRepository -> java.lang.Object
2016-07-20 15:58:30.449 DEBUG 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Processing: org.sgh.persistence.TreeEntity -> java.lang.Object
2016-07-20 15:58:30.450  INFO 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Starting Post-processing phase
2016-07-20 15:58:30.450  INFO 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Building annotation class map
2016-07-20 15:58:30.450  INFO 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Building interface class map for 3 classes
2016-07-20 15:58:30.450 DEBUG 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  :  - ExampleRepository implements 1 interfaces
2016-07-20 15:58:30.450 DEBUG 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  :    - org.springframework.data.neo4j.repository.GraphRepository
2016-07-20 15:58:30.450 DEBUG 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  :  - TreeEntity implements 0 interfaces
2016-07-20 15:58:30.450 DEBUG 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  :  - Object implements 0 interfaces
2016-07-20 15:58:30.450 DEBUG 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Post-processing: org.sgh.persistence.ExampleRepository
2016-07-20 15:58:30.450 DEBUG 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  :  - No ClassInfo found for interface class: org.springframework.data.neo4j.repository.GraphRepository
2016-07-20 15:58:30.450 DEBUG 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Post-processing: org.sgh.persistence.TreeEntity
2016-07-20 15:58:30.450 DEBUG 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Checking for @Transient classes....
2016-07-20 15:58:30.450 DEBUG 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Registering converters and deregistering transient fields and methods....
2016-07-20 15:58:30.451  INFO 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Post-processing complete
2016-07-20 15:58:30.452  INFO 67581 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : 3 classes loaded in 11 milliseconds

从命令行启动时:

 2016-07-21 09:11:46.441  INFO 67991 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Starting Post-processing phase
2016-07-21 09:11:46.441  INFO 67991 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Building annotation class map
2016-07-21 09:11:46.441  INFO 67991 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Building interface class map for 0 classes
2016-07-21 09:11:46.441 DEBUG 67991 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Checking for @Transient classes....
2016-07-21 09:11:46.441 DEBUG 67991 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Registering converters and deregistering transient fields and methods....
2016-07-21 09:11:46.441  INFO 67991 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : Post-processing complete
2016-07-21 09:11:46.441  INFO 67991 --- [           main] o.neo4j.ogm.metadata.ClassFileProcessor  : 0 classes loaded in 1829 milliseconds

虽然调试日志显示ClassPathScanner正在扫描正确的类:

2016-07-21 09:35:40.099 DEBUG 68083 --- [           main] org.neo4j.ogm.scanner.ClassPathScanner   : Scanning zipFile /private/tmp/spring-data-neo4j-demo/build/libs/spring-data-neo4j-example-0.0.1-SNAPSHOT.jar
2016-07-21 09:35:40.101 DEBUG 68083 --- [           main] org.neo4j.ogm.scanner.ClassPathScanner   : Scanning class entry: BOOT-INF/classes/org/sgh/configuration/Neo4jConfig.class
2016-07-21 09:35:40.101 DEBUG 68083 --- [           main] org.neo4j.ogm.scanner.ClassPathScanner   : Scanning class entry: BOOT-INF/classes/org/sgh/Controller/ExampleController.class
2016-07-21 09:35:40.101 DEBUG 68083 --- [           main] org.neo4j.ogm.scanner.ClassPathScanner   : Scanning class entry: BOOT-INF/classes/org/sgh/persistence/ExampleRepository.class
2016-07-21 09:35:40.101 DEBUG 68083 --- [           main] org.neo4j.ogm.scanner.ClassPathScanner   : Scanning class entry: BOOT-INF/classes/org/sgh/persistence/TreeEntity.class
2016-07-21 09:35:40.101 DEBUG 68083 --- [           main] org.neo4j.ogm.scanner.ClassPathScanner   : Scanning class entry: BOOT-INF/classes/org/sgh/service/ExampleService.class
2016-07-21 09:35:40.101 DEBUG 68083 --- [           main] org.neo4j.ogm.scanner.ClassPathScanner   : Scanning class entry: BOOT-INF/classes/org/sgh/SpringDataNeo4jExampleApplication.class
2016-07-21 09:35:40.101 DEBUG 68083 --- [           main] org.neo4j.ogm.scanner.ClassPathScanner   : Scanning zipped entry: BOOT-INF/lib/spring-test-4.3.1.RELEASE.jar

我在Github上创建了一个最小的演示项目来重现这个问题: https://github.com/sglahn/spring-data-neo4j-demo

要重现此问题,请执行以下操作: 建立jar:

./gradlew build

执行jar:

java -Dspring.config.location=build/resources/main/ -jar build/libs/spring-data-neo4j-example-0.0.1-SNAPSHOT.jar

在浏览器中访问Controller:

http://localhost:8765/

日志显示:

org.sgh.persistence.TreeEntity is not an instance of a persistable class

Spring Boot版本是1.4.0.RC1

1 个答案:

答案 0 :(得分:1)

看看the docs for Spring Boot 1.4.0

方法org.neo4j.ogm.metadata.DomainInfo#load扫描域的"basePackages"并加载所有域类。当您使用Spring Boot 1.4.0构建jar时,“basePackages”的类路径将更改为boot-info/class/"basePackages",然后org.neo4j.ogm.scanner.ClassPathScanner#scanClassFileEntry无法处理域类。

因此,您必须更改为Spring boot 1.3才能解决此问题。