20
@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)?

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();
    }
}
user800799
  • 2,883
  • 7
  • 31
  • 36
  • Try `@Autowire`ing... or more precisely `@Qualifier("myObj")`. – Mr. Polywhirl Sep 10 '14 at 22:10
  • I can not do @Autowire because I have to create Foo object using new in runtime – user800799 Sep 10 '14 at 22:11
  • Check out the [Qualifier annotation](http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers). – Mr. Polywhirl Sep 10 '14 at 22:12
  • Related: [Spring injecting or autowiring datasource bean to class](http://stackoverflow.com/q/17819752/1065197). Check the part in the bottom of my answer. – Luiggi Mendoza Sep 10 '14 at 22:20
  • Do you have it setup in your Spring context file? – Dan Sep 10 '14 at 22:57
  • If you need to create a Foo using new, then you could use the @Lookup annotation. `@Lookup public Foo getFoo() { return null; }` --> the Lookup annotation means Spring will handle instantiating and Autowiring Foo in this situation. – Rich Sep 08 '22 at 09:18

4 Answers4

14

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);
    }
}
Grim
  • 1,938
  • 10
  • 56
  • 123
9

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.

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:

ctx.getBean("someName")

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

Xstian
  • 8,184
  • 10
  • 42
  • 72
5

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

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

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

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
2

This can be a useful sample:

@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;

    public static ApplicationContext getContext() {
        return context;
    }

    public static <T> T getBean(Class<T> bean) {
        return getContext().getBean(bean);
    }

    public static <T> T getBean(String beanName, Class<T> bean) {
        return getContext().getBean(beanName, bean);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
}

Then you can use as follow:

MeyComplexBean myBean = ApplicationContextUtil.getBean(MeyComplexBean.class);
rodrix
  • 470
  • 6
  • 17