1

我正在跨 SpringVaadinIntegration 附加组件使用 Spring 和 Vaadin 开发应用程序,现在当我在我的主 Vaadin UI 类中注入服务时一切正常,问题是我在其他类中使用此注释

这是我的服务

@Component
@Scope("session")
public class MyService {

public MyService() {
    // TODO Auto-generated constructor stub
}

public String getLabel(){
    return "this is my label";
}

public void saveUser(Utente utente){
    System.out.println("save user");
}

}

这是我的主要 Vaadin UI 类

@Component
@Scope("prototype")
public class MyUI extends UI
{

@Autowired
private transient ApplicationContext applicationContext;

@Autowired
private MyService myService;

private MainPanel mainPanel;

private VerticalLayout verticalLayout; 




@Override
protected void init(VaadinRequest request) {
    // TODO Auto-generated method stub


    verticalLayout = new VerticalLayout();

    mainPanel = new MainPanel();
    verticalLayout.addComponent(mainPanel);
    setContent(verticalLayout);


}
  }

在 MyUI 中,@autowired 工作实际上 myservice 不为空,而在主面板中不起作用

@Component
public class MainPanel extends CustomComponent implements View {

private VerticalLayout mainLayout;

private Button button;

@Autowired
MyService  secondService;

public MainPanel() {
    buildMainLayout();
    setCompositionRoot(mainLayout);

    // TODO add user code here
}

@AutoGenerated
private VerticalLayout buildMainLayout() {
    // common part: create layout
    button = new Button();
    mainLayout = new VerticalLayout();
    mainLayout.addComponent(button);

    button.addClickListener(new Button.ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {
            // TODO Auto-generated method stub
            secondService.saveUser(new Utente());

        }
    }); 


    return mainLayout;
}
 }

在这里我尝试保存用户,服务为空。我究竟做错了什么?

4

1 回答 1

0

您已将 MainPanel 创建为“new MainPanel();” 所以主面板不是 Spring Bean,所以不会发生任何注入。

将此 MainPanel 替换为可能具有原型范围的 Spring 注入,并且 MainPanel 中的任何注入都可以。

于 2014-03-25T09:15:43.597 回答