Spring's Bean management (XML way)
1. Three way Bean instantiated
- Examples of the use of class constructor (no default parameter)
- Examples of static factory method (simple factory pattern)
- Examples of using the method of Example plant (factory methods)
Normally use the default settings mode: mode without constructor arguments.
Show
Here no longer make a specific description of the demo, I have uploaded the relevant code github, you can visit the following link reference:
Three kinds of ways to instantiate Bean (GitHub)
2. Bean Configuration
id and name
- In general, when assembling a Bean, an id attribute specified by the name of Bean
- id attribute must be unique within the vessel IOC
- If the name Bean contains special characters, you need to use the name attribute
class
class is used to set a full path name of the class, the main role is an example of class generating IOC container.
Bean scopes
category | Explanation |
---|---|
singleton | Bean instance only in the presence of a container SpringIOC, by way of a single example Bean presence |
prototype | Returns a new instance every time getBean call () |
request | Each HTTP request will create a new Bean, that scope applies only to the environment WebApplicationContext |
session | Share a same HTTP Session Bean, different HTTP Session use different Bean. The scope applies only to the environment WebApplicationContext |
scope properties to configure. singleton default value (singleton).
Commonly used as a singleton and protoype.
Show
- Bean is singleton scope:
<bean id="person" class="com.test.ioc.demo3.Person"/>
Print-out test results are as follows:
Examples can be seen getBean return addresses are the same, so a single embodiment.
- Bean scopes when more cases:
<bean id="person" class="com.test.ioc.demo3.Person" scope="prototype"/>
Print out test results:
getBean return address is not the same instance, prototype each call getBean () will return when the new instance.