neo4j / sdn-rx

Nextgen Spring Data module for Neo4j supporting (not only) reactive data access and immutable support
https://neo4j.github.io/sdn-rx
Apache License 2.0
65 stars 23 forks source link

Consider defining a bean of type... #196

Closed pentarex closed 4 years ago

pentarex commented 4 years ago

Hi,

I am playing around with spring boot webflux and neo4j and I am having the following issue.

APPLICATION FAILED TO START
***************************

Description:

Field userRepository in com.example.rx.service.impl.UserServiceImpl required a bean of type 'com.example.rx.repository.UserRepository' that could not be found.

Action:

Consider defining a bean of type 'com.example.rx.repository.UserRepository' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:53683', transport: 'socket'

Process finished with exit code 0

My User looks like that

package com.example.rx.entity;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.neo4j.springframework.data.core.schema.GeneratedValue;
import org.neo4j.springframework.data.core.schema.Id;
import org.neo4j.springframework.data.core.schema.Node;

@Node
@Getter
@Setter
@NoArgsConstructor
public class User {
    @Id @GeneratedValue
    private int id;

    private String name;
}

My Repository looks like that

package com.example.rx.repository;

import com.example.rx.entity.User;
import org.neo4j.springframework.data.repository.ReactiveNeo4jRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Mono;

@Repository
public interface UserRepository extends ReactiveNeo4jRepository<User, Long> {
    Mono<User> findByName(String name);
}

My Service Implementation looks like that

package com.example.rx.service.impl;

import com.example.rx.entity.User;
import com.example.rx.repository.UserRepository;
import com.example.rx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public Mono<User> findUserByName(String name) {
        return userRepository.findByName(name);
    }
}

The whole code you can find under https://github.com/pentarex/spring-rx-neo4j I have tried with rc01 and currently trying with beta04

If I try the same code (except Node I replace with Document and ReactiveNeo4jRepository with ReactiveCrudRepositry) with mongodb it works. Am I missing something?

I have also tried setting @EnableNeo4jRepositories("com.example.rx.repository") to my Main class, but no success

BR, Hristo

meistermeier commented 4 years ago

You are declaring only the spring-data-neo4j-rxdependency. This will not create the needed infrastructure for your code to work. Since you are developing a Spring Boot application, you should declare the Spring Boot starter instead of the direct relationship:

<dependency>
    <groupId>org.neo4j.springframework.data</groupId>
    <artifactId>spring-data-neo4j-rx-spring-boot-starter</artifactId>
    <version>1.0.0-rc01</version>
</dependency>

This will also have the benefit that it pulls ins the Neo4j Driver Spring Boot starter as a transitive dependency and you can define all the needed connection properties in the application.properties file like:

org.neo4j.driver.uri=bolt://localhost:7687
org.neo4j.driver.authentication.username=neo4j
org.neo4j.driver.authentication.password=secret

If you do not want to work with the starter (hint: you really should) you can use the @EnableNeo4jRepositories annotation but also have to define a bean of type org.neo4j.driver.Driver.

@Bean
public Driver driver() {
    return GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "secret"));
}

Side note: If you are working with database-side generated ids, they should be of type Long.

meistermeier commented 4 years ago

Is this working for you? I would like to close this issue if it is no longer active.

meistermeier commented 4 years ago

Closed due to inactivity. Please feel free to re-open this if this does solve your problem.

pentarex commented 4 years ago

Sorry, I was not available. It does work. Thank you!