在Spring中自动注入所有依赖项

时间:2014-08-14 11:56:05

标签: java spring dependency-injection autowired

我正在尝试将Spring Dependency Injection实现到我的桌面应用程序中(我只是尝试注入我的服务)。

显然我并不真正理解它是如何工作的,尽管所有的教程(尽管它们中的大多数都是针对Web应用程序)。所以当前的状态是我在我的pom.xml中添加了一个依赖项来添加spring jar,它可以正常工作。我还设法扫描我的服务。应用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>

我很确定我必须告诉Spring我的application-Context.xml在哪里。因此,我在指南中找到了一个装载程序类:

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);
}
}

不幸的是,这个加载器只加载给定对象的依赖项。但是我不想为我拥有的每个Panel手动加载依赖项。所以最后一个问题是:如何告诉Spring我的applicationContext是什么,并自动注入所有依赖项,而不必为每个对象手动加载它?

1 个答案:

答案 0 :(得分:1)

您无需经历已设置的ApplicationContextLoader障碍。 Spring提供了简单的引导任何应用程序的方法,包括桌面应用程序。

请执行以下操作:

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
  }

}

上面的代码来自this博文,如果application-Context.xml位于类路径的根目录下(例如正好位于maven项目的resources目录下),则可以使用。

使用上面的代码,Spring将自动创建并连接已指定的所有bean

请注意,上面的代码是老式的Spring做事方式。 Spring Boot提供了一种更愉快的入门方式。 Here是一个示例,here是另一个