2014-06-23 37 views
1

我有以下類:只有在訪問靜態成員時纔會調用類的註釋嗎?

package hello; 

import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.SpringApplication; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.stereotype.Component; 

@Component 
@EnableAutoConfiguration 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

如果我引用它在我的主駱駝航線,像這樣:

package com.example.integration; 

import hello.*; 

import org.apache.camel.ProducerTemplate; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 


public class TestCamelSpring { 

     public static void main(String[] args) { 
       ApplicationContext context = new ClassPathXmlApplicationContext("camelspring.xml"); 
      ProducerTemplate camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class); 

     Application.main(args); 
     System.out.println("Message Sending started"); 
     camelTemplate.sendBody("jms:queue:testQSource","Sample Message"); 
     System.out.println("Message sent"); 

    } 

} 

難道我在Application.class註釋仍然可以訪問,即使我只是參考Application.main?

我問,因爲@EnableAutoConfiguration應該爲Tomcat配置應用程序,但現在我沒有直接運行Application.class,應用程序默認爲碼頭,然後我得到一個錯誤,WebSockets只在Tomcat中受支持。

有沒有人有過這個問題,或知道如何解決它?

這是堆棧跟蹤。我可以從控制檯日誌中看到,在示例中訪問整個類時,它從不啓動它執行的Tomcat實例。它似乎仍在繼續,好像它是一個碼頭應用程序而不是Tomcat。請糾正我,如果任何這些假設是錯誤的:

Caused by: java.lang.IllegalStateException: Websockets are currently only supported in Tomcat (found class org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory). 
at org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration$1.customize(WebSocketAutoConfiguration.java:74) 
at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:67) 
at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:54) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1545) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) 
... 16 more 
+1

一個更通用的問題可以*「的一類做註釋仍然只能訪問靜態成員時,被調用?」 * – christopher

+0

你可能是正確的。我改變了這個問題,以更正確地反映我的問題。我感謝你的意見。 – Theo

+0

異常發生在哪裏?在'camelTemplate.sendBody(..)'?這個bean與'Application'創建的'ApplicationContext'完全無關。 –

回答

3

首先,註釋不能被「調用」。

註解是數據,而不是代碼。在您的情況下,當您撥打SpringApplication.run(Application.class, args);並執行必要的配置時,Spring Boot會讀取您的註釋,因此無論您如何撥打Application.main()都無關緊要。

我想你的問題是由於你在類路徑中有Jetty而引起的,它強制Spring Boot使用Jetty而不是Tomcat作爲嵌入式servlet容器。

所以,儘量做到以下幾點:

  • 瞭解如何碼頭出現在你的classpath
    使用mvn dependency:tree -Dverbose如果你使用Maven

  • 如果你沒有在classpath中需要碼頭,從依賴

  • 排除它,否則,你需要力彈簧引導忽略
    喜歡的東西碼頭的存在exclude = EmbeddedServletContainerAutoConfiguration.EmbeddedJetty.class@EnableAutoConfiguration可以幫助