Vaadin 组件作为 Spring bean [英] Vaadin Component as Spring bean
问题描述
问题:
Vaadin 组件何时可以是 spring 容器中的 bean(@SpringComponent
注释)?
When Vaadin component could be a bean in spring container (@SpringComponent
annotation)?
问题说明:
我问这个问题是因为我知道使用@SpringView
后Vaadin View可能是spring bean.但是如果我用 @SpringComponent
注释 Button
组件,它只会被创建一次.会不会有什么问题?
I ask this question because I know that Vaadin View could be spring bean after using @SpringView
.
But if I annotate Button
component with @SpringComponent
it will be created only once. Can it make any problem?
示例:
我有很多 JpaRepository bean:
I have a lot of JpaRepository bean:
public interface CustomerRepository extends JpaRepository<Customer, Long>
// ...
public interface UserRepository extends JpaRepository<User, Long> {
// ...
而且我想在不同的地方使用它们 - 例如在 Tab 组件中(在 Vaadin TabSheet 中).所以我有一个想法 - tabContent
也可以是 spring 组件:
And I want use them in different places - for example in a Tab component (in Vaadin TabSheet). So I have an idea - the tabContent
can be also spring component:
@SpringView(name = "viewName")
public class SomeView extends VerticalLayout implements View {
@Autowired
private SomeTabContent tabContent;
//...
public void init() { // call every view enter()
removeAllComponents();
// Initialize whole view.
tabSheet.addTab(tabContent, /* ... */);
// ...
}
然后我可以注入所有需要的 bean:
And then I can inject all needed beans:
@SpringComponent
public class SomeTabContent extends VerticalLayout {
@Autowired
private CustomerRepository customerRepository;
@Autowired
private UserRepository UserRepository;
}
这是正确的架构吗?
注意:我知道 Vaadin 具有 CDI 和数据绑定功能,但我不想使用它们.我也知道我可以在任何地方手动创建 Spring 应用程序上下文 - 但我认为这不是正确的方法.
Note: I know that Vaadin has CDI and Data Binding features, but I don't want use them. I also know that I could manually create Spring application context in any place - but I think this is not proper way.
推荐答案
在增加我的知识之后,我有了一些假设.
After increasing my knowledge I have some hypothesis.
我们可以将 Vaadin 组件用作 Spring bean 吗?为什么不.我试过这个,它的工作原理.但是...
Can we use Vaadin component as Spring bean? Why not. I tried this and it works. But...
但它也会导致多个侦听器调用 - 因为我有类设置的 Button 对象.
But it cause also multiple listeners invocation - as I have class-instated Button object.
Spring 自己创建组件的实例并将其放在某处.所以我不能在组件构造函数中做任何事情.此外,此类组件在使用前应进行清洁和初始化.听起来很奇怪吗?我想是的.而且它可能会导致很多问题——尤其是在多线程环境中.
Spring creates the instance of the component by itself and put it somewhere. So I can't do anything in component constructor. Moreover such component should be cleaned and initialized before any use. Does it sound weird? I think yes. And it might cause a lot of problems - especially in multithread environment.
所以我不建议使用任何 Vaadin 组件(例如 Tab、Buttons...)制作 Bean.因为他们需要根据使用地点改变他们的状态.
So I don't recommend making a Bean of any Vaadin components (for example Tab, Buttons...). Because they need to change their state depending on place of use.
但是如何使对象中的所有 Spring bean 都可用,而不是由 Spring 容器维护呢?这不是新问题,至少有两种解决方案:
But how to make available all Spring beans in object which is not maintained by Spring container? This isn't new question and there are at least two solutions:
Annotate such class with
@Configurable
and enable any weaving (compile or run time). This is explained for example here or here.
直接使用AutowireCapableBeanFactory
和autowiredBeanProperty()
方法.可以在 此处 找到一些解释和也在 SO 上.
Directly use of AutowireCapableBeanFactory
and autowiredBeanProperty()
method. A little explanation can be find here and also on SO.
这篇关于Vaadin 组件作为 Spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!