简体   繁体   中英

Getting unsatisfied dependency for Arango repository using Spring Data in simple app

I am trying to get a simple Arango app started with Spring Data. My Arango db is running in Docker on localhost and default port.

The project is built as a Spring Boot Groovy-enabled project using Intellij.

I've pushed the demo app to github here and it's about as simple as can get.

I did this trying to follow the online documentation, but structurally I put the model class in the model folder and the repository interface into a repository folder.

When I try and run this I get this error. But so far as I can see I had done what was expected extending from AbstractRepository<Organisation.class> . it says ambiguous dependency for parameter of type ArangoOperations, but I don't do this directly in the repository.

What have I done wrong?

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'organisationRepository': Unsatisfied dependency expressed through constructor parameter 1: Ambiguous argument values for parameter of type [com.arangodb.springframework.core.ArangoOperations] - did you specify the correct bean references as arguments?
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:756) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1187) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:991) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:865) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:574) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:518) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:481) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:602) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:594) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1226) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.boot.SpringApplication.getExitCodeFromMappedException(SpringApplication.java:865) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
    at org.springframework.boot.SpringApplication.getExitCodeFromException(SpringApplication.java:853) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
    at org.springframework.boot.SpringApplication.handleExitCode(SpringApplication.java:840) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
    at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:791) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
    at org.springframework.boot.SpringApplication$run.call(Unknown Source) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) [groovy-2.5.7.jar:2.5.7]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115) [groovy-2.5.7.jar:2.5.7]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:135) [groovy-2.5.7.jar:2.5.7]
    at com.softwood.arango.ArangoApplication.main(ArangoApplication.groovy:14) [main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.7.RELEASE.jar:2.1.7.RELEASE]

so in the end noted that the example i was copying didnt reflect latest versions of libraries etc, so i took the latest docker image 3.4, and updated the gradle app build to latest libraries in the dependencies

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.codehaus.groovy:groovy'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    compile 'com.arangodb:arangodb-spring-data:3.1.0'
    compile 'com.arangodb:arangodb-java-driver:5.0.7'
}

and retried. things started to look better, however they have changed the spring data repository interface defn now where you need two types - the entity class and the type id of the @id field - else the code wont compile

public interface OrganisationRepository extends ArangoRepository<Organisation, String> {}

this allowed the save operation to work -

however the findOne operation takes an Example.of and returns an optional. so in latest code you essentially have to test for completed result before you do a Optional.get() to get the value

so my simple CrudRunner run operation now looks like this, which compiles and runs and returns the previous saved object

@Override
public void run(final String... args) throws Exception {
    // first drop the database so that we can run this multiple times with the same dataset
    operations.dropDatabase()

    // save a single entity in the database
    // there is no need of creating the collection first. This happen automatically
    final Organisation vf = new Organisation(name:"Vodafone", inaugurated: 2000, webAddress:"vodafone.com")
    repository.save(vf)

    CollectionOperations coll= operations.collection(Organisation)

    CollectionPropertiesEntity props = coll.getProperties()
    println props.name

    // the generated id from the database is set in the original entity
    println(String.format("vf organisation saved in the database with id: '%s'", vf.id))

    // create an example from saved object and use to query the db - findOne returns Optional<T>
    Optional<Organisation> res = repository.findOne(Example.of(vf))
    assert res.isPresent()

    final Organisation foundOrg = res.get()

    repository.findOne()
    println(String.format("Found %s", foundOrg.name))

} 

so now we have a basic working version of spring data with arango. enough to get you over the initial 'humps' in the road i can explore a bit further

hope this might help any one else who wants to get started and gets frustrated when your PoC fails to work.

note. in the browser client on the docker server you need to select the database that you create (here 'arango-demo' db) as be default your logged into _system db and not your app db. once you select the right db, you can see the autocreated collection and your record in that collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM