1

I have an Enum :

public enum MyEnum {

    INSTANCE;

    @Autowired
    Regroupements regroupements;

    @PostConstruct
    public void initi()
    {
        System.out.println("---------- i am not called!");
    }

    private MyEnum() {
        System.out.println("---------- i am called!");
    }

}

And a Spring-Factory

@Component
public class MyEnumFactory implements FactoryBean<MyEnum>{

    @Override
    public MyEnum getObject() throws Exception {
        return MyEnum.INSTANCE;
    }

    @Override
    public Class<?> getObjectType() {
        return MyEnum.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

}

The problem is : when in call the method of the factory to give me the unique INSTANCE, the init() method is never called! you'll notice that, as it is an Enum, the class don't have @Component annotation. it's why I use a factory.

what I want is to launch a post-construct, it doesn't matter the way.

sForSujit
  • 987
  • 1
  • 10
  • 24
electrode
  • 205
  • 1
  • 4
  • 16
  • check this https://stackoverflow.com/questions/710392/using-spring-ioc-to-set-up-enum-values – Sławomir Czaja Aug 02 '17 at 09:17
  • The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization, But in your case you are using ENUM (MyEnum ) this cannot be instantiated .so PostConstruct will not work @electrode – AssenKhan Aug 02 '17 at 09:19

1 Answers1

0

You can't use enum as bean (and normally you don't have to), so you can neither use @PostConstruct on it, nor even autowire anything inside: your dependency Regroupements regroupements; is going to be null.

Dmytro Titov
  • 2,802
  • 6
  • 38
  • 60
  • (1) i dont't really understand, but i want to learn more :-). In fact, i wanna use the pattern singleton via ENUM method which is the best practice,a according to many confirmed developpers. isn't it correct ? – electrode Aug 02 '17 at 16:29
  • (2) secondly, to instanciate my type Regroupements (my dependance Regroupements regroupements;), do i have to do it via applicationContext.getBean method ? is it correct ? – electrode Aug 02 '17 at 16:29
  • 1
    @electrode (1) Spring beans are already(by default) singletons, if this is the reason you are using Enums you absolutely don't need them. (2) Depends on what you want to do. I suggest you learn a bit about Spring [you can start here](https://www.tutorialspoint.com/spring/) and come back with better questions. – Oleg Aug 03 '17 at 01:02
  • ok @Oleg you'right. it is a fact that i didn't really undestand scope notions of Spring. it is more clear. if you have better documentations regarding scopes and conflicts between them, it would be nice.. – electrode Aug 03 '17 at 09:55
  • @electrode The link I gave you earlier has a section about scopes. If you want more details you can look at the [spring reference](https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-scopes) (The only scopes of interest are singleton and prototype other scopes are only relevant with SprigMVC). What do you mean by "conflicts between them"? Bean Scopes don't have conflicts. – Oleg Aug 03 '17 at 10:14
  • Guys, I believe it's not the place for such discussion: any Spring-related materials can be easily found on Web. The original question is already answered. – Dmytro Titov Aug 03 '17 at 15:58