Spring的依赖注入的2种方式(1天时间)

时间:2023-03-08 22:12:12

今天花了一天的时间才调试出来

private      接口   实现类的那个bean;

最后面的那个名字不能随便的写,必须是配置文件中,实现类的那个bean

就是后面的那个名字写错了,花了整整一天


Spring的依赖注入的2种方式(1天时间)

必须有这句注解扫描才能扫描到注解

Spring的依赖注入的2种方式(1天时间)


Spring的依赖注入的2种方式(1天时间)

Spring的依赖注入的2种方式(1天时间)

进行注解的配置,不需要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();
}
}
Spring的依赖注入的2种方式(1天时间)

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天时间)