How to use singleton and prototype of spring
Today, the editor will share with you the relevant knowledge points about how to use spring singleton and prototype. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
One singleton
Singleton is the singleton mode, that is, the bean of scope= "singleton", which is instantiated only once in the container.
Dao sample code:
Package com.demo.dao;public class UserDao {public UserDao () {System.out.println ("UserDao no-parameter constructor called");} / get user name public String getUserName () {/ / simulate dao layer return "Alan_beijing";}}
ApplicationContext.xml
Test:
Public class MyTest {@ Test public void test () {/ / define the container and initialize ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("applicationContext.xml"); / / define the first object UserDao userDao = applicationContext.getBean (UserDao.class); System.out.println (userDao.getUserName ()); / / define the second object UserDao userDao2 = (UserDao) applicationContext.getBean ("userDao") System.out.println (userDao2.getUserName ()); / / compare whether two object instances are the same object instance System.out.println ("first instance:" + userDao+ "\ n" + "second instance:" + userDao2);}}
Analysis: in the test code, bean is defined as singleton, and the bean (userDao) is obtained twice through the getBean () method of ApplicationContext, but the same instance object is returned: com.demo.dao.UserDao@27a5f880. Careful observation shows that although bean is obtained twice, the no-parameter constructor of UserDao is only called once, which also proves that singleton is actually instantiated only once in the container. It should be noted that when the bean,ApplicationContext of Singleton mode loads bean. Bean is instantiated.
Two prototype
Prototype is the prototype pattern, and bean is instantiated as many times as it is called.
Change the singleton code to a prototype
The test code is the same as singleton, but the result is different
Analysis: through the test results, it is not difficult to find that when bean is called twice, the UserDao object is instantiated twice, and the object is different. It should be noted that the bean of prototype type will instantiate the object only when bean is obtained.
Differences between singleton and prototype
(1) singleton is instantiated only once in the container, while prototype is instantiated several times after several calls in the container.
(2) in the AppplicationContext container, singleton is pre-instantiated when the applicaitonContext.xml is loaded, while prototype must be instantiated only when called.
These are all the contents of the article "how to use spring's singleton and prototype". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.