Jazzepi
Jazzepi

Reputation: 5480

How do I autowire an object from code I depend on?

I have a class as such.

public class Foo {

  @Autowired
  private Bar bar;

}

The class Bar lives in a jar that comes from a dependency pulled in from Maven. How can I add that class to the Spring context so that it will be properly autowired? I can't add the @Component to the source code. Is there a way to do it without XML? I'm currently trying to stick with annotation driven injection as much as possible.

I'm using Spring 3.1.4-RELEASE

Upvotes: 0

Views: 91

Answers (1)

It appears from your description that you know exactly which class is being instantiated. If that's the case, then in one of your @Configuration classes, you can simply create the appropriate bean, and Spring will configure it before deployment into the context:

@Bean public Foo foo() {
    return new Foo();
}

Upvotes: 2

Related Questions