我正在尝试让Spring为我初始化一个bean。请注意,我对调度员(我认为)不感兴趣,因为我并不关心Vaadin组件本身是由Spring创建的。
这是我到目前为止所做的:
的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: [com.sgss.tam.web.MainUI@30b1a21d]
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上下文呢?
提前致谢!
答案 0 :(得分:0)
好吧,三天后,这是我的解决方案:
首先,我放弃了web.xml,转而支持我的UI子类上的Vaadin 7样式注释:
@WebServlet(initParams = { //
@WebInitParam(name = "contextConfigLocation", value = "/WEB-INF/applicationConfiguration.xml") //
}, value = { "/tam/*", "/VAADIN/*" }, asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = MainUI.class, widgetset = "com.sgss.AppWidgetSet")
public static class Servlet extends VaadinServlet {
}
请注意,重要的不是我对&#34; contextConfigLocation&#34;的价值。 init var here ... Jetty很乐意忽略这个值并加载/WEB-INF/applicationConfiguration.xml。但它必须存在。 所以我重命名并移动了上面的spring-context.xml以对应于此。
然后,为了在MainUI中实现WebApplicationInitializer:
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
final ContextLoaderListener listener = servletContext.createListener(ContextLoaderListener.class);
listener.initWebApplicationContext(servletContext);
final ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
environmentVars = context.getBean("envVars", EnvironmentVars.class);
}
其中environmentVars
是MainUI中的静态变量。
简单,对吧?