Spring Vaadin 7和弹簧喷油-工作不正常
我集成了Vaadin和Spring,但是@Inject只在主UI中工作。 这是我的主课Spring Vaadin 7和弹簧喷油-工作不正常,spring,vaadin,vaadin7,Spring,Vaadin,Vaadin7,我集成了Vaadin和Spring,但是@Inject只在主UI中工作。 这是我的主课 @Component @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @PreserveOnRefresh @Slf4j public class AdminPanelMain extends UI { @Inject //It works private UserService userService; private static
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@PreserveOnRefresh
@Slf4j
public class AdminPanelMain extends UI {
@Inject //It works
private UserService userService;
private static final long serialVersionUID = 1L;
// @Inject
private Panel tagsPanel = new TagsPanel();
用户服务
服务-它只是一个测试版本,什么都不是,我只是想把它当作一个bean
@Service
@Slf4j
public class UserService {
// @Autowired
// private UserRepository userRepository;
public void saveUser() {
// log.info("count: {}", userRepository.count());
log.warn("saved");
}
}
塔斯潘
TagsPanel是许多组件中的一个,我希望每个组件都能提供Spring服务
public class TagsPanel extends Panel implements Property.ValueChangeListener {
private static final long serialVersionUID = 1L;
@Autowired //null
private UserService userService;
public TagsPanel() {
initLayout();
}
.......
}
应用程序初始化器
public class ApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(ApplicationConfiguration.class);
registerSpringContextLoaderListener(servletContext, rootContext);
registerVaadinServlet(servletContext, rootContext);
}
private void registerVaadinServlet(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext) {
VaadinServlet vaadinServlet = new VaadinServlet();
ServletRegistration.Dynamic vaadinServletRegistration = servletContext.addServlet("vaadinServlet",
vaadinServlet);
vaadinServletRegistration.setInitParameter("ui", AdminPanelMain.class.getName());
vaadinServletRegistration.setInitParameter("UIProvider", SpringUIProvider.class.getName());
vaadinServletRegistration.setLoadOnStartup(1);
vaadinServletRegistration.addMapping("/*");
}
private void registerSpringContextLoaderListener(ServletContext servletContext,
AnnotationConfigWebApplicationContext rootContext) {
servletContext.addListener(new ContextLoaderListener(rootContext));
servletContext.addListener(new ContextCleanupListener());
servletContext.addListener(new RequestContextListener());
}
}
当我创建一个类TagsPanel时,UserService已经为null。为什么?
谢谢 因为您是用
新建的创建的。Spring容器只能配置托管bean(即容器创建的bean)。如果改为自动连线,它将工作。问题已解决。当我注射的时候。我犯了一个错误,很多面板都有相同的名字
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@PreserveOnRefresh
@Slf4j
public class AdminPanelMain extends UI {
@Inject
private UserService userService;
private static final long serialVersionUID = 1L;
@Inject
private Panel addUserPanel;
@Inject
private Panel tagsPanel;
.....
这就是我犯的错误
13:25:51,286 SEVERE [com.vaadin.server.DefaultErrorHandler] (default task-3) : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminPanelMain': Injection of autowired dependencies failed; ne
sted exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vaadin.ui.Panel com.jdev.blog.admin.AdminPanelMain.addUserPanel; nested exception is org.springframework.beans.factor
y.NoUniqueBeanDefinitionException: No qualifying bean of type [com.vaadin.ui.Panel] is defined: expected single matching bean but found 2: TagsPanel,UserPanel
解决方案
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@PreserveOnRefresh
@Slf4j
public class AdminPanelMain extends UI {
@Inject
private UserService userService;
private static final long serialVersionUID = 1L;
@Inject
private UserPanel addUserPanel;
@Inject
private TagsPanel tagsPanel;
.....
除了自己解决spring和vaadin的集成问题,您还可以使用新提出的官方spring/vaadin集成
这种集成可以在github上找到。
最有趣的是,这种集成是由Vaadin和Pivotal(spring)的两个核心开发人员实现的。只需添加一些注释(如github项目网站上所述),就可以很容易地进行设置
未来,它还将集成spring security和spring i18n对vaadin的支持 我已经看过了他们的演示和这个实现。告诉我是否比你的好?如果我现在继续朝着这个方向前进,我将面临哪些挑战?正如我看到的例子是spring boot。这很好,但在某些情况下(包括我的),没有使用spring boot,我发现这个问题是正确的。