Error creating bean with name '***' defined in URL

本文介绍了解决Spring MVC与Shiro整合时出现的bean未找到的问题。通过在spring-context-shiro.xml中配置<context:component-scan base-package=com.beat.zjw.shiro/>实现自动扫描注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在spring-mvc.xml中配置了<context:component-scan base-package="com.beta.zjw"/>自动装配bean,然后在spring-context-shiro.xml中使用这些bean。

在启动时报错:

Error creating bean with name 'shiroFilter' defined in URL [file:spring-context-shiro.xml]: Cannot resolve reference to bean 'securityManager' while setting bean property 'securityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityManager' defined in URL [file:spring-context-shiro.xml]: Cannot resolve reference to bean 'systemAuthorizingRealm' while setting bean property 'realm'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'systemAuthorizingRealm' available

根据web.xml的配置:

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:/spring-mybatis*.xml
			classpath*:/spring-context-shiro.xml
		</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:/spring-mvc*.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

spring-mvc.xml与spring-context-shiro.xml从属于不同容器。

所以,虽然在spring-mvc.xml中配置了自动扫描注入,但在spring-context-shiro.xml这个容器中,这些bean name是不存在的。

在spring-context-shiro.xml配置对需要使用的包com.beta.zjw.shiro自动扫描注入:

<context:component-scan base-package="com.beat.zjw.shiro"/>

问题解决。

 

 

我们一般使用这种方法获取配置文件:

ClassPathXmlApplicationContext re = new ClassPathXmlApplicationContext("applicationContext.xml");

而由spring自动装配的xml文件,根据web.xml的配置:

<param-name>contextConfigLocation</param-name>

虽然spring-mybatis.xml、spring-context-shiro.xml和spring-mvc.xml配置的param-name是同样的名字,

但spring-mvc.xml的param-name属于servlet类springMVC:

<servlet-name>springMVC</servlet-name>

所以在springMVC以外,是拿不到在spring-mvc.xml中配置的自动注入的。

 

当你在Spring框架中遇到 "Error creating bean with name 'configProperties' defined in URL" 的错误,这通常意味着Spring容器在尝试初始化或创建一个名为 'configProperties' 的bean时遇到了问题。这个错误可能是由于以下几个原因: 1. **配置文件问题**:可能是在Spring的配置文件(如application.properties或application.yml)中,配置属性 'configProperties' 没有正确定义,或者引用的配置源不存在。 2. **Bean定义问题**:检查定义在指定URL(可能是XML或Java配置类中的)的 'configProperties' 类或bean是否正确,包括类路径、注解或XML元素是否匹配。 3. **依赖注入失败**:如果 'configProperties' 是一个依赖其他bean的,那么那些依赖可能没有正确注入,或者注入的目标bean有问题。 4. **构造函数或工厂方法问题**:确保 'configProperties' 类有一个无参构造函数或者Spring可以正确地找到用于创建实例的方法。 5. **Spring版本兼容性**:检查使用的Spring版本是否与应用的其他依赖项兼容,某些版本的更新可能会引入兼容性问题。 解决这个问题,你可以按照以下步骤操作: 1. **查看日志**:检查详细的错误堆栈信息,它通常会提供关于问题发生位置的线索。 2. **检查配置**:验证配置文件中的bean定义和属性设置。 3. **代码审查**:查看相关的Java代码,确认bean的定义和使用是否符合规范。 4. **启用调试模式**:在开发环境中启用Spring的详细日志输出,有助于找出问题根源。 5. **逐步排除**:尝试移除或更改配置,看是否能简化问题,以确定是哪个部分导致的错误。 如果你能提供更具体的上下文或错误堆栈信息,我可以给出更精确的建议。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值