簡體   English   中英

Spring-如何使用Spring Dependency Injection編寫獨立的Java應用程序

[英]Spring- How to use Spring Dependency Injection to write a Standalone Java Application

我想用IOC編寫一個獨立的應用程序,如何在其中使用springs依賴注入? 我正在使用JIdea。 有spring 2.5支持,但是我想在這里使用spring 3.0!

我在使用Spring MVC方面經驗豐富,可以在WebApplicationContext中注入依賴項,但是如何在獨立應用程序中注入依賴項

我試過了

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

但是我看不到依賴關系是由在那里(在XML文件中)定義的bean注入的,我將上面的代碼放在main方法中,並將兩個bean定義用於兩個Object,在一個Java類的構造函數中,我使用了另一個類的對象-被注入到該對象-並在該方法上調用了一個方法,該方法將打印出一些東西,但沒有奏效,我以為上面的代碼創建了所有依賴項並將其注入,但看起來不像那樣

如何在不包含WebApplicationContext的獨立應用程序中正確使用Springs IOC,依賴項注入?

請提及步驟。

假設您有:

class Bean1 {
  Bean2 bean2;
}

class Bean2 {
  String data;
}

context.xml文件

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

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

那么這應該是真的

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

// bean1.bean2 should not be null here.

您可以使用spring提供的自動裝配支持,以便向不屬於應用程序上下文的對象注入依賴項(並可能將后處理器)注入到對象中。

就您而言,此對象是您的獨立應用程序。

這是實現此目的的方法。 在此示例中,我使用@Autowired(對於b1),傳統DI(對於b2)和初始化鈎子對b3。 具有注釋的自動裝配支持假定您已在應用程序上下文中定義了適當的spring后處理器(例如,通過聲明<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
  }
}

在此示例中,所有b1,b2和b3都應為非空值(假設應用程序上下文中存在b1和b2 Bean)。

我還沒有測試過它(由於打字錯誤,甚至可能無法編譯),但是這個想法在最后3行中。 請參閱有關AutowireCapableBeanFactory的javadocs和所提到的方法,以了解發生了什么。

如果您更喜歡(主要是)純Java,則可以使用Java config:

@Configuration
pubic class MyConfig {

   @Bean
   public Bean1 bean1() { return new Bean1(); }

   @Bean
   public Bean2 bean2() { return new Bean2(bean1()); }
}

然后實例化:

ApplicationContext ctx = 
    new AnnotationConfigApplicationContext(MyConfig.class);

請記住,有些尚不支持純注釋驅動的配置(例如tx:annotation-driven等),您可能需要一些xml膠水代碼。

<beans>
  <tx:annotation-driven />
  <context:annotation-config/>
  <bean class="MyConfig"/>
</beans>

然后使用基於XML的標准方式創建ApplicationContext(例如ClasspathXmlApplicationContext或spring Web context loader等)。

您可以使用命令@Autowired

您是在調用context.getBean("beanName")以獲得對該bean的引用還是在做一個new SomeClass() 如果通過getBean()則應該設置注入的屬性。

另外,請確保使用相同的bean工廠(ClassPathXmlApplicationContext實例)來檢索所有bean。 這很可能應該是static final,並由整個應用程序使用。

您如何確認未正確連接Bean? 一個常見的問題是xml配置文件不在正確的位置。 您能否給我們更多信息,例如項目布局以及用於從容器中獲取bean的代碼?

如果將log4j日志添加到您的應用程序,您應該會看到一連串的消息,這些消息將告訴您有關Spring正在做什么和正在做什么的很多信息。 如果您沒有反饋,那您將處於黑暗之中。 通過從log4j中獲取Spring的更多信息,您也許可以找出答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM