Why is the no-args constructor needed to run this simple Spring configuration?

I tried to follow the manual at Mkyong regarding Spring. I created two simple classes: Customer and Person, which look like this:

Customer

public class Customer {

    private Person person;

    public Customer() {
    }

    //Getters and setters for person here

    public String toString() {
        return "Customer [person=" + person +"]";
    }
}

Person

public class Person {

    private String name;
    private String address;
    private int age;

    public Person() {

    }

    public Person(String name, String address, int age) {
        this.name = name;
        this.address = address;
        this.age = age;
    }

    //All getters and setters

    public String toString() {
        return "Person [address=" + address + ", age=" + age + ", name=" + name + "]";
    }
}

Then I created a Bean configuration file using an internal Bean (which is why I took the tutorial), which looks like this:

// Standard bean declarations

    <bean id="CustomerBean" class="com.andrew.SpringInnerBeans.Customer">
        <property name="person">
            <bean class="com.andrew.SpringInnerBeans.Person">
                <property name="name" value="Andrew" />
                <property name="address" value="Address" />
                <property name="age" value="27" />
            </bean>
        </property>
    </bean>
</beans>

Finally, I created MainApp to run this code:

public static void main (String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
            "NewBean.xml"});

    Customer cust = (Customer) context.getBean("CustomerBean");
    System.out.println(cust);

}

Now, to my understanding, this is what happens:

The xml file is loaded, and the Bean with the identifier CustomerBean is stored in the reference cust (which is the Customer object). CustomerBean accepts the "person" argument, and the details of the three parameters that Person creates are delivered as an internal customerBean Bean.

MainApp toString cust .

, , ? , Person, . , Bean, , ?

Edit:

:

Customer [person=Person [address=4 Ascot House, age=27, name=Andrew]]

:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustomerBean' defined in class path resource [NewBean.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.andrew.SpringInnerBeans.Customer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at com.andrew.SpringInnerBeans.MainApp.main(MainApp.java:10)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.andrew.SpringInnerBeans.Customer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1088)
    ... 13 more
Caused by: java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getDeclaredConstructor(Unknown Source)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
    ... 14 more

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustomerBean' defined in class path resource [NewBean.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.andrew.SpringInnerBeans.Customer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at com.andrew.SpringInnerBeans.MainApp.main(MainApp.java:10)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.andrew.SpringInnerBeans.Customer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1088)
    ... 13 more
Caused by: java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getDeclaredConstructor(Unknown Source)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
    ... 14 more
+4
4

, Spring.

  • bean Customer : ok new Customer() no-arg .
  • bean Person : ok new Person(), no-arg,
  • Person bean : ok
  • Customer bean , setter Person bean : ok
  • , .

, beans new , Spring no-arg.

:

public class Customer {

    private Person person;

    public Customer(Person person) {
        this.person = person;
    }
    ...
}

XML :

<bean id="CustomerBean" class="com.andrew.SpringInnerBeans.Customer">
    <constructor-arg>
        <bean class="com.andrew.SpringInnerBeans.Person">
            <property name="name" value="Andrew" />
            <property name="address" value="Address" />
            <property name="age" value="27" />
        </bean>
    </constructor-arg>
</bean>

, Spring Person bean, , Customer bean new Customer(person) arg.

+2

Person (String name, String address, int age). - :

 ... 
<property name="person">
        <bean class="com.andrew.SpringInnerBeans.Person">
            <constructor-arg index="0" value="Andrew"/>
            <constructor-arg index="1" value="Address"/>
            <constructor-arg index="2" value="27"/>
       </bean>
 </property>
... 

. Person, Customer - ; )

+2

XML. , Spring .

. : Spring beans ?

, Spring, .

: <bean id="CustomerBean" class="com.andrew.SpringInnerBeans.Customer"> <property name="person"> <bean class="com.andrew.SpringInnerBeans.Person"> <constructor-arg index="0" value="Andrew"/> <constructor-arg index="1" value="Address"/> <constructor-arg index="2" value="27"/> </bean> </property> </bean>

Spring, , no-arg. , .

+1

, , bean, , ?

bean, , no-arg. Spring ( ), Spring :

  • Customer no-arg
  • Person no-arg
  • person seters
  • person person, , seters

freakman answer shows how to create a Person object using a constructor argument, I suppose you can also create a client object. Then you will not need no-arg ctors.

+1
source

All Articles