Gys,求助!我生气了。我有spring springzr生成的spring + vaadin项目,它基于spring boot。我希望在同一个项目中拥有我的后端和前端功能,并使用“Autowired annotation”将我的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>
我已经声明了实现ApplicationContextAware的Application类(将静态链接放到我的上下文中),但是当我调试方法setApplicationContext(ApplicationContext applicationContext)时,我发现applicationContext不为空。它包含了vaadin bean的delaration,比如PostView,我想给我的管理器(控制器)注入“Authwired annotation。但是如果我将centext设置为xml文件中的新类路径上下文,我将失去视图防御。我可以找不到将bean定义添加到现有上下文中的方法。我尝试过setParent()setClassLoader()和refresh(),但它没有帮助。如何解决这个问题?我应该尝试“合并”这两个上下文或者什么?
答案 0 :(得分:3)
使用Vaadin Spring插件(https://github.com/vaadin/spring)。
以下是主UI类的示例(我在这里使用javax Inject,但您可以使用Spring Autowired注释):
@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.");
}
}
View类示例:
@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类:
@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了解更多信息。