Spring Reading Notes -- Spring Bean configuration dependency

Source: Internet
Author: User

The previous blog introduced the basic concepts and scopes of Bean in Spring (Spring Reading Notes ----- basic concepts of Bean in Spring). Now we will introduce the basic configurations of Spring Bean.

From the beginning, we know that the essence of mutual calls between components in Java applications can be summarized as dependency. Depending on the injection method, Bean dependency injection can be divided into two forms:

1. properties: Use <property... /> Element configuration, corresponding to set injection.

2. constructor parameters: Use <constructor-arg... /> Element configuration, corresponding to construction injection.

Both attributes and constructor parameters are regarded as Bean dependencies and are managed by Spring containers. The dependency value is either a fixed value or a reference of other beans in the Spring container.

In general, I should not manage the reference of common attributes in the configuration file. Generally, I only use the configuration file to manage the dependencies of Bean instances in the container.

Spring verifies the configuration of each Bean in BeanFactory when instantiating a container. These verifications include:

Whether the dependent Bean referenced by Bean points to a valid Bean.

Whether the common attribute values of Bean get a valid value.

If the Pre-initialization of a Bean in the singleton scope is not forcibly canceled, the system will pre-initialize the singleton Bean used when creating the Spring container. At the same time, the Bean on which the Bean depends is also instantiated.

BeanFactory and ApplicationContext have different timing for instantiating beans in the container: BeanFactory creates Beans only when the program needs Bean instances, while ApplicationContext creates ApplicationContext instances, all beans in the container are pre-initialized. At the same time, BeanFactory does not immediately create Bean instances. Therefore, it is possible that the program can correctly create BeanFactory instances, but an exception is still thrown when requesting Bean instances: an error occurs when creating a Bean instance or injecting its dependency. Therefore, when the delay of configuration errors occurs, it may cause some insecure factors to the system. ApplicationContext is the default pre-initialized Bean of all singleton scopes. Therefore, the ApplicationContext instantiation process has a higher time and memory overhead than the BeanFactory instantiation process, but once created successfully, the response speed after the application is very fast and the configuration error can be checked. Therefore, we recommend that you use ApplicationContext as the Spring container.

In fact, we can specify lazy-int = "true" to forcibly cancel the pre-initialization of the Bean in the singleton scope. In this way, the Bean will not be pre-instantiated as ApplicationContext starts.

Spring can inject any type of attribute to any java object, as long as the java object provides the corresponding setter method for this attribute.

1 <bean id = "person" class = "lee. Person"> 2 <! -- Property configuration depends on the injected property --> 3 <property name = "name" value = "chenming"/> 4 <Property name = "age" value = "22"/> 5 </bean>

Spring will... /> Element to create a java object. Such a java object corresponds to a Bean instance. For the above Code, Spring will use the following form to create a Java instance.

1 // obtain lee. 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 ();

 

After the instance is created, Spring will traverse all the <property... /> Child element. <Bean... /> Each element contains a <property... /> Child element, Spring will call the setter method once for the Bean instance. Similar to the following program:

1 // setter method 2 String setName = "set" + "name"; 3 // obtain lee. set () Name Method 4 java in the Person class. lang. reflect. method setMethod = personClass. getMethod (setName, aVal. getClass (); 5 // call the SetName () method 6 setMethod of the Bean instance. invoke (personBean, aVal );

For the use of <constructor-arg... /> Element to specify the constructor injection. Spring does not use the default constructor to create Bean instances, but uses a specific constructor to create Bean instances.

1     <bean id="person" class="lee.Person">2         <constructor-arg index="0" value="aVal" />3         <constructor-arg index="1" value="bVal" />4     </bean>

 

For the above Code, Spring will use code similar to the following to create Bean instances:

1 // obtain lee. class Object of the Person Class 2 Class personClass = class. forName ("lee. person "); 3 // obtain the first parameter of the aVal type, and the second parameter of the bVal Type Constructor personCtr = personClass. getConstructor (aVal. getClass (), bVal. getClass (); 5 // create Bean instance with the specified constructor 6 Object bean = personCtr. newInstance (aVal, bVal );

 

The above program is only an instance. In fact, Spring also needs to follow the <property... /> Element, <contructor-arg .. /> The value Attribute and ref attribute used by the element determine the data type to be injected, and perform proper type conversion for these values, therefore, the actual processing process of Spring is more complicated.

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.