我有以下類:只有在訪問靜態成員時纔會調用類的註釋嗎?
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
一個更通用的問題可以*「的一類做註釋仍然只能訪問靜態成員時,被調用?」 * – christopher
你可能是正確的。我改變了這個問題,以更正確地反映我的問題。我感謝你的意見。 – Theo
異常發生在哪裏?在'camelTemplate.sendBody(..)'?這個bean與'Application'創建的'ApplicationContext'完全無關。 –