【Java】JPA Dao 注入失败 “Consider defining a bean of type xxx in your configuration”

springboot jpa dao注入失败。

需要使用@EnableJpaRepositories和@EntityScan注解指定dao和model的位置。jpa接口与普通的dao不同,不能通过@Repository注入。

例子:

import com.xiaomi.miui.ad.preinstall.dao.model.App;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface AppDao extends CrudRepository {
    List findByPackageName(String packageName);
}

@SpringBootApplication(scanBasePackages = "com.xiaomi.miui.ad.preinstall")
@EnableJpaRepositories("com.xiaomi.miui.ad.preinstall.dao")
@EntityScan("com.xiaomi.miui.ad.preinstall.dao.model")
public class Main implements CommandLineRunner {
   //.....
}

参考:https://stackoverflow.com/questions/48579678/spring-crudrepository-is-not-created

你可能感兴趣的:(java)