Search code examples
springspring-bootschedulerautowiredconstructor-injection

How to create same Bean using Constructor Injection in Spring boot for different property values


I have to create multiple beans of same type for different property value which is to be injected using constructor.

Currently I have used Bean scope as Prototype & created multiple methods to read different properties to create new object. How to combine all the different methods into single method and to supply different values at run time to create new bean.

    package com.demo.service;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.Environment;

    @Configuration
    public class ConfigClass {


@Bean(name="RC")
public JavaClient getClient(@Autowired Environment env)
{
    String sName=env.getProperty("RCSName");
    String aUrl=env.getProperty("RCAUrl");
    String dUrl=env.getProperty("RCDUrl");
    return new JavaClient(sName,aUrl,dUrl);
}

@Bean(name="O")
public JavaClient getOClient(@Autowired Environment env)
{
    String sName=env.getProperty("OSName");
    String aUrl=env.getProperty("OAUrl");
    String dUrl=env.getProperty("ODUrl");
    return new JavaClient(sName,aUrl,dUrl);
}

}

Now it is creating 2 beans as per above code. Expectation: How to combine getClient & getOClient into single method, but the property to be supplied in a loop to create multiple beans of same type JavaClient


Solution

  • I have modified my ConfigClass as below & created ApplicationContextAware to inject beans by reading file properties.

        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configuration;
        import org.springframework.context.annotation.Scope;
        import org.springframework.scheduling.annotation.EnableScheduling;
    
    
        @Configuration
        @EnableScheduling
        public class ConfigClass {
    
            @Bean
            @Scope("prototype")
            public JavaClient getClient(String sName,String aUrl,String dUrl)
            {
                return new JavaClient(sName,aUrl,dUrl);
            }
        }
    

    Then Have created ApplicationContextAware to create Beans.

    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.util.Iterator;
    import java.util.List;
    
    import javax.annotation.PostConstruct;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import org.springframework.stereotype.Component;
    
    
    
    @Component
    public class ApplicationContextProvider implements ApplicationContextAware {
    
        private ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
    
        } 
    
        public ApplicationContext getContext() {
            return applicationContext;
        }
    
        @PostConstruct
        public void init()
        {
            try {
                String fileName = "Url.txt";
                   Resource resource = new ClassPathResource(fileName);
                   File file = resource.getFile();
    
                   List<String> lines = Files.readAllLines(file.toPath());
    
                    for (Iterator<String> iterator = lines.iterator(); iterator.hasNext();) {
                        String line = (String) iterator.next();
                        String[] s = line.split(",");
                        applicationContext.getBean(JavaClient.class,s[0], s[1], s[2]);
    
                    }
                } catch (IOException e) {
                    e.printStackTrace();
            }
        }
    
    }