首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在vaadin中使用弹簧

在vaadin中使用弹簧
EN

Stack Overflow用户
提问于 2014-08-04 22:40:26
回答 3查看 825关注 0票数 0

我想在vaadin中使用spring

这是我的配置:

web.xml

代码语言:javascript
复制
<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中工作,然后Ok!

在vaadin和tomcat中运行时,identityService出现java.lang.NullPointerException错误

我的问题在哪里?

谢谢

EN

回答 3

Stack Overflow用户

发布于 2014-08-04 23:20:39

com.MyUI是由Vaadin servlet创建的,并且该servlet不知道Spring。实际情况是,您的UI实例是由反射创建的,而不是Spring托管bean。

您需要使用与Spring集成的Vaadin插件。有关更多详细信息,请查看the vaadin4spring project

也许您应该将类更新为org.vaadin.spring.servlet.SpringAwareVaadinServlet

票数 3
EN

Stack Overflow用户

发布于 2014-08-08 22:12:43

尝试自动绑定您的bean explicite (例如,在构造函数中):

代码语言:javascript
复制
   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$
        }
    }
票数 1
EN

Stack Overflow用户

发布于 2014-08-25 20:22:08

你可以使用ru.xpoft.vaadin.SpringVaadinServlet,查看Vaadin网站-插件

代码语言:javascript
复制
<servlet>
    <servlet-name>MyCustom Application</servlet-name>
      <servlet-class>ru.xpoft.vaadin.SpringVaadinServlet</servlet-class> ...............
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25121281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档