Search code examples
javaspring-bootspring-framework-beans

Spring java config to invoke a public non static method of a singleton class with private constructor


I am working on changing a spring project to springboot and changing to java config from xml. I have a singleton class with a private constructor, in the original xml the non static methods are invoked as such:

<bean id="myClassBeanId" class="com.myproject.dao.MyClass">

<bean id="daoservice" factory-bean="myClassBeanId" factory-method="createMyStaticVariable">

The objective is to somehow invoke the "public non static method" of a class with a private constructor to initialize the static variables of the same class in spring java config. Could someone please tell the java config equivalent of the above xml config.


Solution

  • @Bean
    public MyClass myClass() {
    MyClass.class.getConstructror().makeAccessable () ;
    return MyClass.class.getConstructror().invoke() :
    } 
    @Bean
    public DaoService daoService(MyClass myClassBeanId) {
    return myClassBeanId.createMyStaticVariable():
    }