InvocationTargetException(Bean没有注入) 和 spring初始化构造函数

本文针对Spring框架中的依赖注入错误进行了详细解析,特别是当构造函数含有参数时如何正确配置实现类。通过实例展示了如何使用XML配置文件指定构造函数参数的具体设置。

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

代码调试的时候报上述错误,发现时配置spring是服务注入错误。

也就是相当于用接口声明了个变量,没有找到接口的实现类。

 

当需要在spring中配置的实现类构造函数有参数时,如果要在spring中注入可以如下:

<bean id="***Service"
  class="com.**.**.***impl">
        <constructor-arg index="0" value = "abcd" />
        <constructor-arg index="1" value = "1000" />
        <constructor-arg index="2" value = "2000" />
 </bean>

 

2025-05-07 09:33:36.120 INFO 13244 --- [ restartedMain] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited 2025-05-07 09:33:36.122 WARN 13244 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shiroFilterFactoryBean' defined in class path resource [com/aaa/config/ShiroConfig.class]: Unsatisfied dependency expressed through method 'getShiroFilterFactoryBean' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityManager' defined in class path resource [com/aaa/config/ShiroConfig.class]: Unsatisfied dependency expressed through method 'getDefaultWebSecurityManager' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRealm': Unsatisfied dependency expressed through field 'menuService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'menuServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'menuMapper' defined in file [D:\医学信息学\学生-springboot医院信息管理系统源码\hospital\target\classes\com\aaa\mapper\MenuMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation
最新发布
05-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值