Spring Reading notes-----The configuration dependency of spring bean

Source: Internet
Author: User

The previous blog introduces the basic concepts and scopes of beans in spring (Spring Reading notes-----The basic concepts of spring bean beans), and now introduces The basic configuration of the Spring Bean .

from the beginning we know that the nature of the mutual invocation of each component in a Java application can be summed up as a dependency relationship. Depending on the injection method, theBean 's dependency injection can be divided into two forms:

1 , properties: Through <property .../> element configuration, corresponding to set injection.

2 , constructor parameters: through the <constructor-arg.../> element configuration, corresponding to the construction injection.

Whether it is a property or a constructor parameter, it is considered a dependency of the Bean , accepting the Spring container Management, the value of the dependency is either a definite value, or the other in the spring container a reference to the Bean.

in general, I'm not supposed to manage references to normal properties in a configuration file, usually just using a configuration file to manage the dependencies of the Bean instances in the container.

When you instantiate a container, Spring verifies the configuration of each Bean in the beanfactory . These checks include:

Whether the bean referenced by the beans refers to a legitimate bean.

Whether the Bean's normal property value obtains a valid value.

for Singleton -scoped beans, if there is no forced cancellation of its pre-initialization behavior, the system will pre-initialize the Spring container when it is created Singleton Bean, at the same time, the Bean that the Bean relies on is also instantiated together.

beanfactoryAndApplicationContextIn the instantiation container.BeanThe timing is also different:beanfactoryWait until the program needsBeanInstance is created only when theBean, whileApplicationContextis in the creationApplicationContextinstance, all of the containers are pre-initializedBean。 At the same time, createbeanfactoryis not created immediatelyBeaninstance, it is possible that the program can be created correctlybeanfactoryinstance, but when the requestBeaninstance, it still throws an exception: CreateBeanAn error occurred while the instance or its dependency was injected. Therefore, when the delay of configuration error occurs, it brings some unsafe factors to the system. andApplicationContextThe default is to pre-initialize allSingletonScope ofBeanSoApplicationContextThe instantiation process is morebeanfactoryThe time and memory overhead of the instantiation process is large, but once the creation succeeds, the response will be very fast after the application, and the configuration error can be verified, so it is generally recommendedApplicationContextAsSpringContainer.

In fact, we can specify lazy-int= "true" to force the cancellation of Singleton -scoped Bean's pre-initialization. This way the Bean does not pre-instantiate with the applicationcontext boot.

Spring can inject any type of property for any Java object, as long as the Java object provides the corresponding setter method for that property.

[HTML]View Plaincopyprint?
  1. < Bean id="person" class="Lee." Person ">
  2. <!--property configuration requires dependency injection properties--   
  3. < Property name="name" value="Chenming" />
  4. < Property name="age" value="$" />
  5. </ Bean >   
<bean id= "Person" class= "Lee." The person ' ><!--property configuration needs to rely on injected properties--><property name= "name" value= "chenming"/><property name= "age" Value= "/></bean>"

Spring creates a Java object for the <bean.../> element, and one such Java object corresponds to a bean instance, For the above code,Spring uses the following form to create the Java instance.

[Java]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. //Get Lee. The class object of the person class
    2. Class Personclass = Class.forName ("Lee.  Person ");
    3. //Create Lee. Default instance of the person class
    4. Object Personbean = Personbean.newinstance ();
Get Lee. The Class object class  Personclass = Class.forName ("Lee. Person ");//Create Lee. The default instance of the Person class object Personbean = Personbean.newinstance ();


When the instance is created,Spring will traverse the <bean. all <property.../> child elements of the/> element. The <bean.../> element each contains a <property.../> child element, andSpring invokes the setter for that Bean instance method. Similar to the following program:

[Java]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. //get Setter Method for Name property    
    2. string setname =  "set" + ;   
    3. //get Lee. The set () name method inside the person class   
    4. java.lang.reflect.method  Setmethod = personclass.getmethod (Setname, aval.getclass ());   
    5. //call the SetName () method of the Bean instance   
    6. Setmethod.invoke (personbean, aval);   
Gets the setter method of the Name property string setName = "Set" + "Name";//Get Lee. The set () name method inside the person class Java.lang.reflect.Method Setmethod = Personclass.getmethod (SetName, Aval.getclass ());// Call the SetName () method of the Bean instance Setmethod.invoke (Personbean, Aval);


for using the <constructor-arg.../> element to specify constructor injection,Spring does not use the default constructor to create a Bean instance. Instead, a specific constructor is used to create the Bean instance.

[HTML]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
  1. < Bean id="person" class="Lee." Person ">
  2. < Constructor-arg index="0" value="Aval" />
  3. < Constructor-arg index="1" value="BVal" />   
  4. </ Bean >   
<bean id= "Person" class= "Lee." Person "><constructor-arg index=" 0 "value=" aval "/><constructor-arg index=" 1 "value=" BVal "/></bean >

for the code above,Spring creates a Bean instance with code similar to the following:

[Java]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. //get Lee. Class object    of the person class;
    2. class  personclass =  Class.forName ( ");   
    3. // Gets the first parameter is the Aval type, the second argument is the constructor for the Bval type   
    4. constructor  Personctr = personclass.getconstructor (Aval.getclass (), Bval.getclass ());   
    5. //to specify the constructor to create a bean instance   
    6. Object bean = personctr.newinstance (aval,bval);   
Get Lee. The Class object class  Personclass = Class.forName ("Lee. Person ");//Gets the first parameter is the Aval type, the second argument is the Bval type constructor constructor personctr = Personclass.getconstructor (Aval.getclass (), Bval.getclass ());//Create a bean instance with the specified constructor object bean = Personctr.newinstance (aval,bval);


The above program is just one instance, in fact Spring also needs to be based on the <property.../> element,<contructor-arg. The/> element uses the value property,ref attribute, and so on to determine what data type needs to be injected, and to make the appropriate type conversions for these values, so the actual processing of Spring is more complex.

Read Li Gang, "Lightweight Java EE enterprise Application Practice"

(GO) Spring reading notes-----Spring's bean configuration dependency

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.