繁体   English   中英

字段需要一个在通用JPA DAO体系结构中找不到的类型的bean

[英]Field required a bean of type that could not be found on Generic JPA DAO architecture

我正在尝试在春季启动时为我的项目定义架构

我要做的是创建一个从JpaRepository扩展的通用存储库

public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
}

之后,每个EntityDao将从BaseRepository扩展

@Repository    
public interface AuthorityDao extends BaseRepository<Authority, Long> {

        Authority findById(Long idRole);

        Authority findByRoleName(String findByRoleName);

    }

这就是我在存储库层上执行此操作的方式。 在服务层,我创建一个名为GenericService的类,该类实现IGenericService并将BaseRepository注入其中:

@Service
public class GenericService<T, D extends Serializable> implements IGenericService<T, D> {

    @Autowired
    @Qualifier("UserDao")
    private BaseRepository<T, D> baseRepository;
// implemented method from IGenericService

}

每个服务将从GenericService扩展:

public class AuthorityService extends GenericService<Authority, Long> implements IAuthorityService {

    @Autowired
    GenericService<Authority, Long> genericService;

运行项目时,出现以下错误:


申请开始失败


描述:
fr.java.service.impl.GenericService中的字段baseRepository需要找不到类型为“ fr.config.daogeneric.BaseRepository”的bean。

行动:
考虑在配置中定义类型为“ fr.config.daogeneric.BaseRepository”的bean。

我怎么解决这个问题?

更新:

@SpringBootApplication
@EntityScan("fr.java.entities")
@ComponentScan("fr.java")
@EnableJpaRepositories("fr.java")
@EnableScheduling
@EnableAsync
@PropertySource({ "classpath:mail.properties", "classpath:ldap.properties" })
@EnableCaching
@RefreshScope
public class MainApplication extends SpringBootServletInitializer {

    private static final Logger log = LoggerFactory.getLogger(MainApplication.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MainApplication.class);
    }

    public static void main(String[] args) {
        log.debug("Starting {} application...", "Java-back-end-java");

        SpringApplication.run(MainApplication.class, args);
    }

}

您有这个问题,是因为您将GenericService创建为bean并尝试注入BaseRepository但是Spring无法做到这一点,因为不清楚哪个类对BaseRepository进行了参数设置。

从我的角度来看,我可以建议您下一步:首先,您不应该将GenericService用作bean,他的所有子代都应该是bean,您应该在子类中删除对GenericService注入,它们已经扩展了。 您的GenericService应该是抽象的,并且可以有抽象方法getRepository ,该方法将在GenericService内使用,并且存储库的注入将在GenericService子类中完成。

所以你应该有这样的东西:

public abstract class GenericService<T, D extends Serializable> implements IGenericService<T,D> {
    abstract BaseRepository<T, D> getRepository();
}

@Service
public class AuthorityService extends GenericService<Authority, Long> implements IAuthorityService {

    @Autowired
    BaseRepository<Authority, Long> baseRepository;

    public BaseRepository<Authority, Long> getRepository() {
        retrurn baseRepository;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM