Java Spring在方法调用期间获取托管bean

Java Spring在方法调用期间获取托管bean,java,spring,instantiation,Java,Spring,Instantiation,我是春天的新手,我有一个问题,我找不到答案- 在方法执行期间,我需要以预定义的数量创建托管bean scope==prototype的新实例: @Component class SomeClass { @Resource private ConnectionFactory conFactory; private Set <Client> clients; @Value ("${clientsNum}") private int c

我是春天的新手,我有一个问题,我找不到答案- 在方法执行期间,我需要以预定义的数量创建托管bean scope==prototype的新实例:

@Component
class SomeClass
{
     @Resource
     private ConnectionFactory conFactory;

     private Set <Client> clients;

     @Value ("${clientsNum}")
     private int clientsNum; 

     public void init ()
     {
         Client client = null;    //an interface, that the managed bean implements.

         for (int i = 0; i < clientsNum; i++)
         {
               client = ... //how to get a new client instance? 
               clients.add (client);
               client.doSomething ();
         }
     }

     public void shutdown ()
     {
         for (Client client : clients)
               client.shutdown ();

         conFactory.shutdown ();
     }
 }
我该怎么做

我知道我可以使用init方法\@PostConstruct注释和匹配的destroy方法,但我不知道如何根据所需数量获取实例

在过去的几天里,我搜索了这个问题,并阅读了关于服务定位器、查找方法和工厂bean的内容。它们都使用我不喜欢使用的CGLIB或spring的ApplicationContext,后者在spring的实现中创建了一个依赖项,最重要的是——它们在方法调用期间不处理bean的创建,或者至少——我不知道如何在调用期间使用它们

请帮忙

谢谢

编辑:


最终我意识到我必须使用CGLIB或Spring的应用程序上下文,我选择了使用该选项。

我想某种类型的工厂对您来说是更好的选择,您可以定义一个工厂方法,该方法将ClientsUM作为参数并向您返回一个集合。通过这种方式,您可以对服务类隐藏所有详细信息。 如果您仍然想使用依赖项注入来实现这一点,您可以尝试方法注入。看一下下面的代码来了解一下

@Component
class SomeClass
{
 @Resource
 private ConnectionFactory conFactory;
 protected abstract Client createClient();

 private Set <Client> clients;

 @Value ("${clientsNum}")
 private int clientsNum; 

 public void init ()
 {
     Client client = null;    //an interface, that the managed bean implements.

     for (int i = 0; i < clientsNum; i++)
     {
           client = createClient();
           clients.add (client);
           client.doSomething ();
     }
 }

 public void shutdown ()
 {
     for (Client client : clients)
           client.shutdown ();

     conFactory.shutdown ();
 }
}

您可以通过简单的google搜索找到方法注入的完整示例。但是我建议使用第一种解决方案,因为它更干净。

我担心您必须使用CGLIB或ApplicationContext,因为您需要一个托管bean

查找方法注入 向您的SomeClass中添加:

然后在init中,您可以将其称为:

client = createClient();
注意,Spring 4.1.0中引入了注释。如果使用的是旧版本,则需要XML配置

使用ApplicationContext 声明自动连接属性:

@Autowired
private ApplicationContext applicationContext;
在init中:


我不知道如何在不使用注入的情况下获得托管bean的新实例,而是在方法调用期间

注入Spring ApplicationContext,然后使用ApplicationContext.getBean


你想解决的问题是什么我认为你找不到合理答案的原因是,你想用一种非春天的方式解决问题因此,我提出了这个问题。我不知道如何在不使用注入的情况下获得托管bean的新实例,而是在方法调用期间。谢谢。我意识到我必须使用CGLIB或Spring的应用程序上下文。最后,我决定使用服务定位器。
@Autowired
private ApplicationContext applicationContext;
client = applicationContext.getBean(Client.class);
 @Autowired
 private ApplicationContext applicationContext;

 public void init ()
 {
     Client client = null;    //an interface, that the managed bean implements.
     for (int i = 0; i < clientsNum; i++)
     {
/*>>>>*/   client = applicationContext.getBean(Client.class); 
           clients.add (client);
           client.doSomething ();
     }
 }