@Resource是按ByName自动注入,BeanFactory查找容器中全部bean,找出其中id属性与属性同名的bean进行注入。
对于这句不是很懂 "找出其中id属性与属性同名的bean进行注入" ,谁能详细说说,用一小段代码解释下最好!谢谢...

解决方案 »

  1.   

    "找出其中id属性与属性同名的bean进行注入",id:就是你配置的类的id属性,这个在Spring中是唯一的。“属性”指的是需要引入id为某个值的类。比如说我要在A这个对象中注入id为b的B类的对象,则A中存在属性名字如: private B b;这样Spring的BeanFactory就会在自己的Map容器中根据id找到相应的对象,完成注入。希望说的能帮楼主理解。
      

  2.   


    <bean id="customDAO" class="com.softeem.crm.dao.impl.CustomDAOImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
          
        <bean id="customService" class="com.softeem.crm.service.impl.CustomServiceImpl">
         <property name="customDAO" ref="customDAO"></property>
        </bean>
    比如说这两个,就可以根据ID进行注入。
    其中,每一对bean标签中的id,在其class中有对应的熟悉名称。
      

  3.   

    我用注解的话在配置文件里面不需要配置这个ID吧,它是通过什么来判断这个Id?还有就是用注解的话引用上一层的接口可以不要set方法吗?
      

  4.   

    注解本身就帮助省去了getter方法,不会用到setter方法吧..
      

  5.   


    请说一下 没有setter方法 他是怎么给属性赋值的
      

  6.   


    牛逼,打错了,是不需要用到getter方法。多谢纠正。
      

  7.   

    今天配了一下依赖注入注解 出了这个错 不知道常见不?还是新手 不知道怎么调
    org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'saveStudentAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDaoImpl' defined in file [D:\java\apache-tomcat-6.0.13\webapps\SSHStudentManager\WEB-INF\classes\com\dao\impl\StudentDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
    org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'studentServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDaoImpl' defined in file [D:\java\apache-tomcat-6.0.13\webapps\SSHStudentManager\WEB-INF\classes\com\dao\impl\StudentDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
    org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'studentDaoImpl' defined in file [D:\java\apache-tomcat-6.0.13\webapps\SSHStudentManager\WEB-INF\classes\com\dao\impl\StudentDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
    Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
      

  8.   

    其实没有setter方法一样可以赋值的,
      

  9.   

    ,根据的就是你这个成员变量的名称,比如private A a,则这个a就是id。