IOC container default at startup will initialize all the bean configuration file, when the code is not our mistake, but mistakes can be quickly loaded container tips to help us exclude coding errors, this is called fail-fast, without modification in most cases, but we do not need a special time when the container is loaded on startup bean object, such as a project of a feature not commonly used, load so many resources when we do not need to start the project, the project will reduce wasteful time-sensitive, you need to set the bean objects lazy loading, is the only call it when (getBean) and then load it, which would be subject to the bean plus lazy-init attribute, attribute value is true, the default is false, generally do not write as follows:
In the case of multiple profiles, we will introduce in a configuration file additional configuration file in XML is to use the Import label introducing additional configuration file, as follows:
<importresource="applicationContext.xml"/>
The introduction of the XML configuration file with annotations ways to do this:
@ImportResource("applicationContext.xml")
The role of these two approaches are the same, any one can grasp.
bean's name property
Since the name attribute is not common bean, is not too much to explain here, and its role as the id attribute, to the current bean objects marked facilitate access to a name, except that the name attribute values may be provided to make a plurality of names, between the comma separated, note, from the plurality of names can not be repeated, and not to any other bean name attribute value is repeated.
Injection anonymous inner bean this way corresponds to the object assigned to the object assignment, there are two ways, one is through the new object, and then set the properties of the object to the object property assignment, transmission parameters by one is parameterized constructor assigned to the object in the bean configuration file created by an object can be assigned to objects, practices are as follows:
<beanid="outer"class="..."><!-- 像set一样实例化对象的方式用这样的注入方式 --><propertyname="target"><beanclass="com.example.Person"><!-- this is the inner bean --><propertyname="name"value="Fiona Apple"/><propertyname="age"value="25"/></bean></property><!-- 像带参构造函数实例化对象的方式用这样的注入 --><constructor-argname="target"><beanclass="com.example.Person"><!-- this is the inner bean --><propertyname="name"value="Fiona Apple"/><propertyname="age"value="25"/></bean></constructor-arg></bean>
Injection type attribute set
Injected interlinked nature, are assigned, do not explain, list injection method, Map injection method:
<!-- lsit注入 --><propertyname="someList"><list><!-- 普通元素 --><value>a list element followed by a reference</value><!-- 引用类型元素 --><refbean="myDataSource"/></list></property><!-- Map注入 --><propertyname="someMap"><map><entrykey="an entry"value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map></property>
C namespace and the nature of the traditional injection method as set the same way as an instance of an object, only need to modify the configuration file header information
Can by C: injecting property value, than the traditional injection method is simple and convenient
P namespace like nature and manner of a conventional injection-argument constructor of the object with the same instance, only need to modify the configuration file header information
We can, through P: injecting property value, than the traditional injection method is simple and convenient
Using the following injection mode, when the IOC container starts various parameter values will load the database connection, more efficient, since when the need to change the database, only need to modify the value jdbc.properties file.