springboot project startup exception – required a single bean, but 2 were found
-
- description li>
- detailed error message li>
- error analysis li>
- why li>
- solution li> ul>
problem description
springboot+mybatis, write interface and implementation classes, and use @autowired injection controller, start exception: required a single bean, but 2 were found
controller interface:
@RestController @RequestMapping("/api/sys/statistic") public class StatisticController { @Autowired private ISysAccessLogService iSysAccessLogService; }
interface and implementation class
detail error
APPLICATION FAILED TO START
Description:Field iSysAccessLogService in com.msb.sys.controller.StatisticController required a single bean, but 2 were found:
– sysAccessLogServiceImpl: defined in file [/Users/ronghuilin/java/msbjava/target/classes/com/msb/sys/service/impl/SysAccessLogServiceImpl.class]
– ISysAccessLogService: defined in file [/Users/ronghuilin/java/msbjava/target/classes/com/msb/sys/service/ISysAccessLogService.class]Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
Disconnected from the target VM, address: ‘127.0.0.1:61739’, transport: ‘socket’
error analysis
if you’re prompted, there’s no way to automatically inject iSysAccessLogService at StatisticController.
@autowired is injected by type by default, and as prompted, my project has two beans of this type, so it cannot be injected automatically.
The
error message also suggests a solution:
1. Use one of the beans with the @primary annotation as the default,
2. Add @qualifier to the injected property to specify beanName to specify which beanto use
but I’ve only written one implementation class for the interface, and as expected there’s only one bean, which should be fine.
Reason
h2>
after a roundabout search, I found that I had annotated springboot’s boot class with @mapperscan (” “com”).
@mapperscan this is the annotation is the Mapper scan annotation for mybatis, and the scope of the scan I specified is the com package.
mybatis scans the interface under com, generates the implementation class of the interface, and infuses it into spring.
then spring scans the interface implementation class I wrote, injects spring, and you have two beans.solution
in the springboot project it is recommended to add @mapper directly to the Mapper interface and not to use @mapperscan.
if you want to use @mapperscan annotations, be careful to configure the scope of the scan to avoid the above problems caused by repeat scan injection of spring beans.
Read More:
- @Autowired injection * * required a single bean, but 2 were found**
- Springboot startup exception: error creating bean with name ‘permissioncontroller’
- Springboot startup error – classnotfound Exception:EnhancerBySpringCGLIB
- Field userrepository in com.example.demo2.service.imp.UserServiceImp required a bean of type ‘com.ex
- error Newline required at end of file but not found eol-last
- Idea SpringBoot:Error creating bean with name ‘XXXXController‘
- Could NOT find CUDNN: Found unsuitable version “..“, but required is at least “6“
- Springboot error: property ‘sqlsessionfactory’ or ‘sqlsessiontemplate’ are required error details
- Datasource bean injection failed, with startup error
- Idea startup project Lombok error, no symbol found
- Solve the problem that springboot does not scan @ repository
- Error creating bean with name usercontroller appears when springboot starts
- An error is reported when springboot starts: error creating bean with name ‘XXXX’
- Idea startup project: main class not found or cannot be loaded
- Springboot integration redis reports non null key required (solved)
- Tomcat startup project warning: org.apache.jasper.servlet.tldscanner.scanjars at least one jar has been scanned for TLD but does not contain TLD
- An error was reported on the command line of Vue when it first wrote a small project: expected indentation of 4 spaces but found 6
- Springboot startup error: java.lang.IllegalArgumentException : Property ‘sqlSessionFactory’ or ‘sqlSessionTempla
- How to handle exception in springboot
- Info:Memory module [DIMM] needs attention: Single-bit warning error rate exceeded, Single-bit fai…