我想讓Spring爲我初始化一個bean。請注意,我對調度員(我認爲)並不感興趣,因爲我不在意讓Spring自己初始化Vaadin組件。Vaadin 7 + Spring:沒有ContextLoaderListener註冊?
這是我到目前爲止有:
web.xml中:
<web-app
id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>TAM</servlet-name>
<servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>com.sgss.tam.web.MainUI</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TAM</servlet-name>
<url-pattern>/tam/*</url-pattern>
</servlet-mapping>
</web-app>
彈簧的context.xml:
<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:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
">
<!-- Turn on AspectJ @Configurable support -->
<context:spring-configured />
<context:component-scan base-package="com.sgss.tam" />
<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="envVars" class="com.sgss.tam.service.EnvironmentVars">
<property name="workflowEngineUrl" value="fafafaf"></property>
<property name="userAuthenticationUrl" value="fofofo"></property>
</bean>
</beans>
這個練習的要點是隻需要具有由Spring配置的com.sgss.tam.service.EnvironmentVars。如果能正常工作,那麼我應該能夠通過如此實施WebApplicationInitializer在我的UI子類來訪問這個bean,:
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
final ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
final EnvironmentVars envVars = context.getBean("envVars", EnvironmentVars.class);
}
但是...
相反,onStartup執行上面的代碼時,這裏是實際發生的事情:
2015-02-24 15:53:34.814:INFO:/:main: Spring WebApplicationInitializers detected on classpath: [[email protected]]
2015-02-24 15:53:42.706:WARN:oeja.ServletContainerInitializersStarter:main:
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:90)
at com.sgss.tam.web.MainUI.onStartup(MainUI.java:69)
就好像我沒有在web.xml中定義一個監聽器。
如果我將偵聽器類更改爲不存在的東西,則會看到相應的錯誤消息,通知我該類未找到,這說明此配置實際上正在被解析。那麼爲什麼沒有Spring的上下文呢?
在此先感謝!