如何配置 spring 和 vaadin 将我自己的 bean 注入到 vaadin 视图中?

how to configure spring and vaadin to inject my own beans into vaadin views?

Gys,帮帮忙!我快疯了。我有 spring + 使用 spring initialzr 生成的 vaadin 项目,它基于 spring 引导。我想在同一个项目中拥有我的后端和前端功能,并使用“Autowired 注释”将我的 bean 注入到 vaadin 视图中。我哪里错了? 我已经创建了上下文 xml 配置文件来描述我的 bean:

<?xml version="1.0" encoding="UTF-8"?>
<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"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean class="ru.yudnikov.blog.backend.PostManager" id="postManager"/>
<bean class="ru.yudnikov.blog.backend.PostModel" id="postModel" scope="prototype"/>
</beans> 

我已经声明了 Application class,它实现了 ApplicationContextAware(将静态 link 放置到我的上下文中)但是当我调试方法 setApplicationContext(ApplicationContext applicationContext) 时,我看到 applicationContext 是不是空的。它包含 vaadin bean 的声明,例如 PostView,我想在其中注入我的管理器(控制器)"Authwired annotation. But if i would set centext as new classpath context from xml file, i'll lose views defenition. And i can't find a way to add my beans definitions into existing context. I've tried setParent() setClassLoader() and refresh() but it doesn't help. How to solve this? Should i try to "merge" 这两个上下文或什么?

使用 Vaadin Spring 插件(https://github.com/vaadin/spring)。

这里是主要的例子 UI class(我在这里使用 javax Inject,但你可以使用 Spring Autowired annotation):

@SpringUI
@Theme("new")
@Widgetset("com.example.NewWidgetSet")
public class NewUI extends UI implements Constants {
    private static final Logger logger = LoggerFactory.getLogger(NewUi.class);
    private CssLayout contentLayout;
    private SpringViewProvider viewProvider;

    @Inject
    public RessuUi(HeaderLayout headerLayout, SpringViewProvider viewProvider) {
        this.headerLayout = headerLayout;
        this.viewProvider = viewProvider;
    }

    @Override
    protected void init(VaadinRequest request) {
        initNavigator();
        initRootLayout(); // init here content
    }

    private void initNavigator() {
        Navigator newNavigator = new Navigator(this, contentLayout);
        newNavigator.addProvider(viewProvider);
        setNavigator(newNavigator);
    }

    @Override
    public void detach() {
        logger.debug("Detaching UI.");
    }

}

视图示例 class:

@SpringView(name = UserFormView.NAME)
public class UserFormView extends VerticalLayout implements View {

    private MyService myService;

    @Autowired
    public UserFormView(MyService myService) {
        this.myService = myService;
    }

}

您还需要在 main/webapp/WEB-INF 目录中创建 applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:component-scan base-package="com.mypackage.to.scan"/>
</beans>

同时创建 MainConfiguration class:

@Configuration
@EnableVaadin
@ComponentScan
@PropertySource("classpath:application.properties")
public class MainConfiguration {

}

同时添加 web servlet:

@Slf4j
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = true, ui = NewUi.class, widgetset = "fi.ssm.ressu.NewWidgetSet",
    heartbeatInterval = 60, closeIdleSessions = true)
public class NewServlet extends SpringVaadinServlet {

}

您可以在 Vaadin Injection and Scopes 阅读更多内容。