不同的包下有文件名相同的java类,spring自动注入会报错

今天闲的蛋疼,做了一下spring自动注入的例子:

看看spring自动注入的是否可以区别出不同包下相同名字的Java类,答案是不能,spring居然这么渣。

整体结构图如下:


1:首先建立两个同名的java类和接口

com.a包下面有一个IAutoInject接口和实现类AutoInject,同理com.b包下面也有相同的接口和实现类。

同时加上spring声明bean的注解,@component或者@service或者@repository都可以,这里我用@component

代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.a;  
  2.   
  3. public interface IAutoInject {  
  4.     public void print();  
  5. }  


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.a;  
  2.   
  3. import org.springframework.stereotype.Component;  
  4.   
  5. @Component  
  6. public class AutoInject implements IAutoInject{  
  7.   
  8.     public void print() {  
  9.         // TODO Auto-generated method stub  
  10.         System.out.println("打印a中的方法");  
  11.     }  
  12.   
  13. }  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.b;  
  2.   
  3. public interface IAutoInject {  
  4.     public void print();  
  5. }  


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.b;  
  2.   
  3. import org.springframework.stereotype.Component;  
  4.   
  5. @Component  
  6. public class AutoInject implements IAutoInject{  
  7.   
  8.     public void print() {  
  9.         // TODO Auto-generated method stub  
  10.         System.out.println("打印b中的方法");  
  11.     }  
  12.   
  13. }  


Spring配置文件如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xmlns:task="http://www.springframework.org/schema/task"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/mvc   
  8.     http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd    
  9.     http://www.springframework.org/schema/beans   
  10.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  11.     http://www.springframework.org/schema/aop  
  12.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  13.     http://www.springframework.org/schema/context   
  14.     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  15.     http://www.springframework.org/schema/task   
  16.     http://www.springframework.org/schema/task/spring-task-3.1.xsd  
  17.     http://www.springframework.org/schema/tx   
  18.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  19.   
  20.     <!-- 自动扫描的包名 -->  
  21.     <context:component-scan base-package="com"></context:component-scan>  
  22.     <!-- 默认的注解映射的支持 -->  
  23.     <mvc:annotation-driven />  
  24.   
  25.   
  26. </beans>  


最后写一个测试类:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.test;  
  2. import javax.annotation.Resource;  
  3. import org.junit.Test;  
  4. import org.junit.runner.RunWith;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.test.context.ContextConfiguration;  
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  8. import org.springframework.test.context.transaction.TransactionConfiguration;  
  9. import org.springframework.transaction.annotation.Transactional;  
  10. import com.a.IAutoInject;  
  11.   
  12. //@Transactional  
  13. //@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)  
  14. @RunWith(SpringJUnit4ClassRunner.class)  
  15. @ContextConfiguration(locations = { "classpath:applicationContext.xml" })  
  16. public class MainTest {  
  17.     @Resource  
  18.     private IAutoInject autoInjecta;  
  19.     @Resource  
  20.     private com.b.IAutoInject autoInjectb;  
  21.     @Test  
  22.     public void test(){  
  23.         autoInjecta.print();  
  24.         autoInjectb.print();  
  25.     }  
  26. }  

最后报错:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="background-color: rgb(102, 102, 102);">java.lang.IllegalStateException: Failed to load ApplicationContext</span>  
  2.     at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:103)  
  3.     at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)  
  4.     at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)  
  5.     at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)  
  6.     at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)  
  7.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)  
  8.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)  
  9.     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)  
  10.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)  
  11.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)  
  12.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)  
  13.     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)  
  14.     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)  
  15.     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)  
  16.     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)  
  17.     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)  
  18.     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)  
  19.     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)  
  20.     at org.junit.runners.ParentRunner.run(ParentRunner.java:236)  
  21.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)  
  22.     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)  
  23.     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)  
  24.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)  
  25.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)  
  26.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)  
  27.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)  
  28. <span style="background-color: rgb(102, 102, 102);">Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [applicationContext.xml]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'autoInject' for bean class [com.b.AutoInject] conflicts with existing, non-compatible bean definition of same name and class [com.a.AutoInject]</span>  
  29.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:413)  
  30.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335)  
  31.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)  
  32.     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)  
  33.     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)  
  34.     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)  
  35.     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)  
  36.     at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:233)  
  37.     at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:117)  
  38.     at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)  
  39.     at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:102)  
  40.     at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:246)  
  41.     at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:69)  
  42.     at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:95)  
  43.     ... 25 more  


看看上面深颜色的报错 ,可以看出
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Annotation-specified bean name 'autoInject' for bean class [com.b.AutoInject] conflicts with existing, non-compatible bean definition of same name and class [com.a.AutoInject]  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [com.b.AutoInject]  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 与  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [com.a.AutoInject]  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 冲突了!  

 
 
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 老老实实改成:  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Component(value="a")  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Component(value="b")  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 使用的时候改成:  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class MainTest {  
  2.     @Resource(name="a")  
  3.     private IAutoInject autoInjecta;  
  4.     @Resource(name="b")  
  5.     private com.b.IAutoInject autoInjectb;  
  6.     @Test  
  7.     public void test(){  
  8.         autoInjecta.print();  
  9.         autoInjectb.print();  
  10.     }  
  11. }  
 
0
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值