Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring Controllers singleton or not?

 
Fawad Ali
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I know that spring beans are singleton by default. I want to know whether Spring controllers are singleton or a new instance is created for each user?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still a singleton, and proud of it. ;)

Mark
 
kunalkumar somani
Greenhorn
Posts: 7
Eclipse IDE Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fawad Ali,

By default it always singleton.

There are many ways to check your bean is singleton or not. For your confirmation if you want to check just create a Standalone application and load your configuration file from BeanFactory or ApplicationContext thn get your bean more thn one time using getBean() method.

And finally print object and check the address of that objects.It should be same. For example you can see below code in main method....


Resource resource = new FileSystemResource("com/spring/example/spring.cfg.xml");
BeanFactory factory = new XmlBeanFactory(resource);

Singleton singleton = (Singleton) factory.getBean("singleton");

Singleton singleton1 = (Singleton) factory.getBean("singleton");

System.out.println("Address 1=>"+singleton);
System.out.println("Address 2=>"+singleton1);

xml file has...

<bean id="singleton" class="com.example.Singleton">
</bean>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic