2016-06-25 6 views
2

私はJavaとSpring Frameworkには比較的新しいです。これは私が構築している私の最初のSpring Appで、私のBeanのAutowiringで問題が発生しています。Spring:mainメソッドでBeanを取得できません。

マイApplication.java:

@SpringBootApplication 
public class Application { 

public static void main(String[] args) throws Exception { 
    SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class); 
    builder.headless(false); 
    ConfigurableApplicationContext ctx = builder.run(args); 
    AppRepository oAppRepo = ctx.getBean(AppRepository.class); 
    // Check the SystemTray is supported 
    if (!SystemTray.isSupported()) { 
     System.out.println("SystemTray is not supported"); 
     return; 
    } 
    CustomTray oTray = ctx.getBean(CustomTray.class); 
    } 
} 

ressourceフォルダ内の私のconfig.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
    "> 

<context:property-placeholder location="classpath:/application.properties"/> 
<context:component-scan base-package="app"/> 
<import resource="beans/*.xml" /> 
</beans> 

マイ豆/ tray.xml:私が得る

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

<bean id="customTray" class="app.CustomTray"> 
    <property name="schedulerThread" value="app.SchedulerThread"></property> 
    <property name="commandControlInterface" value="app.CommandControlInterface"></property> 
</bean> 
</beans> 

エラーメッセージ起動時:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [app.CustomTray] is defined 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331) 
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968) 
at app.Application.main(Application.java:46) 

なぜ、私がtray.xmlにBeanを登録したために、修飾されたBeanが存在しないのか分かりません。 CustomTrayクラスはTrayIconのクラスから派生し、特別のTrayIconの何も作成しませんが、されて終了し、 私CustomTray用:

public class CustomTray extends TrayIcon { 

private static final String IMAGE_PATH = "/images/icon.png"; 
private static final String TOOLTIP = "Test"; 

private PopupMenu popup; 
private SystemTray tray; 
@Autowired private AppRepository oAppRepo; 

public CustomTray() { 
    super(createImage(IMAGE_PATH, TOOLTIP), TOOLTIP); 
    popup = new PopupMenu(); 
    tray = SystemTray.getSystemTray(); 
    try { 
     this.setup(); 
    } catch (AWTException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

@PostConstruct 
private void setup() throws AWTException { 

} 

protected static Image createImage(String path, String description) { 
    URL imageURL = CustomTray.class.getResource(path); 
    if (imageURL == null) { 
     System.err.println("Failed Creating Image. Resource not found: " + path); 
     return null; 
    } else { 
     return new ImageIcon(imageURL, description).getImage(); 
    } 
    } 
} 

私はオートワイヤリングと豆を使用せずに、前に実行しているアプリケーションを、得たが、今私は、セットアップAをリファクタリングしたいですきれいな春のアプリ、私は間違ってやっていることを知っている誰かがheresを願っています。

編集: これで、UUIDで言及されている注釈付きのxml-Filesをインポートしても、別のエラーメッセージが表示されます。 BeanのcustomTrayには2つのプロパティがあり、SchedulerThreadプロパティはSingleton Beanになります。 beans/scheduler.xmlで宣言されています。それ自体beanであるプロパティもあります。そのような:

scheduler.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id="schedulerThread" class="app.SchedulerThread"> 
     <property name="processManager" value="app.ProcessManager"></property> 
    </bean> 
</beans> 

エラー:

Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'app.ProcessManager' for property 'processManager'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [app.ProcessManager] for property 'processManager': no matching editors or conversion strategy found 

これらのクラスを配線してBeanを作成するための最良の方法は何ですか?私が言ったように、私は春には新しく、豆とオートワイヤリングで動作する方法を理解していなかったと思います。 SpringBootアプリケーションで

答えて

1

、あなたはあなたが私のためのソリューションを

Arrays.stream(ctx.getBeanDefinitionNames()).forEach(System.out::println); 
0

を使って豆の名前を取得することができますImportResource注釈

@ImportResource(locations = { "classpath:config.xml", "beans/tray.xml" }) 

Spring Boot documentation - XML configuration

を使用してリソースを指定する必要が削除しましたXML-Configファイルを作成し、AppConfig-Fileを作成してください。これで、私のApplication.javaは次のようになります:

012このような
@SpringBootApplication 
    public class Application { 
     public static void main(String[] args) { 
      SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class); 
      builder.headless(false); 
      builder.run(args); 
     } 
    } 

とのAppConfig:

@Configuration 
    public class AppConfig { 
     @Autowired 
     GridFsTemplate gridFSTemplate; 

     @Autowired 
     AppRepository appRepository; 

     @Autowired 
     UserRepository userRepository; 

     @Bean 
     ProcessManager processManager() { 
      return new ProcessManager(); 
     } 

     @Bean 
     SchedulerService schedulerService() { 
      SchedulerService schedulerService = new SchedulerService(); 
      SchedulerService.setProcessManager(processManager()); 
      SchedulerService.start(); 
      return schedulerService; 
     } 

     @Bean 
     CustomTray customTray() { 
      if (!SystemTray.isSupported()) { 
       System.out.println("SystemTray is not supported"); 
       return null; 
      } 
      CustomTray customTray = new CustomTray(); 
      customTray.setSchedulerService(schedulerService()); 
      customTray.setAppRepository(this.appRepository); 
      try { 
       customTray.setup(); 
      } catch (AWTException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return customTray; 
     } 

     @Bean 
     GridFSController gridFSController() { 
      GridFSController gridFSController = new GridFSController(); 
      return gridFSController; 
     } 
    } 

は、今私はAutowired注釈を持つすべてのこれらのBeanにアクセスすることができますし、私はそのcontext.getBean()のものを必要としません。それはコードを大幅に削減し、私はそのクリーナーソリューションだと思います。

関連する問題