【问题标题】:Inject all dependencies automatically in Spring在 Spring 中自动注入所有依赖项
【发布时间】:2014-08-14 11:56:05
【问题描述】:

我目前正在尝试在我的桌面应用程序中实现 Spring 依赖注入(我只是尝试注入我的服务)。

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

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

【问题讨论】:

    标签: java spring dependency-injection autowired


    【解决方案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 是另一个例子

    【讨论】:

    • tbh 我不喜欢老式的春季方式。访问 data-jpa 似乎并没有真正满足我的要求。大约 4 小时后,我仍然没有让 githubg 示例运行... Idk,我可能做错了什么,但我做的和所有示例中的完全一样。
    • 你是从 github 上查看代码还是在复制它?如果你在后面做,你可能错过了一些东西
    • 好吧,我得到了这个例子。仍然无法在我的项目中工作。我的 Main 类有 Configuration、EnableAutoConfiguration、ComponentScan Annotations,而我的 Service 类有 Component。我的 pom.xml 除了名称之外与示例 pom.xml 相同...
    • 哈哈:X 我已经监督使用 springapplication.run() 运行它。它仍然无法正常工作。如果我的主类不在我想要自动装配的类的包中,它会说:没有为依赖项找到类型为 [FireworksApp.Service.ProjectService] 的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。如果它在服务包中,则服务将自动使用 null。
    • 我想我已经明白了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多