码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-08-14 21:14:16      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:color   今天   get   actor   code   reference   except   alt   class   

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

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

标签:color   今天   get   actor   code   reference   except   alt   class   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!