<> Singleton mode
Characteristics of singleton model :
A singleton class can only have one instance
Singleton classes must create their own unique instances
Singleton classes must provide this object instance to all other objects
<> Slouch style
Lazy singletons are initialized on the first call
Lazy singletons are thread unsafe , In the case of concurrency , There may be more than one Singleton example
To achieve thread safety, you need to getInstance() Carry out transformation , To ensure thread safety .
<> A single example of starving Han style
A single example of starving Han style :
The starving singleton is instantiated at the beginning of the class
The difference between the hungry and the lazy
By name , The hungry and the lazy ,
A hungry man is a class once loaded , Just initialize the singleton , ensure getInstance When I was young , The singleton already exists ,
Lazy people are lazy , Only when called getInstance When I was young , Just go back to initialize this singleton .
In addition, the following two ways can be distinguished from each other ：
1, Thread safety ：
Starvation is inherently thread safe , Can be directly used for multithreading without problems ,
Laziness itself is not thread safe , There are several ways to write for thread safety , They are above 1,2,3, These three implementations differ in resource loading and performance .
2, Resource loading and performance ：
When creating a class, we instantiate a static object , Whether or not this singleton will be used later , Will occupy a certain amount of memory , But correspondingly , It's also faster on the first call , Because its resource has been initialized ,
And the sluggard style, as the name suggests , Load will be delayed , The object is instantiated only when the singleton is used for the first time , Initialization should be done on the first call , If there is more work to be done , There will be some delay in performance , After that, it's the same as the hungry man style .
as for 1,2,3 There are some differences among the three implementations ,
The third 1 species , Method calls are synchronized , Although the thread is safe , But synchronize every time , Can affect performance , after all 99% In this case, synchronization is not required ,
The third 2 species , stay getInstance I did it twice null inspect , It ensures that synchronization is only done when the singleton is called for the first time , This is also thread safe , At the same time, the performance loss of every synchronization is avoided
The third 3 species , Took advantage of classloader To ensure initialization instance There is only one thread when running , So it's also thread safe , At the same time, there is no performance loss .
<> build Spring MVC+Spring+JDBC frame
The previous chapters have been mastered Spigvc Start with this section ,
Let's transform the control layer of supermarket order management system (orolel Part of the code implementation , Combined with the source code of the materials provided and the software we have built SinovCc frame , Complete the transformation of the project framework , Finally, it was transformed into JSP+Spring
Performance based Considerations in the future ,Spring MVC+Spring+JDBC The framework is also used more frequently in some Internet projects
high , Therefore, this chapter will be in the original material of this book (SMBMS) On the basis of , use Spring
MVC Research on the framework transformation of supermarket order management system Contoller layer ,DAO Layer temporarily used JDBC realization . On page 12 chapter SSM Framework integration , Let's reinvent it DAO Layer is MyBatis The realization of , Complete the three frameworks (Spring
MWtSpingnMyBai) The integration of .
1. join Spring. Soring MNC. Database driver, etc jar file .
2.Spring configuration file
3. to configure web.xml file