问题
I am using spring-data-neo4j (standalone) in my JavaEE7-application as nice neo4j-OGM.
For time being, I am trying to integrate spring-data-neo4j repositories via @Autowired into my project.
public interface UserRepository extends GraphRepository<User> {}
I have started writing some JUnit-tests, which are testing beans which themselves use this repositories. Everything works fine so far.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/application-context.xml" })
@Transactional
@Import(NeighborinoNeo4jConfiguration.class)
public class UserFactoryTest {
private UserFactory userFactory;
...
}
@Named
@Stateless
public class UserFactory {
@Autowired
private UserRepository userRepo;
...
}
Now, I want to integrate this new repositories-classes into my JavaEE7-application, which I am deploying to a wildfly-8.1.
Adding the mentioned UserRepository to my application and deploying it results in following error:
javax.enterprise.inject.UnsatisfiedResolutionException: Unable to resolve a bean for 'org.springframework.data.neo4j.support.mapping.Neo4jMappingContext' with qualifiers [@javax.enterprise.inject.Default(), @javax.enterprise.inject.Any()].
at org.springframework.data.neo4j.repository.cdi.Neo4jCdiRepositoryExtension.createRepositoryBean(Neo4jCdiRepositoryExtension.java:109)
at org.springframework.data.neo4j.repository.cdi.Neo4jCdiRepositoryExtension.afterBeanDiscovery(Neo4jCdiRepositoryExtension.java:83)
...
To make myself clear: Just by adding this new interface to my source-code and deploying it, results in this error in the application-server. The app without this repository is deployed just fine.
As far as I can see, Neo4jCdiRepositoryExtension.createRepositoryBean(), runs too early. I have an own @ApplicationScoped-bean "Application" which, without this repository in the source-code, does the spring-configuration. But with this repository added, this ApplicationScoped-bean "Application" is not executed at all; and I assume this UnsatisfiedResolutionException occurs, because spring-configuration was not done before Neo4jCdiRepositoryExtension runs . I guess my problem could be solved by having the repositories initialization done after my "Application"-bean.
so... How do I load spring-data-neo4j repositories lazily?
Hint#1: @NoRepositoryBean makes the app deployable again. Of course, I cannot use the UserRepository.
import org.springframework.data.repository.NoRepositoryBean
@NoRepositoryBean
public interface UserRepository extends GraphRepository<User> {}
Hint#2: @Lazy does not help, same error.
import org.springframework.context.annotation.Lazy;
@Lazy
public interface UserRepository extends GraphRepository<User> {
versions:
pom.xml:
<spring.version>4.0.7.RELEASE</spring.version>
<spring.data.neo4j.version>3.2.0.RELEASE</spring.data.neo4j.version>
回答1:
I was finally able to develop a workaround. In short I stopped using "@Autowired" at all and falled back to using javax.inject.Inject etc. again, and I am boostrapping spring-data-neo4j in good old fashioned "ClassPathXmlApplicationContext" way.
The main difficulty was to find out, how to build a spring-data-neo4j repository on my own. It is easy.
// The repo you want to have an instance of.
@NoRepositoryBean // Has to be here to avoid UnsatisfiedResolutionException from question.
public interface UserRepository extends GraphRepository<User> {
}
(Hint: "@NoRepositoryBean" is required on your repository-interfaces. Even tough I removed "@EnableNeo4jRepositories(...)".)
I assume you have a running springApplication:
final ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"spring/application-context.xml");
final Neo4jTemplate neo4jTemplate = applicationContext .getBean(Neo4jTemplate.class);
final Neo4jMappingContext neo4jMappingContext = applicationContext .getBean(Neo4jMappingContext.class);
Next is to create a GraphRepositoryFactory, which is needed to build the repositories:
org.springframework.data.neo4j.repository.GraphRepositoryFactory graphRepositoryFactory = new GraphRepositoryFactory(neo4jTemplate, neo4jMappingContext);
UserRepository userRepository = graphRepositoryFactory.getRepository(UserRepository.class);
来源:https://stackoverflow.com/questions/26363430/how-do-i-load-spring-data-neo4j-repositories-lazily