Spring: Automatically assemble user according to Bean name

Source: Internet
Author: User

This example describes how to automatically assemble a User object according to the Bean's name !

The Autowire property of the <bean> element is responsible for automating the assembly <bean> labeling, defining the properties of the JavaBean. This can save a lot of configuration JavaBean attribute tag code, make the code more neat, beautiful;

However, there are also negative effects: after using automatic assembly, it is not possible to read from the configuration file what attributes JavaBean needs.

1. Write the user object with the following code:

 Packagecom.importnew; Public classUser {PrivateString name= "Test"; PrivateString sex= "Male"; Private intage=29; PrivateString tel= "123456789";  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getsex () {returnsex; }     Public voidsetsex (String sex) { This. Sex =sex; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString Gettel () {returnTel; }     Public voidSettel (String tel) { This. Tel =Tel; } @Override PublicString toString () {return"User [name=" + name + ", sex=" + Sex + ", age=" + Age+ ", tel=" + Tel + "]"; }    }

2. Define the Bean (Testutil) and inject the User object into the Testutil object. The code is as follows:

 Package com.importnew;
   Public class testutil { private user user; Public   User GetUser () {   return user; Public void setUser (user user ) {  this. user =   user; } }

3. Set the automatic assembly of the Bean in spring's configuration file Applicationcontext.xml, and Spring will automatically inject the User object into the specified bean based on the attribute name in the bean. The code is as follows;

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-conte        Xt.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd ">        <BeanID= "User"class= "Com.importnew.User" />          <BeanAutowire= "ByName"ID= "Testutil"class= "Com.importnew.TestUtil"  />     </Beans>

4. Write the test class testspring with the following code:

 Packagetest;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.importnew.TestUtil;ImportCom.importnew.User; Public classtestspring { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Testutil Testutil= (testutil) context.getbean ("Testutil"); User User=Testutil.getuser ();        System.out.println (User.getage ());    System.out.println (User.getname ()); }}

———————————————————————————————————————————————————

Note:

1. Automatic assembly of problems by Bean name.

It is possible to assemble JavaBean incorrectly according to the Bean name, and if the configuration file defines JavaBean with the same name as the JavaBean that need to be automatically assembled, the different types of JavaBean will be injected incorrectly.

2. If you change the autowire= "ByName" in the configuration file to "Bytype", you can implement automatic assembly of User by Bean type . The code in the actual configuration file is as follows:

<bean autowire= "Bytype" id= "Testutil" class= "Com.importnew.TestUtil"/>

3. About the difference between byname and Bytype:

ByName is automatically assembled by the name of the Bean. If the name of a bean is the  same as the name of another bean, it is automatically assembled.
The bytype automatically assembles automatically through the Bean's attribute type. If a Bean's property type is compatible with another Bean's property type, it is automatically assembled.
4. Automatic assembly can occur automatically whenever there is no automatic assembly, either by the type of bean or by the name of the bean. For example, in the configuration file to add a User class implementation object, Bytype automatic assembly type will be unable to automatically identify the assembly that one javabean and throw Org.springframework.beans.factory.UnsatisfiedDependencyException exception. To solve this type of problem, you can only specify which JavaBean to assemble by mixing manual assembly.

End

Spring: Automatically assemble user according to Bean name

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.