简体   繁体   English

如何在 Spring boot 中使用构造函数注入为不同的属性值创建相同的 Bean

[英]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.我必须为要使用构造函数注入的不同属性值创建多个相同类型的 bean。

Currently I have used Bean scope as Prototype & created multiple methods to read different properties to create new object.目前我已经使用 Bean 范围作为原型并创建了多种方法来读取不同的属性来创建新对象。 How to combine all the different methods into single method and to supply different values at run time to create new bean.如何将所有不同的方法组合成一个方法,并在运行时提供不同的值来创建新的 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.现在它按照上面的代码创建了 2 个 bean。 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期望:如何将 getClient 和 getOClient 组合成单个方法,但要在循环中提供属性以创建多个相同类型的 JavaClient bean

I have modified my ConfigClass as below & created ApplicationContextAware to inject beans by reading file properties.我修改了我的 ConfigClass 如下并创建了 ApplicationContextAware 以通过读取文件属性来注入 bean。

    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.然后创建了 ApplicationContextAware 来创建 Bean。

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();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM