首页 > 编程语言 > 详细

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

时间:2017-08-14 20:55:51      阅读:491      评论:0      收藏:0      [点我收藏+]

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

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

原文:http://www.cnblogs.com/cs-lcy/p/7360004.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!