File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes Spring Controllers singleton or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring Controllers singleton or not?" Watch "Spring Controllers singleton or not?" New topic

Spring Controllers singleton or not?

Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 115
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?

Regards, Fawad Ali.
Software Engineer, Stafona Inc. - My Blog
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Still a singleton, and proud of it. ;)


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
kunalkumar somani

Joined: Jul 13, 2011
Posts: 7

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">
I agree. Here's the link:
subject: Spring Controllers singleton or not?
It's not a secret anymore!