Inject all dependencies automatically in Spring Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Inject all dependencies automatically in Spring without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m currently trying to implement Spring Dependency Injection into my Desktop Application (I am only trying to inject my services).

Apparently I dont really understand how it works, despite all the tutorials (most of them address web applications though). So the current state is that I have added a dependency in my pom.xml to add the spring jars, which works properly. I also managed to scan for my services. application-Context.xml:

<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
http://www.springframework.org/schema/data/jpa 
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">

<context:annotation-config />
<context:component-scan base-package="FireworksApp" />

</beans>

I am pretty sure that I have to tell Spring where my application-Context.xml is. Therefore I have found a loader class in a guide:

public class ApplicationContextLoader {

protected ConfigurableApplicationContext applicationContext;

public ConfigurableApplicationContext getApplicationContext() {
    return applicationContext;
}

/**
 * Loads application context. Override this method to change how the
 * application context is loaded.
 * 
 * @param configLocations
 *            configuration file locations
 */
protected void loadApplicationContext(String... configLocations) {
    applicationContext = new ClassPathXmlApplicationContext(
            configLocations);
    applicationContext.registerShutdownHook();
}

/**
 * Injects dependencies into the object. Override this method if you need
 * full control over how dependencies are injected.
 * 
 * @param main
 *            object to inject dependencies into
 */
protected void injectDependencies(Object main) {
    getApplicationContext().getBeanFactory().autowireBeanProperties(
            main, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
}

/**
 * Loads application context, then injects dependencies into the object.
 * 
 * @param main
 *            object to inject dependencies into
 * @param configLocations
 *            configuration file locations
 */
public void load(Object main, String... configLocations) {
    loadApplicationContext(configLocations);
    injectDependencies(main);
}
}

Unfortunately this loader only loads dependencies for a given object. But I don’t want to have to manually load dependencies for every Panel I have. So the final question is: How can I tell Spring where my applicationContext is and to inject all dependencies automatically without having to load it manually for every object?

Answer

There is no need to go through the ApplicationContextLoader hurdle that you have setup. Spring provides easy means of bootstrapping any application, including a desktop one.

Just do the following:

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class SpringApplicationContextExample
{

  public static void main (String[] args)
  {
    new Main();
  }

  public SpringApplicationContextExample()
  {
    // open/read the application context file
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application-Context.xml");

    // retrieve a bean for class SomeSpringService
    SomeSpringService someSpringService = (SomeSpringService)ctx.getBean("someSpringService");

    //do whatever with the bean
  }

}

The code above is taken from this blog post and works if application-Context.xml is at the root of the classpath (for example right under the resources directory of maven project).

Using the code above, Spring will automatically create and wire all the beans that have been specified

Note that the code above is sort of the old-school Spring way of doing things. Spring Boot provided a much more pleasant way of getting started. Here is an example and here is another

We are here to answer your question about Inject all dependencies automatically in Spring - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji