In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces what are the characteristics of the factory pattern in Spring. It is very detailed and has a certain reference value. Friends who are interested must read it!
I. the advantages of hungry Han style creation
After the factory is created, all the objects in the Spring configuration file are created (hungry).
Improve the running efficiency of the program. Avoid multiple IO and reduce object creation time. (the concept is close to the connection pool, create it at once, and obtain it directly when you use it.)
II. Life cycle approach
Custom initialization method: add the "init-method" attribute, and Spring will call this method after the object is created.
Custom destruction method: add the "destroy-method" attribute, and Spring will call this method before destroying the object.
Destroy: after the factory's close () method is called, Spring destroys all singleton objects that have been created.
Classification: Singleton objects are destroyed by Spring containers, and Prototype objects are destroyed by JVM.
III. Notes on life cycle
Initialize comments, destroy comments
Import javax.annotation.PostConstruct;import javax.annotation.PreDestroy; @ PostConstruct / / initialize public void init () {System.out.println ("init method executed");} @ PreDestroy / / destroy public void destroy () {System.out.println ("destroy method executed");} IV. Lifecycle phase
Single case bean:singleton
Create with factory startup = "construction method = =" set method (input value) = = "init (initialization) = =" build complete = = "destroy with factory shutdown
Multiple cases of bean:prototype
Create when used = "Construction method = =" set method (input value) = = "init (initialization) = =" build complete = = "JVM garbage collection destruction
5. Use cases
User entity class
Package com.cos.qf.entity; import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import java.util.*; public class User {private Integer id; private String password; private String sex; private Integer age; private Date bornDate; private String [] hobbys; private Set phones; private List names; private Map countries; private Properties files @ PostConstruct / / initialize public void init () {System.out.println ("initialized");} @ PreDestroy / / destroy public void destroy () {System.out.println ("destroyed");} public User () {System.out.println ("No parameter construction performed") } public void setId (Integer id) {System.out.println ("set-id"); this.id = id;} / / get and set method}
Application-config.xml profile
Test method: do not use the parent class (ApplicationContext) of ClassPathXmlApplicationContext to turn it off or you will report an error
@ Test public void text4 () {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext ("application-config.xml"); ctx.close ();} above is all the content of the article "what are the characteristics of factory schemas in Spring?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.