FullyQualifiedAnnotationBeanNameGenerator启动异常

@SpringBootApplication
@ComponentScan(nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

@RestController
@RequestMapping("/test")
public class TestAction {
    @RequestMapping("/aaa")
    public String aaa(){
        return "aaa";
    }
}

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'testAction' method 
org.example.action.TestAction#aaa()
to { [/test/aaa]}: There is already 'org.example.action.TestAction' bean method
org.example.action.TestAction#aaa() mapped.

RequestMapping 映射冲突了。

可是只定义了一个Action
或者去掉nameGenerator 就正常

我猜是这样的:

@SpringBootApplication 已经包含了 @ComponentScan,默认就会扫描当前包,及其子包下的组件。

而你又定义了一个 @ComponentScan 扫描了 FullyQualifiedAnnotationBeanNameGenerator 类所在的包。

这两次扫描,都加载了同一个 Controller,所以导致映射冲突。

谢谢。应该是这问题,配置到
@SpringBootApplication(nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class) 就行了

1 Like