Neo4J存储库到处都没有连接

时间:2016-05-13 09:56:32

标签: spring neo4j spring-data-neo4j

我已经定义了一个Neo4j存储库如下(代码在Kotlin中,但它非常接近Java):

@Repository
interface UserRepository : GraphRepository<User> {

    fun findByEmail(email: String): User?

    @Query("match (n:User)-[:IS_AUTH]->(:Permission {name: {0}}) where id(n) = {1} return n")
    fun authorizedUser(permission: String, userId: Long): User?

}

在控制器中,我写道:

@Controller
@RequestMapping("/company")
open class CreateCompanyController {

    @Autowired private lateinit var userRepo: UserRepository

    @RequestMapping(method = arrayOf(RequestMethod.POST))
    @ResponseBody
    fun createCompany(@RequestParam(value = "name", required = true) name: String,
                      @RequestParam(value = "siret", required = true) siret: Long) : ResponseEntity<String> {

        val connectedUser = SecurityContextHolder.getContext().authentication.principal as User

        val testUser = userRepo.findByEmail("test@company.com")

        if (!PermissionManager().hasAuthorizationFor(PermissionManager.ADMIN_ALL, connectedUser))
            return ResponseEntity("You're not authorized to create a company", HttpStatus.FORBIDDEN)


        return ResponseEntity(HttpStatus.OK)

    }

}

在上面的代码中,userRepo属性设置得很好(也就是说,它的值不为空)。

我希望将此UserRepository放在自定义服务中。所以,我编码:

@Service
open class PermissionManager {

    companion object {
        val ADMIN_ALL = "ADMIN_ALL"
    }

    @Autowired private lateinit var userRepo: UserRepository

    fun hasAuthorizationFor(permissionName: String, user: User): Boolean {
        return userRepo.authorizedUser(permissionName, user.id!!) != null
    }

}

但在这种情况下,userRepo属性未初始化。

我已将PersistenceContext类编写为:

@Configuration
@ComponentScan("persistence")
@EnableNeo4jRepositories("persistence.repositories")
@EnableTransactionManagement
open class PersistenceContext : Neo4jConfiguration() {
    @Bean override  fun getSessionFactory(): SessionFactory {
        return SessionFactory("persistence.domain")
    }

    @Bean
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    open override fun getSession(): Session {
        return super.getSession()
    }
}

我不明白为什么userRepo设置在我的控制器中但不在我的服务中?

0 个答案:

没有答案