升级Spring到3.2.9适配Java8时Autowired依赖注入失败求助
Spring 3.2.9升级后Bean创建报错排查请求
为适配Java 8,我将Spring库升级至3.2.9版本,目前遇到如下报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'acsManagerImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.taxstream.acs.repository.LdapDAO net.taxstream.acs.service.AcsManagerImpl.ldapDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ldapDao' defined in ServletContext resource [/WEB-INF/caa-servlet.xml]: Cannot resolve reference to bean 'ldapTemplate' while setting bean property 'ldapTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ldapTemplate' defined in ServletContext resource [/WEB-INF/caa-servlet.xml]: Cannot resolve reference to bean 'contextSource' while setting bean property 'contextSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'contextSource' is defined
相关代码及配置
AcsManagerImpl类
@Service public class AcsManagerImpl implements AcsManager { @Autowired private AcsRepository acsRepository; @Autowired private LdapDAO ldapDao; /** * @param acsRepository the acsRepository to set */ public void setAcsRepository(AcsRepository acsRepository) { this.acsRepository = acsRepository; } public <T extends Serializable> Page<T> getComponent(Class<T> type, Pageable pageable) { return acsRepository.getComponent(type, pageable); } }
LdapDAO接口
public interface LdapDAO<E> { List<E> getUserInfo(final String uid); List<E> searchUsers(final String uid); void setFilter(final String filter); void setAttributes(final String[] attrs); }
caa-servlet.xml配置片段
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate" p:contextSource-ref="contextSource" p:ignorePartialResultException="true" /> <bean id="ldapDao" class="net.acs.repository.LdapDaoImpl"> <property name="ldapTemplate" ref="ldapTemplate" /> <property name="filter" value="(uid=@)" /> <property name="attributes"> <list> <value>cn</value> <value>mail</value> <value>uid</value> <value>givenname</value> <value>sn</value> </list> </property> </bean>
applicationContext.xml配置片段
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"> <property name="url" value="ldap://C5QSQ13:10389/" /> <property name="base" value="ou=people,o=sevenSeas" /> <property name="anonymousReadOnly" value="true" /> </bean>
核心问题分析
报错根源是:NoSuchBeanDefinitionException: No bean named 'contextSource' is defined,即caa-servlet.xml所在的Servlet上下文无法找到定义在applicationContext.xml中的contextSource Bean。
解决思路
- 检查
applicationContext.xml加载配置:确认web.xml中已配置ContextLoaderListener,且contextConfigLocation参数正确指向applicationContext.xml,确保根上下文被正确初始化。示例配置:<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> - 验证上下文层级权限:Spring Web应用中,Servlet上下文是根上下文的子上下文,子上下文可访问根上下文Bean,若无法访问需排查配置冲突或加载顺序问题。
- 检查
contextSourceBean定义:确认applicationContext.xml中contextSource的id拼写无误、类路径正确,无XML语法错误导致Bean无法创建。 - 临时测试定位问题:将
contextSource的Bean定义临时复制到caa-servlet.xml中,若报错消失,说明是根上下文加载问题,需重点排查web.xml配置。
内容的提问来源于stack exchange,提问作者redds1
相关产品推荐
相关产品推荐

