在springmvc中注入服務時用@Service
當有兩個實現類時都標明@Service后則會出現異常:
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.james.dao.impl.BaseDao] is defined: expected single matching bean but found 2:
這是因為都標明了@Service會自動注入,這時會導致不清楚實際運行時實例化哪一類。
解決:
方法一
實際運用哪一個實現類就在哪個類上注入@Service,另外一個不標注
方法二
都不標注@Service,在resource文件夾中配置 applicationContext.xml,表明實際運行哪個實現類。
<bean id="serviceImpl" class="com.james.service.impl.ServiceImpl"></bean>