JUnit & IntelliJ:无法自动装配,找不到对应类型Bean求助
NoSuchBeanDefinitionException for MultiplicationService in Your Test Let's break down what's going wrong and how to fix it step by step:
Root Cause
The error pops up for two key reasons:
MultiplicationServiceis just an interface — Spring can’t create a bean from an interface directly. It needs a concrete implementation class that Spring can manage as a bean.- You added the
@Repositoryannotation to the interface, which is incorrect:@Repositoryis meant for data access layer (DAO) implementation classes, not service interfaces.- Even if you’d used the right annotation for services (
@Service), adding it to the interface still wouldn’t work — stereotype annotations like@Serviceneed to be on the concrete implementation so Spring knows to instantiate it.
Step-by-Step Solution
1. Create a Concrete Implementation of MultiplicationService
Make a new class MultiplicationServiceImpl that implements your MultiplicationService interface. Add the @Service annotation to this class so Spring detects it and creates a managed bean.
package microservices.book.multiplication.service; import org.springframework.stereotype.Service; import microservices.book.multiplication.domain.Multiplication; @Service public class MultiplicationServiceImpl implements MultiplicationService { private final RandomGeneratorService randomGeneratorService; // Constructor injection (preferred over @Autowired for dependencies) public MultiplicationServiceImpl(RandomGeneratorService randomGeneratorService) { this.randomGeneratorService = randomGeneratorService; } @Override public Multiplication createRandomMultiplication() { int factorA = randomGeneratorService.generateRandomFactor(); int factorB = randomGeneratorService.generateRandomFactor(); int result = factorA * factorB; return new Multiplication(factorA, factorB, result); } }
2. Clean Up the Interface
Remove the @Repository annotation from your MultiplicationService interface — interfaces don’t need Spring stereotype annotations. Your interface should look like this:
package microservices.book.multiplication.service; import microservices.book.multiplication.domain.Multiplication; public interface MultiplicationService { Multiplication createRandomMultiplication(); }
3. Verify Component Scanning
Your SocialMultiplicationApplication uses @SpringBootApplication, which automatically scans for beans in its own package (microservices.book.multiplication) and all subpackages. Since your service implementation lives in microservices.book.multiplication.service, Spring will find it without any extra configuration.
Why This Works
- The
@Serviceannotation tells Spring to create a singleton bean ofMultiplicationServiceImpland add it to the application context. - When your test uses
@AutowiredforMultiplicationService, Spring will inject theMultiplicationServiceImplbean (since it implements the interface) — resolving the missing bean error. - Constructor injection in the implementation class is the recommended way to inject dependencies (like
RandomGeneratorService) in Spring, as it makes your code more testable and aligned with best practices.
After making these changes, your MultiplicationServiceTest should run without the NoSuchBeanDefinitionException.
内容的提问来源于stack exchange,提问作者Space Cadet

