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: <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. When www.2cto.com Spring instantiates a container, it verifies the configuration of each Bean in BeanFactory. These verifications include: whether the dependent Bean referenced by the 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. [Html] <bean id = "person" class = "lee. Person"> <! -- Property configuration depends on the injected property --> <property name = "name" value = "chenming"/> <Property name = "age" value = "22"/> </ bean> Spring will set <bean... /> 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. [Java] // obtain lee. class Object of the Person Class personClass = Class. forName ("lee. person "); // create lee. default instance of the Person Class Object personBean = personBean. newInStance (); after the instance is created, Spring will traverse the <bean .. /> all <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: [java] // setter method for obtaining the name attribute String setName = "set" + "Name"; // obtain lee. the Set () Name method in the Person class is java. lang. reflect. method setMethod = personClass. getMethod (setName, aVal. getClass (); // call the SetName () method of the Bean instance setMethod. 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. [Html] <bean id = "person" class = "lee. person "> <constructor-arg index =" 0 "value =" aVal "/> <constructor-arg index =" 1 "value =" bVal "/> </bean> the above code, spring will use code similar to the following to create Bean instances: [java] // obtain lee. class Object of the Person Class class personClass = Class. forName ("lee. person "); // obtain the first parameter of the aVal type, and the second parameter of the bVal Type Constructor personCtr = personClass. getConstructor (aVal. getClass (), bVal. getClass (); // specify the constructor to create the Bean Instance 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.