1

When I create my ApplicationContext the myBean Constructors are used successfully. But after creation the beans are null using @Autowired tag.

I though @Autowired would replace getBean() somehow? Am I getting this wrong?

Why do I need to call GetBean, when I already created my Beans (during ApplicationContext startup) and also Autowired them?

Here is what I have done so far:

Main:

@EnableAutoConfiguration
@Configuration
@ComponentScan("com.somePackage")
public class Main {
    ApplicationContext ctx= new AnnotationConfigApplicationContext(AppConfig.class);

    SomeBean myBean = ctx.getBean(SomeBean.class);
    myBean.doSomething();//succeeds

    AnyOtherClass aoc = new AnyOtherClass();//Nullpointer (see AnyOtherClass.class for details)

}

AnyOtherClass:

public class AnyOtherClass {
    @Autowired
    protected SomeBean someBean;

    public AnyOtherClass(){
        someBean.doSomething();//Nullpointer
    }

AppConfig:

@Configuration
public class AppConfig {
    @Bean
    @Autowired
    public SomeBean BeanSomeBean() {
        return new SomeBean();
    }
}

MyBean:

public class SomeBean {
    public SomeBean (){}
    public void doSomething() {
        System.out.println("do Something..");
    }
}

Note: Interface ApplicationContextAware works fine but without @Autowired. And I would really like to go with @Autowired since it sounds more comfortable and less errorprone to me.

Haphil
  • 1,180
  • 1
  • 14
  • 33

2 Answers2

2

For @Autowired to work in AnyOtherClass, AnyOtherClass needs to be a Spring bean.
Something like this

AppConfig

@Configuration
public class AppConfig {
    @Bean
    @Autowired
    public SomeBean BeanSomeBean() {
        return new SomeBean();
    }

    @Bean
    @Autowired
    public AnyOtherClass BeanAnyOtherClass() {
        return new AnyOtherClass();
    }
}

Main

@EnableAutoConfiguration
@Configuration
@ComponentScan("com.somePackage")
public class Main {
    ApplicationContext ctx= new AnnotationConfigApplicationContext(AppConfig.class);

    SomeBean myBean = ctx.getBean(SomeBean.class);
    myBean.doSomething();//succeeds

    AnyOtherClass aoc = ctx.getBean(AnyOtherClass.class);

}

If you instantiate a class using new AnyOtherClass(), it will bypass Spring and none of the annotations will work. You must get it out of Spring context in order for it to be a Spring bean.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • I declear it as a Bean in AppConfig, which I load in Main.class. Or do you mean to define it as a Bean in both places (AppConfig & AnyOtherClass)? – Haphil Dec 09 '14 at 12:46
  • @Hansa Just to be clear on what I meant, I'm not talking about `SomeBean` class, but the class which injects it using `@Autowired`. So, `SomeBean` is already a Spring bean (declared in `AppConfig`), but `AnyOtherClass` also needs to be a Spring bean (declared in `AppConfig`, or annotated with `@Component`, located in a package which is scanned (`com.somePackage`)) – Predrag Maric Dec 09 '14 at 12:51
  • Hmm that would mean for me I add `@autowired static AnyOtherClass aoc` to Main. (`static` because its in main..) Then I get: BeanCreationException->BeanInstantiationException-> nested exception is java.lang.NullPointerException – Haphil Dec 09 '14 at 13:00
  • @Hansa I don't think you understood me, check my updated answer. – Predrag Maric Dec 09 '14 at 13:10
  • so `@Autowired` helps me to find Beans in Beans, but the first bean I have to get with getBean()? --> therefore @Autowired does not replace the getBean() fully?! I am a bit confused because, during Creating the ApplicationContext the beans are already created. And then I have to get them via getBean() again although I Autowired them... It seems like I expected to much from Autowired – Haphil Dec 09 '14 at 13:48
  • 1
    @Hansa Depends on the kind of application you are building (for example, you won't need this in web app), but yes, if you need a bean in a class which is not a bean, you need to get it from context using `getBean()`. – Predrag Maric Dec 09 '14 at 14:29
  • ok thx for your answer as well as your patience – Haphil Dec 09 '14 at 15:09
  • @Hansa NP, glad I could help. – Predrag Maric Dec 09 '14 at 15:10
0

Try

@Component
public class AnyOtherClass {
@Autowired
protected SomeBean someBean;

public AnyOtherClass(){
    someBean.doSomething();//Nullpointer
}

Besides, you need to make sure this class is in com.somePackage package.