Spring无法将名称生成器注入Mybatis Mapper Bean导致ConflictingBeanDefinitionException

时间:2018-06-05 01:57:52

标签: java spring mybatis

我有一些 spring。* xml ,例如spring-web.xmlspring-service.xmlspring-dao.xml。 这些xml使用PackageNameGenerator生成Bean名称。

<!-- spring-web.xml -->
<!-- scan Bean -->
<context:component-scan base-package="com.ahao" use-default-filters="false"
                        name-generator="com.ahao.core.beans.PackageBeanNameGenerator">
    <!-- only scan Controller -->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
</context:component-scan>
<!-- spring-dao.xml -->
<!-- scan Bean -->
<context:component-scan base-package="com.ahao" use-default-filters="false"
                        name-generator="com.ahao.core.beans.PackageBeanNameGenerator">
    <!-- only scan DAO -->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
public class PackageBeanNameGenerator implements BeanNameGenerator {
    private static final Logger logger = LoggerFactory.getLogger(PackageBeanNameGenerator.class);

    @Override
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
        String beanClassName = Introspector.decapitalize(definition.getBeanClassName());
        logger.debug("init Bean Name: " + beanClassName);
        return beanClassName;
    }
}

现在我有一些Java类并生成Bean名称。 在调试代码之后,我发现ModuleDAO没有使用PackageNameGenerator来生成Bean名称。它会导致ConflictingBeanDefinitionException

- com.ahao.web
    - moduleOld
        - ModuleController (BeanName: com.ahao.web.moduleOld.ModuleController)
        - ModuleService    (BeanName: com.ahao.web.moduleOld.ModuleService    )
        - ModuleDAO        (BeanName: ModuleDAO        )
    - moduleNew
        - ModuleController (BeanName: com.ahao.web.moduleNew.ModuleController)
        - ModuleService    (BeanName: com.ahao.web.moduleNew.ModuleService    )
        - ModuleDAO        (BeanName: ModuleDAO        )
@Repository
public interface ModuleDAO {
    List<String> getNames(); // use ModuleMapper.xml
}

我猜我的Mybatis代理类不能通过component-scan标记include-filter过滤器进行扫描。 但我需要拆分xml。我该怎么办?

0 个答案:

没有答案