我想问下,能不能使用内部bean实现构造注入啊?<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" >
<constructor-arg index="0" ref="personDao" type="cn.itcast.dao.PersonDao" />
<constructor-arg index="1" value="构造注入" type="java.lang.String"/>
<property name="personDao1">
<bean class="cn.itcast.dao.impl.PersonDaoBean" /> 
</property>
</bean>
这时配置文件,执行报错了;Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'personDao' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'personDao' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:479)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:162)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:925)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:835)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at test.SpringTest.main(SpringTest.java:16)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'personDao' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:971)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
... 20 more

解决方案 »

  1.   

    <constructor-arg index="0" ref="personDao" type="cn.itcast.dao.PersonDao" />
    这句有问题
    可以这么写
    <constructor-arg index="0" ref="personDao" />
    <bean class="cn.itcast.dao.impl.PersonDaoBean" id="personDao"/>  
      

  2.   

    不行,报错了
    No constructor with 1 argument defined in class 'cn.itcast.service.impl.PersonServiceBean'
      

  3.   

    No bean named 'personDao' is defined
      

  4.   

    能不能使用内部bean实现构造注入啊?有没有这个功能的,在spring里面,我知道有内部bean实现set注入,但是构造注入就不知道了
      

  5.   

    personDao 访问不到。可能在访问不到的文件里 也有可能是顺序问题。
      

  6.   

    能不能使用内部bean实现构造注入啊?有没有这个功能的,在spring里面,我知道有内部bean实现set注入,但是构造注入就不知道了
      

  7.   


    为什么要构造注入呢 set注入是普遍的比较好的
      

  8.   

    ++1
    另外这类配置文件 要细心。
    'personDao' 这个东西在这里找不到配置
      

  9.   

    所谓构造注入,创建对象调用相应的构造函数。应该是你的配置和你的所想注入的构造函数不匹配的缘故。
    比如
    public class Person{
       private Car car;
       public Person(){}
       public Person(Car car){this.car=car;}
    ……
    }
    现在你应该想构造注入 public Person(Car car)
    那么配置应该是
    <bean id="person" class=....>
       <Constructor-arg><ref bean="宝马"/> </Constructor-arg>
    </bean>
    <bean id="宝马" class=..../>