Spring2.5继续坚持了这个发展趋向,特别是为那些使用Java 5或更新版本java的开发人员提供了进一步简化而强大的新特性。这些新特性包括:注解驱动的依赖性注入(annotation-driven dependency injection),使用注解而非XML元数据来自动侦测classpath上的Spring组件,注解对生命周期方法的支持,一个新的web控制器模型将请求映射到加注解的方法上,在测试框架中支持Junit4,Spring XML命名空间的新增内容,等等。
项目中使用的spring2.5版本。如果想要使用spring提供的注解功能.
applicationContext.xml配置文件中增加如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- Using annontations -->
<context:annotation-config></context:annotation-config>
新增以下三项才可以使用注解功能
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config></context:annotation-config>
或者<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/> 注册一个单独的Spring post-processor.
一个没有实现任何接口的业务类,配置如下:
public class LogService extends Service{
public LogBiz(){}
@Resource //默认按名称查找bean. 如果找不到,则按类型匹配bean.
private BaseDao dao;
}
tomcat启动错误1:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logBiz': Injection of resource fields failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.ccms.base.dao.BaseDAO] is defined: expected single matching bean but found 40: [baseDAO, loginDAO, logDAO]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessAfterInstantiation(CommonAnnotationBeanPostProcessor.java:291)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:876)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:437)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:383)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:353)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
... 32 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.ccms.base.dao.BaseDAO] is defined: expected single matching bean but found 40: [baseDAO, loginDAO, logDAO]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:583)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:418)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:497)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:145)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:79)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessAfterInstantiation(CommonAnnotationBeanPostProcessor.java:288)
... 42 more
使用@Resource默认按名称查找bean. 如果找不到,则按类型匹配bean.而名称dao没有找到,所以会根据BaseDAO匹配,但是BaseDAO是一个接口:
<bean id="loginDAO" class="com.test.dao.loginDAO" parent="baseDAO" />
<bean id="logDAO" class="com.test.dao.logDAO" parent="baseDAO" />
<bean id="baseDAO" class="com.ccms.base.dao.BaseDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="simpleJdbcTemplate">
<ref bean="simpleJdbcTemplate" />
</property>
</bean>
有两个实现了BaseDAO接口的类,所以spring就不知道应该使用哪一个了。直接抛出异常。
解决方法1:
我们可以看到baseDAO名称是唯一的, 所以如果想要按照名称匹配,则直接写baseDAO就可以了
@Resource
private BaseDAO baseDAO;
解决方法2:
@Autowired //默认按照类型匹配,同样应为有两个子类bean,所以抛出异常。
@Qualifier("loginDAO") //限定bean
private BaseDAO dao;
参考博客:
http://doc.chinaunix.net/java/200801/240056.shtml
http://stackoverflow.com/questions/10534053/autowiring-two-beans-implementing-same-interface-how-to-set-default-bean-to-au
posted on 2013-03-21 10:22
David1228 阅读(2482)
评论(0) 编辑 收藏 所属分类:
Spring