3rd. Minimizing Spring XML configuration

Source: Internet
Author: User
Tags tutorialspoint

Directory:
    1. Automatically assemble bean properties
    2. Assembling with annotations
    3. Automatically detect beans
    4. Using spring Java-based configuration
Automatic assembly of Bean properties four types of automatic assembly:

1 ByName The name of the ID and the name of the property, to ensure that the name of the attribute in the bean instance is the same as the ID name of the assembly.

2 Bytype determines the assembled bean by type, but when there are multiple types of matching beans, an error occurs.

3 Contructor When constructing the injection, the assembly method is used, and the effect is as bytype.

4 AutoDetect first try to use constructor for automatic assembly. If it fails, try using Bytype to automatically assemble the line again. (Tested, 3.0.5 version is not available, do not know whether it was removed.) )

Automatic assembly takes-bytype as an example to explain:

In Bytype assembly mode, spring first reflects the bean autowire= "Bytype", gets the return type of the Bean property, and then goes to the spring container by class

Type to match, and finally inject the matching bean into the current bean. Look at an example to understand:

1. Create a new package Com.tutorialspoint.autowire.bytype and create a new Zoo.java class in the package with the following contents:

 PackageCom.tutorialspoint.autowire.bytype;ImportCom.tutorialspoint.autowire.Cat;ImportCom.tutorialspoint.autowire.Dog;ImportCom.tutorialspoint.autowire.Duck; Public classZoo {Privatecat Cat; PrivateDog Dog; PrivateDuck Duck; //Automatic assembly does not apply to the original type. We can manually assemble this property at this time    PrivateString Zooname;  Public voidsetzooname (String zooname) { This. Zooname =Zooname; }     Public voidSetcat (cat cat) { This. cat =Cat; }     Public voidSetdog (dog dog) { This. Dog =Dog; }     Public voidSetduck (Duck Duck) { This. Duck =Duck; }     Public voidprint () {if(Cat = =NULL) {System.out.println ("Cat is null"); } Else{cat.sayhi (); }        if(Dog = =NULL) {System.out.println ("Dog is null"); } Else{dog.sayhi (); }        if(Duck = =NULL) {System.out.println ("Duck is null"); } Else{duck.sayhi ();    } System.out.println (Zooname); }}
View Code

2. Create a new configuration file Autowire_bytype.xml in the SRC directory. The contents are 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"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd ">       <!--A bean named Zoo can be adapted to the type of bean named Cat1, Dog1, duck1 when assembled according to Bytype.        So eventually the CAT1, Dog1 and Duck1 will be injected into the zoo's cat, dog, and duck properties respectively. When assembling by type, if the type of two beans is met, spring does not know which one to use, and we can use primary= "true" to tell spring to use this bean first . -   <BeanID= "CAT1"class= "Com.tutorialspoint.autowire.Cat"Primary= "true"></Bean>   <BeanID= "Cat2"class= "Com.tutorialspoint.autowire.Cat"></Bean>   <BeanID= "Dog1"class= "Com.tutorialspoint.autowire.Dog"></Bean>   <BeanID= "Duck1"class= "Com.tutorialspoint.autowire.Duck"></Bean>      <BeanID= "Zoo"class= "Com.tutorialspoint.autowire.bytype.Zoo"Autowire= "Bytype">       <!--because automatic assembly is only available for reference types, normal types also need to be injected manually -       < Propertyname= "Zooname"value= "International_zoo"></ Property>   </Bean></Beans>

Note:

    1. To identify a preferred bean for automatic assembly, you can use the primary property of the <bean> element. If the primary property of a candidate bean with only one auto-assembly is set to true, the bean will be preferred over other candidate beans. But the primary property has a very weird point, and its default setting is true. This means that all candidate beans become the preferred bean (so there is no preferred bean in fact). All, in order to use the primary property, we have to set the primary property of all non-preferred beans to false.
    2. The primary property is only meaningful for identifying preferred beans. If the assembly is automatic. We want to exclude certain beans, which can set the Autowire-candidate property of these beans to false, as shown in the following example:
<id= "saxophone"  class= "Com.springinaction.springidol.Saxophone"  autowire-candidate= "false"/>
Default Automatic assembly

If you need to configure the same Autowire property for each bean (or most of them) in the context of the spring app, you can add a Default-autowire attribute on the root element <beans>:

 <?  xml version= "1.0" encoding= "UTF-8"  ?>  <  beans  xmlns  = "Http://www.springframework.org/schema/beans"   Xmlns:xsi  = "Http://www.w3.org/2001/XMLSchema-instance"   Xsi:schemalocation  = "Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans-3.0.xsd "  Default-autowire  = "Bytype"  >  </ beans  >  

Attention

    1. The Default-autowire is applied to all beans in the specified spring configuration file, not all beans applied to the context of the spring application. We can define multiple profiles in one application context, each of which can have its own default automatic assembly policy.
    2. Again, not because we have configured a default automatic assembly policy, it means that all beans can only use this default automatic assembly policy. We can also use the Autowire property of the <bean> element to override the configured default automatic assembly policy.
    3. Automatic assembly policies and explicit assembly strategies can be used, and explicit assembly overrides automatic assembly.

3rd. Minimizing Spring XML configuration

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.