Spring Boot@Autowired注入失败及版本切换后空指针问题求助
Hey there! Let's break down why you're hitting these errors and how to fix them step by step.
First Error: Bean Not Found for CompagnieDao
The root cause here is package scanning scope. Your Application class lives in the com.assurastuce package, and Spring Boot by default only scans for beans in this package and its sub-packages. But your CompagnieDao is in com.dao—a completely separate package—so Spring can't locate it to create a usable bean for injection.
Second Error: NullPointerException After Version Downgrade
This is just a side effect of the same core problem. When you downgraded Spring Boot, the error handling behavior shifted slightly, but the issue remains: compagnieDao isn't being injected properly (it's null), so calling compagnieDao.findAll() throws a null pointer exception.
Step-by-Step Solutions
1. Configure JPA Repository Scanning
Add the @EnableJpaRepositories annotation to your Application class, specifying the package where your CompagnieDao resides. This tells Spring to scan that package for JPA repository interfaces and generate the necessary proxy beans.
2. Ensure Entity Classes Are Detected
Add @EntityScan to let Spring find your Compagnie entity class. Adjust the package path to match where your actual entity class is stored (e.g., if it's in com.entity, use that as the base package).
3. Remove Redundant @Component From CompagnieDao
JPA Repository interfaces don't need the @Component annotation—Spring automatically detects and processes them once scanning is configured correctly. Removing this avoids any potential confusion or misconfiguration.
Corrected Application Class Code
@SpringBootApplication @EnableJpaRepositories(basePackages = "com.dao") @EntityScan(basePackages = "com.entity") // Update this to your actual entity package public class Application implements CommandLineRunner { @Autowired private CompagnieDao compagnieDao; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { System.out.println("tic toc"); List<Compagnie> compagnies = compagnieDao.findAll(); compagnies.forEach(compagnie -> System.out.println(compagnie.getNomCompagnie())); } }
Corrected CompagnieDao Code
// Remove @Component annotation here public interface CompagnieDao extends JpaRepository<Compagnie, Integer> { @Query("select c from Compagnie c where idCompagnie != :idCompagnie") List<Compagnie> getAllCompagnieNonAssurAstuce(@Param("idCompagnie") int idCompagnie); @Query("select c from Compagnie c where nomCompagnie = :nomCompagnie") Compagnie getCompagnieByNom(@Param("nomCompagnie") String nomCompagnie); }
Quick Additional Checks
- Double-check that your
Compagnieentity class is in the package specified in@EntityScan. If it's nested undercom.assurastuce.entity, adjust the base package value accordingly. - Verify your
application.properties/application.ymlhas correct database credentials and configuration—this ensures JPA can connect to your database once the beans are successfully created.
内容的提问来源于stack exchange,提问作者FooBayo

