views:

2817

answers:

5

I want To write a standalone application with IOC,How do i use springs dependancy injection in there. Im using JIdea. There is spring 2.5 support but i want to use spring 3.0 here is the way i tried!

I experience in using Spring MVC we can inject dependancies there in a WebApplicationContext but how do i inject dependancies in a standalone application

I tried this

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"com\\ttg\\xmlfile.xml"});

but i cannot see that the dependancies are injected by the beans defined there (in the xml file) I put the above code in the main method and two bean definitions for two Objects,in one java class's constructor i used the other class's object - which was injected to this object - and called a mehtod on that which will print some thing but it didnt worked i thought that the above code creates all the dependencies and injects them but it doesnt seem like that

How do i properly use Springs IOC, dependancy injection in my stand alone app which does not contain a WebApplicationContext?

Please mention steps

A: 

Are you calling context.getBean("beanName") to get a reference to the bean or are you doing a new SomeClass()? If you do it through getBean() then the injected properties should be set.

Also, be sure to use the same bean factory (ClassPathXmlApplicationContext instance) for retrieving all your beans. This should most likely be static final and used by the entire application.

Taylor Leese
+1  A: 

How did you confirm that your beans aren't being wired correctly? One common issue is the xml config file not being in the right place. Can you give us some more information like your project layout and the code you use to obtain the beans from the container?

neesh
+8  A: 

suppose you have:

class Bean1 {
  Bean2 bean2;
}

class Bean2 {
  String data;
}

the context.xml file

<bean id="bean1" class="Bean1">
  <property name="bean2" ref="bean2" />
</bean>

<bean id="bean2" class="Bean2" />

then this should be true

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"context.xml"});
Bean1 bean1 = (Bean1) context.getBean("bean1");

// bean1.bean2 should not be null here.
Toader Mihai Claudiu
A: 

If you add log4j logging to your app, you should see a cascade of messages coming out that will tell you a lot about what Spring is and is not doing. If you don't have that feedback, you're in the dark. You might be able to ferret out the answer just by getting more information out of Spring from log4j.

duffymo
A: 

you can use autowiring support provided by spring, in order to inject dependencies (and possibly apply post processors) to an object that is not part of the application context.

In your case, this object is your standalone application.

Here is the way to achieve this. In this example, I use @Autowired (for b1), traditional DI (for b2) and initialization hook for b3. The autowiring support with annotations assumes you have defined the appropriate spring post-processor in your application context (e.g. by declaring <context:annotation-config/>).

public class StandaloneApp implements InitializingBean {
  @Autowired private Bean1 b1;
  private Bean2 b2;
  private Bean3 b3;

  public void afterPropertiesSet() throws Exception {
    this.b3 = new Bean3(b1, b2);
  }

  public void setB2(Bean2 b2) {
    this.b2 = b2;
  }

  public static void main(String... args) {
    String[] locations = // your files relative to the classpath
    ApplicationContext ac = new ClasspathXmlApplicationContext(locations);
    // or use FileSystemXmlApplicationContext if the files are not in the classpath

    StandaloneApp app = new StandaloneApp();
    AutowireCapableBeanFactory acbf = ac.getAutowireCapableBeanFactory();
    acbf.autowireBeanProperties(app, AUTOWIRE_BY_NAME, false);
    acbf.initializeBean(app, "standaloneApp"); // any name will work
  }
}

In this example, all b1, b2 and b3 should be non-null (assuming b1 and b2 beans exist in your application context).

I haven't tested it (might not even compile due to some typo), but the idea is in the last 3 lines. See the javadocs for AutowireCapableBeanFactory and mentionned methods to see exactly what happens.

Gaetan