I\'m using Spring 3.0.x and following the enum singleton pattern for one of my implementatons.
public enum Person implements Nameable {
INSTANCE;
pu
If you really need to use enum-based singleton (despite the fact that Spring beans are singletons by default), you need to use some other way to register that bean in the Spring context. For example, you can use XML configuration:
<util:constant static-field="...Person.INSTANCE"/>
or implement a FactoryBean
:
@Component
public class PersonFactory implements FactoryBean<Person> {
public Person getObject() throws Exception {
return Person.INSTANCE;
}
public Class<?> getObjectType() {
return Person.class;
}
public boolean isSingleton() {
return true;
}
}
You won't need to use the enum singleton pattern if you're using Spring to manage dependency injection. You can change your Person to a normal class. Spring will use the default scope of singleton, so all Spring-injected objects will get the same instance.