我正在使用Spring和Vaadin在SpringVaadinIntegration附加组件中开发一个应用程序,现在当我在我的主要Vaadin UI类中注入服务时,每个方法都运行良好,问题是我在其他类中使用此注释
这是MyService
@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不为null而在主面板中不起作用
@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;
}
}
在这里,我尝试保存用户,该服务为null。我做错了什么?
答案 0 :(得分:0)
您已将MainPanel创建为“new MainPanel();”因此主面板不是Spring Bean,因此不会进行任何注射。
将此MainPanel替换为Spring注入,可能是ptototype范围,MainPanel中的任何注入都可以。