Spring bean自定义命名策略(注解实现)

我们都知道项目后台开发是从 控制层——业务层——mybatis层,@Controller、@Service、@Mapper...等等注解可以将对象自动加载到bean容器中,还能实现相应的功能,使用起来得心应手。并且模块化实现解耦合,套用模板增删改查等功能很快就写出来。

那有没有想过一个问题,如果在不同模块中有相同名称的类,那注解将对象自动加载到bean容器中的名称是默认的类名称,会不会有重名的问题?会不会程序启动报错?答案当然会。

例如:

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'imageController' for bean class [io.renren.modules.swr.controller.ImageController] conflicts with existing, non-compatible bean definition of same name and class [io.renren.modules.db.controller.ImageController]
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:349)
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:287)
    at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:295)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:249)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:206)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:174)
    ... 13 common frames omitted

利用翻译软件翻译一下:为bean类[io.renren.modules.swr.controller.ImageController]指定的注解bean名称“imageController”与同名类[io.renren.modules.db.controller.ImageController]的现有的、不兼容的bean定义冲突。 即,存在两个imageController,导致bean冲突。

大致知道原因了,怎么解决呢?以下有两种解决方案

  • 修改重复的类名称

这相当于废话...也感觉很鸡肋,难道没有自定义bean的命名策略吗?肯定有的,请看以下方案

  • 自定义bean命名策略

@Controller——>@Controller("dbImageController"),可以传入一个String类型的参数(只要不重名叫什么都行,不写默认是小写开头的类名称),表示将bean名称重命名为dbImageController

例如:

@Service——>@Service("dbImageServiceImpl"),可以传入一个String类型的参数,表示将bean名称重命名为dbImageServiceImpl

例如:

@Mapper,由于@Mapper不支持传入参数,但是我们可以写个自定义注入的注解@Component("dbImageDao"),表示将bean名称重命名为dbImageDao,等同于@Repository("dbImageDao")

例如:

ok,现在启动程序,如果你要是使用了基础实体类(继承BaseEntity)的话,你又会发现报错

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.apache.ibatis.type.TypeException: The alias 'ImageEntity' is already mapped to the value 'io.renren.modules.swr.entity.ImageEntity'.
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
    ... 56 common frames omitted
Caused by: org.apache.ibatis.type.TypeException: The alias 'ImageEntity' is already mapped to the value 'io.renren.modules.swr.entity.ImageEntity'.
    at org.apache.ibatis.type.TypeAliasRegistry.registerAlias(TypeAliasRegistry.java:157)
    at org.apache.ibatis.type.TypeAliasRegistry.registerAlias(TypeAliasRegistry.java:147)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
    at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1553)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    at com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.buildSqlSessionFactory(MybatisSqlSessionFactoryBean.java:517)
    at com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.afterPropertiesSet(MybatisSqlSessionFactoryBean.java:430)
    at com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.getObject(MybatisSqlSessionFactoryBean.java:628)
    at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration.sqlSessionFactory(MybatisPlusAutoConfiguration.java:214)
    at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration$$EnhancerBySpringCGLIB$$1cfc4994.CGLIB$sqlSessionFactory$1(<generated>)
    at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration$$EnhancerBySpringCGLIB$$1cfc4994$$FastClassBySpringCGLIB$$641672b3.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration$$EnhancerBySpringCGLIB$$1cfc4994.sqlSessionFactory(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 57 common frames omitted

报错信息的翻译是:别名'ImageEntity'已映射到'io.renren.modules.swr.entity.ImageEntity'.

同样的我们加个注解,@Alias("dbImageEntity"),表示将实体名称重命名为dbImageEntity,重启服务完美解决问题

例如:

其实技术不是太难,难的是遇到问题解决的过程,但是只有通过困难才能磨练自己,成长的道路上不仅要学会解决问题的技巧。还要学习掌握高级的技能,扩大自己的视野,一步一步实现自己的目标。

我来评几句
登录后评论

已发表评论数()

相关站点

热门文章