0

我想在 vaadin 中使用 spring,
这是我的配置:
web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>VaadinApplicationServlet</servlet-name>
        <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
        <init-param>
            <param-name>UI</param-name>
            <param-value>com.MyUI</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>VaadinApplicationServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

applicationContext.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:5432/Activiti"/>
        <property name="username" value="postgres"/>
        <property name="password" value="10"/>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="databaseType" value="postgres"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="transactionManager" ref="transactionManager"/>
        <property name="databaseSchemaUpdate" value="true"/>
        <property name="deploymentResources"
                  value="classpath* : #{Init.path_Process}"/>
        <property name="history" value="audit"/>
        <property name="jobExecutorActivate" value="false"/>
    </bean>

    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
    </bean>

    <bean id="repositoryService" factory-bean="processEngine"
          factory-method="getRepositoryService"/>
    <bean id="runtimeService" factory-bean="processEngine"
          factory-method="getRuntimeService"/>
    <bean id="taskService" factory-bean="processEngine"
          factory-method="getTaskService"/>
    <bean id="historyService" factory-bean="processEngine"
          factory-method="getHistoryService"/>
    <bean id="managementService" factory-bean="processEngine"
          factory-method="getManagementService"/>
    <bean id="formService" factory-bean="processEngine"
          factory-method="getFormService"/>
    <bean id="identityService" factory-bean="processEngine"
          factory-method="getIdentityService"/>

    <bean id="Init" class="util.Init"/>
    <context:annotation-config/>
    <context:component-scan base-package="com"/>    
    <context:spring-configured/>
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

in MyUI class:
#java
@Component
@Configurable
public class MyUI extends UI {
 protected void init(VaadinRequest vaadinRequest) {
...
@Autowired
    private IdentityService identityService;
...
}}

这个配置在junit中工作,好的!
但是
在 vaadin 和 tomcat 中运行时,identityService 的 java.lang.NullPointerException 错误
我的问题在哪里?
谢谢

4

3 回答 3

3

com.MyUI由 Vaadin servlet 创建,该 servlet 不了解 Spring。发生的事情是您的 UI 实例是通过反射创建的,而不是 Spring 托管 bean。

您需要使用与 Spring 集成的 Vaadin 插件。请查看vaadin4spring 项目以获取更多详细信息。

也许您应该将课程更新为org.vaadin.spring.servlet.SpringAwareVaadinServlet?

于 2014-08-04T15:20:39.233 回答
1

尝试自动装配你的 bean 显式(例如在构造函数中):

   if (VaadinServlet.getCurrent() != null) {
        try {
            WebApplicationContextUtils
                    .getRequiredWebApplicationContext(VaadinServlet.getCurrent().getServletContext())
                    .getAutowireCapableBeanFactory().autowireBean(this);
        } catch (BeansException e) {
            LOG.error("Could not inject beans!" + this.getClass(), e); //$NON-NLS-1$
        }
    }
于 2014-08-08T14:12:42.730 回答
1

您可以使用 ru.xpoft.vaadin.SpringVaadinServlet,查看 Vaadin website-addons

<servlet>
    <servlet-name>MyCustom Application</servlet-name>
      <servlet-class>ru.xpoft.vaadin.SpringVaadinServlet</servlet-class> ...............
于 2014-08-25T12:22:07.670 回答