Java 以编程方式检索 Bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25775888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:06:39  来源:igfitidea点击:

retrieve Bean programmatically

javaspringjavabeans

提问by user800799

@Configuration
public class MyConfig {
    @Bean(name = "myObj")
    public MyObj getMyObj() {
        return new MyObj();
    }
}

I have this MyConfig object with @Configuration Spring annotation. My question is that how I can retrieve the bean programmatically (in a regular class)?

我有这个带有 @Configuration Spring 注释的 MyConfig 对象。我的问题是如何以编程方式(在常规课程中)检索 bean?

for example, the code snippet looks like this. Thanks in advance.

例如,代码片段看起来像这样。提前致谢。

public class Foo {
    public Foo(){
    // get MyObj bean here
    }
}

public class Var {
    public void varMethod(){
            Foo foo = new Foo();
    }
}

采纳答案by Xstian

Here an Example

这里有一个例子

public class MyFancyBean implements ApplicationContextAware {

  private ApplicationContext applicationContext;

  void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
  }

  public void businessMethod() {
    //use applicationContext somehow
  }

}

However you rarely need to access ApplicationContext directly. Typically you start it once and let beans populate themselves automatically.

但是,您很少需要直接访问 ApplicationContext。通常,您启动它一次,然后让 bean 自动填充自己。

Here you go:

干得好:

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

Note that you don't have to mention files already included in applicationContext.xml. Now you can simply fetch one bean by name or type:

请注意,您不必提及已包含在 applicationContext.xml 中的文件。现在您可以简单地按名称或类型获取一个 bean:

ctx.getBean("someName")

Note that there are tons of ways to start Spring - using ContextLoaderListener, @Configuration class, etc.

请注意,有很多方法可以启动 Spring - 使用 ContextLoaderListener、@Configuration 类等。

回答by Jim Garrison

The simplest (though not the cleanest) approach if you really need to fetch beans from the ApplicationContextis to have your class implement the ApplicationContextAwareinterface and provide the setApplicationContext()method.

如果您真的需要从 中获取 bean,最简单(虽然不是最干净)的方法ApplicationContext是让您的类实现ApplicationContextAware接口并提供setApplicationContext()方法。

Once you have a reference to the ApplicationContextyou have access to many methods that will return bean instances to you.

一旦您获得了对 的引用,ApplicationContext您就可以访问许多将向您返回 bean 实例的方法。

The downside is that this makes your class aware of the Spring context, which one should avoid unless necessary.

不利的一面是,这会让您的班级了解 Spring 上下文,除非必要,否则应避免使用。

回答by Grim

Try this:

尝试这个:

public class Foo {
    public Foo(ApplicationContext context){
        context.getBean("myObj")
    }
}

public class Var {
    @Autowired
    ApplicationContext context;
    public void varMethod(){
            Foo foo = new Foo(context);
    }
}