--constructor in spring by constructing automatic assembly

Source: Internet
Author: User

In spring, you can use automatic assembly by construction, which is actually automatically assembled by the parameter type of the constructor. This means that if the data type of a bean is the same as the data type of the constructor parameters of other beans, it will be automatically assembled.

 Package Auto_constructor; /**  */Publicclass  student    {public  String getName () {         return name;    }      Public void setName (String name) {        this. Name = name;    }      Private   String name;}

 Package Auto_constructor; /**  */Publicclass  school    {public  School (student St) {         this. st=St;    }      Public Student Getst () {        return  st;    }     Private student St;}

<!--  Construction method Injection   is actually automatically assembled by the parameter type of the constructor--    class= "Auto_constructor.student" >        <property name= "name" value= "Cat" ></property>    </bean>    class= "Auto_ Constructor.school "Autowire=" constructor "></bean>

In spring, "auto-assemble by automatic detection" means that if there is a default constructor (parameter and any data type) The constructor injection is installed, and if no constructor is automatically assembled by type.

<bean id= "Student" class= "auto_constructor.student" >        <property name= "name" value= "Cat" ></ property>    </bean>    <bean id= "School" class= "Auto_constructor.school" autowire= "AutoDetect" ></bean>

--constructor in spring by constructing automatic assembly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.