Spring的依赖注入的2种方式(1天时间)
Posted 多情剑客无情剑;
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring的依赖注入的2种方式(1天时间)相关的知识,希望对你有一定的参考价值。
今天花了一天的时间才调试出来
private 接口 实现类的那个bean;
最后面的那个名字不能随便的写,必须是配置文件中,实现类的那个bean
就是后面的那个名字写错了,花了整整一天
必须有这句注解扫描才能扫描到注解
进行注解的配置,不需要get方法和set方法,小龙儿说xml文件配置才需要set方法
package com.baobaotao1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CarServiceImpl implements CarService { private CarDao carDao; public void setCarDao(CarDao carDao) { this.carDao = carDao; } public void produceDi(){ carDao.produceLun(); System.out.println("閹绘劒绶垫惔鏇犳磸缂佹瑨婧呯�涳拷"); } public static void main(String[] args){ System.out.println(123); ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); CarService boss = (CarService) ctx.getBean("carService"); boss.produceDi(); } }
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'carTest\' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean \'carService\' while setting bean property \'carService\'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'carService\' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property \'carDao\' of bean class [com.baobaotao1.CarServiceImpl]: Bean property \'carDao\' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
以上是关于Spring的依赖注入的2种方式(1天时间)的主要内容,如果未能解决你的问题,请参考以下文章
Spring 依赖注入(DI)详解 [Spring][依赖注入的 6 种实现方式][setter注入][构造器注入][注解注入][自动装配注入][静态工厂注入][实例工厂注入]