Spring Java中的通用DAO和服务

时间:2014-11-30 05:35:12

标签: java spring jpa

我有关于DAO和服务层模式的问题,我使用的是spring-boot-starter-data-jpa。

Dao Layer:

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

服务层

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

1 个答案:

答案 0 :(得分:0)

原因通常是两件事之一,检查您是否错过了ModuleService实现类的@Service注释。另外,检查您的配置中是否正在扫描包含ModuleService实现的软件包