Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A @Service class in package com.acme.billing is not found at startup, and the application class is in com.acme.app. Why, and what are the fixes?

@SpringBootApplication turns on component scanning for the package of the application class and its sub-packages only. com.acme.billing is a sibling of com.acme.app, not a sub-package, so the @Service is never scanned and anything that needs it fails with a missing bean error. The fixes are to move the application class up to com.acme, to list the packages with scanBasePackages, or to bring the class in explicitly with @Import; and if billing is a library, it should provide an auto-configuration instead of relying on being scanned.

Why this question is asked

This is one of the first startup failures almost every Spring Boot developer meets, usually right after reorganising packages or extracting a module. It is easy to fix once we know the rule, and confusing until then, because the class is clearly annotated and clearly on the classpath. Interviewers use it to check that we know how scanning decides where to look. In this answer, we will reproduce the failure, explain the scanning rule behind it, walk through the three fixes with their trade-offs, and look at the case where billing is really a separate library.

1A Quick Recap: How Boot Finds Beans

@SpringBootApplication is a shortcut for three annotations: @SpringBootConfiguration, which marks the class as configuration, @EnableAutoConfiguration, which loads Spring Boot's auto-configurations, and @ComponentScan. Component scanning is the step in which Spring looks through packages for classes annotated with @Component or one of its stereotypes, such as @Service, and registers each as a bean.

@ComponentScan without arguments has one important default: it scans the package of the class it is declared on, and every package below it. Nothing else. That default is why the usual advice is to put the main application class in the root package of the project.

2Reproducing the Failure

Here is the layout from the question.

Java
// src/main/java/com/acme/app/BillingApp.java
package com.acme.app;
@SpringBootApplication
public class BillingApp { public static void main(String[] a) { SpringApplication.run(BillingApp.class, a); } }
// src/main/java/com/acme/app/web/InvoiceController.java
package com.acme.app.web;
@RestController
class InvoiceController {
InvoiceController(InvoiceService invoices) { ... } // needs the service
}
// src/main/java/com/acme/billing/InvoiceService.java
package com.acme.billing;
@Service
public class InvoiceService { ... }

Starting the application fails with a message along these lines: Parameter 0 of constructor in com.acme.app.web.InvoiceController required a bean of type 'com.acme.billing.InvoiceService' that could not be found. Spring Boot's failure analyzer even suggests defining a bean of that type. The controller was found, because com.acme.app.web is below com.acme.app. The service was not.

3Why com.acme.billing Is Not Scanned

Java packages are not nested folders in any meaningful sense for the language, but scanning treats the package name as a prefix. Spring scans com.acme.app and everything whose name starts with com.acme.app. followed by more segments. com.acme.billing does not start with com.acme.app, so it is outside the scanned area, even though both share the parent com.acme.

Notice that the class being public, annotated and compiled does not matter. Scanning never looks there, so Spring never creates a definition for it. The error appears only when some other bean needs it; if nothing injected InvoiceService, the application would start and simply not have the bean.

What the default scan covers
PackageScanned from com.acme.app?Why
com.acme.appyesthe application class's own package
com.acme.app.webyesa sub-package
com.acme.app.billingyesa sub-package
com.acme.billingnoa sibling, not below com.acme.app
com.acmenothe parent is never scanned

4Fix 1: Move the Application Class Up

The cleanest fix for an application that owns both packages is to move BillingApp to com.acme. Now com.acme.app.web and com.acme.billing are both sub-packages of the root, and the default scan finds everything. This also makes @EntityScan and Spring Data repository scanning, which use the same base package by default, cover the new packages without further configuration.

The only thing to check is that com.acme does not contain packages we do not want scanned, for example shared libraries that happen to use the same prefix. If several teams' libraries live under com.acme, scanning from there could pick up their components too, which is the problem the library section covers.

Java
package com.acme; // moved up one level
@SpringBootApplication
public class BillingApp { ... } // now scans com.acme.app.* and com.acme.billing.*

5Fix 2 and Fix 3: scanBasePackages or @Import

If moving the class is not possible, we can tell scanning where to look. scanBasePackages replaces the default, so we must list the application's own package too. Alternatively, scanBasePackageClasses takes classes instead of strings, which survives package renames in an IDE.

The third option avoids scanning entirely for that package: @Import(InvoiceService.class) registers that one class as a bean, or @Import(BillingConfig.class) brings in a configuration class that declares the billing beans. This is explicit and precise, and it is the right choice when we want exactly one or two classes from a package rather than everything in it.

Java
@SpringBootApplication(scanBasePackages = {"com.acme.app", "com.acme.billing"})
public class BillingApp { ... }
// or, type-safe
@SpringBootApplication(scanBasePackageClasses = {BillingApp.class, InvoiceService.class})
// or, explicit
@SpringBootApplication
@Import(InvoiceService.class) // only this class from com.acme.billing
public class BillingApp { ... }

Notice one trap with scanBasePackages: if the billing package also holds JPA entities or Spring Data repositories, those use separate scans, so we may also need @EntityScan and @EnableJpaRepositories with the extra package. Moving the application class avoids that.

6If Billing Is a Library: Use Auto-Configuration

Sometimes com.acme.billing is a separate module or JAR shared by several applications. Then none of the fixes above is ideal, because each consuming application must know the library's package names and the library's internals leak into every main class. Libraries should not rely on being scanned. Instead, the library provides an auto-configuration class that declares its beans, and lists it in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. Adding the dependency is then enough.

Java
// In the billing library
@AutoConfiguration
public class BillingAutoConfiguration {
@Bean @ConditionalOnMissingBean
InvoiceService invoiceService(InvoiceRepository repo) { return new InvoiceService(repo); }
}
// META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
// com.acme.billing.BillingAutoConfiguration

The service class itself then needs no @Service annotation, which also prevents it from being registered twice if some application does scan the package.

7Follow-up questions

Why does the controller get created but the service does not?

Because they are in different packages relative to the application class. com.acme.app.web starts with com.acme.app, so scanning reaches it; com.acme.billing does not. Scanning is purely by package prefix, not by module or folder.

Would the error appear if nothing injected InvoiceService?

No. The application would start without it, and the missing bean would go unnoticed until some code tried to use it, for example through getBean or an event listener that never fires. That is one reason to check the Actuator beans endpoint, or write a small context test, after moving packages.

Is it a problem to put the application class in the default package?

Yes. A class without a package makes scanning cover every class on the classpath, including all libraries, which is slow and usually fails. Spring Boot warns about it. Always give the application class a package, ideally the project's root package.

8Conclusion

In this answer, we saw that @SpringBootApplication scans only the package of the application class and its sub-packages, so a @Service in com.acme.billing is not found when the application class sits in com.acme.app. We can fix it by moving the application class to com.acme, by listing packages with scanBasePackages or scanBasePackageClasses, or by bringing the class in with @Import. And when the code is a shared library, it should not depend on being scanned at all; it should expose its beans through an auto-configuration.

What to say in the interviewAbout 80 seconds aloud

Open with

The cause is the default scanning rule. @SpringBootApplication includes @ComponentScan, and without arguments it scans the package of the annotated class and everything below it, as a package-name prefix.

Then, if they want more

The application class is in com.acme.app, so com.acme.app.web is scanned, but com.acme.billing is a sibling, not a sub-package, so the @Service is never seen and never becomes a bean. Then whatever injects it fails at startup with the message that a bean of type InvoiceService could not be found. There are three fixes.

The cleanest one, if the application owns both packages, is to move the main class up to com.acme, so both are sub-packages; that also keeps entity and repository scanning working. Second, set scanBasePackages to both packages, or scanBasePackageClasses to be refactoring-safe, remembering to include the application's own package and possibly @EntityScan if there are entities.

Third, use @Import to bring in just that class or a configuration class for billing, which is explicit and avoids scanning a whole package.

And if billing is really a shared library, I would not make each application scan it at all: the library should provide an auto-configuration class registered in the AutoConfiguration.imports file, with @ConditionalOnMissingBean beans, so adding the dependency is enough.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A @Service class in package com.acme.billing is not found at startup, and the...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/d0ba47f022/