aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring Controllers singleton or not?" Watch "Spring Controllers singleton or not?" New topic
Author

Spring Controllers singleton or not?

Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 112
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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Still a singleton, and proud of it. ;)

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
kunalkumar somani
Greenhorn

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">
</bean>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Spring Controllers singleton or not?