今天闲的蛋疼,做了一下spring自动注入的例子:
看看spring自动注入的是否可以区别出不同包下相同名字的Java类,答案是不能,spring居然这么渣。
整体结构图如下:
1:首先建立两个同名的java类和接口
com.a包下面有一个IAutoInject接口和实现类AutoInject,同理com.b包下面也有相同的接口和实现类。
同时加上spring声明bean的注解,@component或者@service或者@repository都可以,这里我用@component
代码如下:
[java] view plain copy
1. package com.a;
2.
3. public interface IAutoInject {
4. public void print();
5. }
[java] view plain copy
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. "打印a中的方法");
11. }
12.
13. }
[java] view plain copy
1. package com.b;
2.
3. public interface IAutoInject {
4. public void print();
5. }
[java] view plain copy
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. "打印b中的方法");
11. }
12.
13. }
Spring配置文件如下:
[html] view plain copy
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. >
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
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
1. <span style="background-color: rgb(102, 102, 102);">java.lang.IllegalStateException: Failed to load ApplicationContext</span>
2. 103)
3. 122)
4. 109)
5. 75)
6. 321)
7. 211)
8. 1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
9. 15)
10. 284)
11. 231)
12. 88)
13. 3.run(ParentRunner.java:193)
14. 1.schedule(ParentRunner.java:52)
15. 191)
16. 000(ParentRunner.java:42)
17. 2.evaluate(ParentRunner.java:184)
18. 61)
19. 71)
20. 236)
21. 174)
22. 50)
23. 38)
24. 467)
25. 683)
26. 390)
27. 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. 413)
30. 335)
31. 303)
32. 174)
33. 209)
34. 180)
35. 243)
36. 233)
37. 117)
38. 60)
39. 102)
40. 246)
41. 69)
42. 95)
43. 25 more
看看上面深颜色的报错 ,可以看出
[java] view plain copy
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
1. [com.b.AutoInject]
[java] view plain copy
- 与
[java] view plain copy
- [com.a.AutoInject]
[java] view plain copy
- 冲突了!
[java] view plain copy
- 老老实实改成:
[java] view plain copy
1. @Component(value="a")
[java] view plain copy
1. @Component(value="b")
[java] view plain copy
- 使用的时候改成:
[java] view plain copy
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 踩
今天闲的蛋疼,做了一下spring自动注入的例子:
看看spring自动注入的是否可以区别出不同包下相同名字的Java类,答案是不能,spring居然这么渣。
整体结构图如下:
1:首先建立两个同名的java类和接口
com.a包下面有一个IAutoInject接口和实现类AutoInject,同理com.b包下面也有相同的接口和实现类。
同时加上spring声明bean的注解,@component或者@service或者@repository都可以,这里我用@component
代码如下:
[java] view plain copy
1. package com.a;
2.
3. public interface IAutoInject {
4. public void print();
5. }
[java] view plain copy
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. "打印a中的方法");
11. }
12.
13. }
[java] view plain copy
1. package com.b;
2.
3. public interface IAutoInject {
4. public void print();
5. }
[java] view plain copy
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. "打印b中的方法");
11. }
12.
13. }
Spring配置文件如下:
[html] view plain copy
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. >
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
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
1. <span style="background-color: rgb(102, 102, 102);">java.lang.IllegalStateException: Failed to load ApplicationContext</span>
2. 103)
3. 122)
4. 109)
5. 75)
6. 321)
7. 211)
8. 1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
9. 15)
10. 284)
11. 231)
12. 88)
13. 3.run(ParentRunner.java:193)
14. 1.schedule(ParentRunner.java:52)
15. 191)
16. 000(ParentRunner.java:42)
17. 2.evaluate(ParentRunner.java:184)
18. 61)
19. 71)
20. 236)
21. 174)
22. 50)
23. 38)
24. 467)
25. 683)
26. 390)
27. 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. 413)
30. 335)
31. 303)
32. 174)
33. 209)
34. 180)
35. 243)
36. 233)
37. 117)
38. 60)
39. 102)
40. 246)
41. 69)
42. 95)
43. 25 more
看看上面深颜色的报错 ,可以看出
[java] view plain copy
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
1. [com.b.AutoInject]
[java] view plain copy
- 与
[java] view plain copy
- [com.a.AutoInject]
[java] view plain copy
- 冲突了!
[java] view plain copy
- 老老实实改成:
[java] view plain copy
- @Component(value="a")
[java] view plain copy
1. @Component(value="b")
[java] view plain copy
- 使用的时候改成:
[java] view plain copy
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. }