Vaadin Spring integration add-on doesn't work

I’m developing an application using Spring and Vaadin across SpringVaadinIntegration add-on, now when i inject the service in my main Vaadin UI class everythings work well, the problem is then i use this annotation in others class.

This is 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");
	}

    }

This is my main Vaadin UI class


 @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);
                 

	}
      }

in MyUI the @autowired work in fact myservice is not null while in Mainpanel doesn’t work


@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;
	}
     }

Here where I try to save the user, the service is null. What am I doing wrong?

You don’t autowire the MainPanel instance in the MyUI class so it is not Spring-managed bean → autowiring does not apply.

Hi Mario,

Did you manage to solve this issue ? I am having similar problem, in my case I cannot autowire the service to the UI class. I had the same problem with the view class but I solved it my autowiring the constructor. I tried the same approach with UI class but it requires an empty constructor which (automatically) invoked.

I am really exhausted of dealing with this. Would you mind sending me a sample project just with a service being autowired in the UI so I can try to figure out what am I doing wrong.

I have already seen the examples at github but they are not helpful.

Best Regards,

Alex