spring's management of beans
catalogue
2. Scope and life cycle of bean object
1. How to create bean s
(1) Use default constructor
•
The bean tag is used in the Spring configuration file. After the id and class attributes are configured, there is no configuration
For other properties and tags, the default constructor is used to create bean objects. At this time, if the class
Object cannot be created without a default constructor in.
(2) The set method is injected, and the transfer collection (dynamically creating the factory method and indirectly calling userserviceimpl4) is managed by bean.
main method
public static void main(String[] args) { // 1. Get the core container object ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //4.set method injection and transfer collection (dynamically create factory method and indirectly call userserviceimpl4) IUserService service5 = (IUserService) context.getBean("userService5"); service5.queryUser(); // 3. Close the container (if you don't remember to close the container, the most typical problem is that the database connection cannot be released) ((ClassPathXmlApplicationContext) context).close(); }
applicationContext.xml main configuration file
<bean id="myFactory" class="com.iflytek.bookmanagesystem.verbFactory"> </bean> <bean id="userService5" factory-bean="myFactory" factory-method="createIUserService"> <property name="strings"> <array> <value>verb Rush</value> <value>verb Roll, roll</value> <value>verbFactoty in fact</value> </array> </property> </bean>
(3) The set method is injected, and the transfer collection (the factory method is created statically and calls userserviceimpl4 indirectly) is managed through bean s.
main method
public static void main(String[] args) { // 1. Get the core container object ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //5.set method injection and transfer collection (create factory method statically and directly call userserviceimpl4) IUserService service6 = (IUserService) context.getBean("userService6"); service6.queryUser(); // 3. Close the container (if you don't remember to close the container, the most typical problem is that the database connection cannot be released) ((ClassPathXmlApplicationContext) context).close(); }
applicationContext.xml main configuration file
<bean id="userService6" class="com.iflytek.bookmanagesystem.staticFactory" factory-method="createIUserService"> <property name="strings"> <array> <value> static Rush</value> <value> static Operation room</value> <value> staticFactoty Fang Gang</value> </array> </property> </bean>
UserServiceImpl4 code
public class UserServiceImpl4 implements IUserService{ private String[] strings; private List<String> list; private Set<String> set; private Map< String, String> map; private Properties properties; public void setStrings(String[] strings) { this.strings = strings; } public void setList(List<String> list) { this.list = list; } public void setSet(Set<String> set) { this.set = set; } public void setMap(Map<String, String> map) { this.map = map; } public void setProperties(Properties properties) { this.properties = properties; } public String[] getStrings() { return strings; } public List<String> getList() { return list; } public Set<String> getSet() { return set; } public Map<String, String> getMap() { return map; } public Properties getProperties() { return properties; } @Override public String toString() { return "UserServiceImpl4 [strings=" + Arrays.toString(strings) + ", list=" + list + ", set=" + set + ", map=" + map + ", properties=" + properties + "]"; } @Override public String queryUser() { System.out.println(Arrays.toString(strings)); System.out.println(list); System.out.println(set); System.out.println(map); System.out.println(properties); return ""; } }
2. Scope and life cycle of bean object
–
In bean Tags
scope
Property is used to specify the scope of the object. The general values of this attribute are:
singleton
,
prototype
.
–
scope="singleton"
, the default value, represents the singleton object
•
An application has only one instance of an object. Its scope is the entire reference.
•
Life cycle:
–
Object birth: when the application loads and creates a container, the object is created.
–
Object alive: the object is alive as long as the container is.
–
Object death: when the application unloads and destroys the container, the object is destroyed.
–
scope="prototype"
, representing multiple objects
•
Each time an object is accessed, the object instance is recreated.
•
Life cycle:
–
Object birth: creates a new object instance when using an object.
–
Object alive: as long as the object is in use, it will always be alive.
–
Object death: when an object is not used for a long time, it is recycled by the java garbage collector.
An extended bean factory (for example, in a web environment) may support more scopes, such as
"request" or "session".
•
Request: the scope of the request applied to the Web application
•
Session: the session scope used for Web applications
•
Global session: the session scope (global session scope) acting on the cluster environment, but not a set
In a group environment, it is a session
There are other attributes in the bean tag related to the life cycle:
•
Init method: Specifies the name of the initialization method in the class.
•
Destroy method: Specifies the name of the destroy method in the class.