问题:
当 Vaadin 组件可以是 spring 容器中的 bean 时(@SpringComponent
注解)?
问题澄清:
我问这个问题是因为我知道 Vaadin View 使用后可能是 spring bean@SpringView
。
但如果我注释Button
组件与@SpringComponent
它只会被创建一次。会造成什么问题吗?
Example:
我有很多 JpaRepository bean:
public interface CustomerRepository extends JpaRepository<Customer, Long>
// ...
public interface UserRepository extends JpaRepository<User, Long> {
// ...
我想在不同的地方使用它们 - 例如在 Tab 组件中(在 Vaadin TabSheet 中)。所以我有一个想法——tabContent
也可以是弹簧组件:
@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:
@SpringComponent
public class SomeTabContent extends VerticalLayout {
@Autowired
private CustomerRepository customerRepository;
@Autowired
private UserRepository UserRepository;
}
这是正确的架构吗?
注意:我知道 Vaadin 具有 CDI 和数据绑定功能,但我不想使用它们。我还知道我可以在任何地方手动创建 Spring 应用程序上下文 - 但我认为这不是正确的方法。
如果您只使用@Component(或@SpringComponent),该类将为应用程序实例化一次,因此,是的,当您不希望多个会话共享结果对象时,您会遇到问题。
所以你也添加@UIScope。这将确保您在每个会话中获得该类的一个实例。您可以在类中存储会话数据(就像您使用 Spring 的 @Session 一样,但这需要您使用 Spring 的 Dispatcher)。
在 @UIScope 类中,您可以使用 @Autowired 和 @PostConstruct 等。我将它用于 Windows 和布局等的扩展。您的类路径上需要 com.vaadin:vaadin-spring:1.0.0 。更多信息瓦丁泉 https://github.com/vaadin/spring
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)