我想让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的上下文呢?
在此先感谢!