2

Hi friends i am developing a maven based spring boot project project , this project is multiple module form one module is Main module and second module is Service Module. I have one controller in Main module and one service in Serivce module

Controller

package com.aquevix.controller;

import com.aquevix.common.MyService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;

/**
 * Created by mohdqasim on 11/9/15.
 */
@RestController
@RequestMapping("/api")
public class MyController {

    @Inject MyService myService;
    @Inject BookRepository bookRepository;
    @RequestMapping(value = "/data" , method = RequestMethod.GET)
    public String getData(){
     return myService.getData();
    }
}

Service

package com.aquevix.common;

import org.springframework.stereotype.Service;

/**
 * Created by mohdqasim on 11/9/15.
 */
@Service
public class MyService {
    public String getData(){
        return "hello qasim";
    }
}

In maven multiple modules this scenerio is working fine but i have also one repository in the form of interface in service module.

package com.aquevix.common;

import org.springframework.data.jpa.repository.*;

/**
 * Spring Data JPA repository for the Book entity.
 */
public interface BookRepository extends JpaRepository<Book,Long> {

}

So when i execute main class from Main module my project works fine without bookrepository in service module( or present in Main module) but if i put bookrepository in Service module then MyController could not instantiate due dependency injection failure of bookRepository in MyController. Can anyone help me how to to avoid this failure i put any interface in Service module which is being injected into Main module

Qasim
  • 9,058
  • 8
  • 36
  • 50
  • 1
    Could you show your Application class? And second question - are you changing package of repository while relocation it to **service** module? – Evgenii Beschastnov Sep 11 '15 at 11:25
  • I had similar issue with a multi module Spring Boot project where my Mongo Repositories were in different project. Tried adding as Dependency Project in Eclipse too and it didn't work. It threw error referring cglib. Had to convert to Java Standalone application to get this working. – James Jithin Sep 11 '15 at 12:24
  • Show your main Spring app class and show the package structure. – improbable Oct 01 '19 at 14:23

2 Answers2

1

You would need to JavaConfig your repository location like below:

@Configuration
@EnableJpaRepositories("com.aquevix.common")
class ApplicationConfiguration {

  @Bean
  public EntityManagerFactory entityManagerFactory() {
    // …
  }
}

More reference at Working with Spring Data Repositories

James Jithin
  • 10,183
  • 5
  • 36
  • 51
  • This configuration isn't needed with Spring Boot. Check out [this](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-sql) part of the documentation – geoand Sep 11 '15 at 11:46
  • Thnx for replying , what if my custom interface do not extend jpa repository and i want to create bean on fly – Qasim Sep 11 '15 at 13:09
1

I had a similar problem and solved it using this post: Spring Boot: autowire beans from library project

In short, add this annotation to your application:

@Import(SharedConfigurationReference.class)

And then in your Service project create the SharedConfigurationReference class

package com.aquevix.common;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@ComponentScan("com.aquevix")
@EnableJpaRepositories("com.aquevix")
@EntityScan("com.aquevix")
public class SharedConfigurationReference {}

For the entity and component scan annotations, make sure to specify a package that is parent to all your projects, i.e. parent to controller and service

M. Noreikis
  • 173
  • 6