Spring Boot Bean Injection Issue with Package Scanning

Spent 10 minutes wondering why my Spring Boot bean wasn't being injected. No errors. No warnings. Just null. The code looked fine: @Service public class PaymentService { // logic } But Spring couldn't find it. The problem: My main class was in com.myapp My service was in com.payments Spring Boot only scans the package where @SpringBootApplication lives and its sub-packages. The fix: Move the service under com.myapp.payments Or add: @SpringBootApplication(scanBasePackages = "com") But be careful with broad scans - they slow startup. Simple rule: Keep everything under your main package. #SpringBoot #Java #BackendDevelopment #Programming

Same thing happens with @Repository and @Controller. If they're outside the scan path, Spring won't see them. Check your package structure first before debugging deeper.

Like
Reply

To view or add a comment, sign in

Explore content categories