2014-11-30 3 views
0

У меня вопрос о шаблоне DAO и Service layer, и я использую spring-boot-starter-data-jpa.Общие DAO и службы весной Java

Dao Слой:

@NoRepositoryBean 
public interface Dao<T, ID extends Serializable> extends JpaRepository<T, ID>, 
     JpaSpecificationExecutor<T> { 
} 
... 
public interface ModuleDao extends Dao<Module, Long> { 
} 

Service Layer

public interface Service<T, ID extends Serializable> { 
    T save(T entity); 

    void delete(T entity); 

    void deleteById(ID id); 

    T update(T entity); 

    T getById(ID id); 

    List<T> getAll(); 

    List<T> getByFilter(Specification<T> spec); 

    Page<T> getByFilter(Specification<T> spec, Pageable pageable); 
} 
... 
public abstract class AbstractService<T, ID extends Serializable> implements 
     Service<T, ID> { 

    protected final Logger logger = LoggerFactory.getLogger(getClass()); 
    protected Dao<T, ID> dao; 

    public AbstractService(Dao<T, ID> dao) { 
     this.dao = dao; 
    } 

    @Override 
    public T save(T entity) { 
     this.logger.debug("Create a new {} with information: {}", entity.getClass(), 
       entity.toString()); 
     return this.dao.save(entity); 
    } 
    ... 
} 

//The concrete implementation 
@Service 
@Transactional(readOnly = true) 
public class ModuleService extends AbstractService<Module, Long> { 

    @Autowired 
    public ModuleService(ModuleDao moduleDao) { 
     super(moduleDao); 
    } 

    @Override 
    public Page<Module> getByFilter(Specification<Module> spec, Pageable pageable) { 
     Sort sort = new Sort(Direction.ASC, "sequence"); 
     return this.getByFilter(spec, sort, pageable); 
    } 

} 

Контроллер

@RestController 
@RequestMapping("/modules") 
public class ModuleController extends AbstractController { 
    private final ModuleService moduleService; 

    @Autowired 
    public ModuleController(ModuleService moduleService) { 
     this.moduleService = moduleService; 
    } 

    @RequestMapping(method = RequestMethod.GET) 
    public ResponseTemplate getModules(
      @RequestParam(defaultValue = "0", required = false) int page, 
      @RequestParam(defaultValue = "10", required = false) int size, 
      @RequestParam(defaultValue = "0", required = false) long parentId, 
      @RequestParam(defaultValue = "-1", required = false) int status, 
      @RequestParam(required = false) String name) { 
     ModuleSpecification spec = new ModuleSpecification(parentId, name, status); 
     return ResponseTemplate.successResponse(this.moduleService.getByFilter(spec, 
       new PageRequest(page, size))); 
    } 
    ... 
} 

Существует не ком Проблема ворс, однако я не могу запустить приложение из-за следующей ошибки:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'moduleController' defined in file [D:\Code\study\java\zero\admin\target\classes\com\test\web\controller\ModuleController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.test.service.module.ModuleService]: : No qualifying bean of type [com.test.service.module.ModuleService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.service.module.ModuleService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:747) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1115) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762) ~[spring-context-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) ~[spring-context-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109) [spring-boot-1.1.9.RELEASE.jar:1.1.9.RELEASE] 
     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) [spring-boot-1.1.9.RELEASE.jar:1.1.9.RELEASE] 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) [spring-boot-1.1.9.RELEASE.jar:1.1.9.RELEASE] 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) [spring-boot-1.1.9.RELEASE.jar:1.1.9.RELEASE] 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) [spring-boot-1.1.9.RELEASE.jar:1.1.9.RELEASE] 
     at com.test.Application.main(Application.java:16) [classes/:na] 
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.service.module.ModuleService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:811) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:739) ~[spring-beans-4.0.8.RELEASE.jar:4.0.8.RELEASE] 
     ... 18 common frames omitted 

ответ

0

Причина, как правило, одна из двух вещей, убедитесь, что вы не хватает в @Service аннотацию класса реализации ModuleService. Кроме того, проверьте, если вы в своей конфигурации сканируете пакет, где есть реализация ModuleService

Смежные вопросы