我有一些 spring。* xml ,例如spring-web.xml
,spring-service.xml
和spring-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。我该怎么办?