0

I took a lookat the old Vaadin 14 Application Lifecycle Documentation. There you could configure your own Servlets, Services and Sessions.

I kinda need that, because I need to add on top of the regular Vaadin Sessions. Simply defining my own Classes and setting them up correctly doesnt seem to do the trick.

I also already tried defining them as its own @Bean in my Application Class, but that only gave me some Error Messages during Startup.

My Application.java:

package com.marcobsidian.briefmarken;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;

import com.marcobsidian.briefmarken.base.application.ApplicationServlet;
import com.vaadin.flow.component.dependency.NpmPackage;
import com.vaadin.flow.component.page.AppShellConfigurator;
import com.vaadin.flow.server.VaadinServlet;
import com.vaadin.flow.theme.Theme;

/**
 * The entry point of the Spring Boot application.
 *
 * Use the @PWA annotation make the application installable on phones, tablets
 * and some desktop browsers.
 *
 */
@SpringBootApplication(exclude = ErrorMvcAutoConfiguration.class)
@NpmPackage(value = "@fontsource/nunito-sans", version = "4.5.0")
@Theme(value = "briefmarken")
@NpmPackage(value = "line-awesome", version = "1.3.0")
public class Application implements AppShellConfigurator {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

My ApplicationServlet.java:

package com.marcobsidian.briefmarken.base.application;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.server.*;

@WebServlet
public class ApplicationServlet extends VaadinServlet implements SessionInitListener, SessionDestroyListener {

    @Override
    protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration)
        throws ServiceException {
        ApplicationService service = new ApplicationService(this, deploymentConfiguration);
        service.init();
        return service;
    }

    @Override
    public void sessionDestroy(SessionDestroyEvent event) {
        //To be continued...
    }

    @Override
    public void sessionInit(SessionInitEvent event) throws ServiceException {
        //To be continued...
    }

}

My ApplicationService.java:

package com.marcobsidian.briefmarken.base.application;

import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.server.*;

public class ApplicationService extends VaadinServletService {

    public ApplicationService(VaadinServlet servlet, DeploymentConfiguration deploymentConfiguration) {
        super(servlet, deploymentConfiguration);
    }

    @Override
    protected VaadinSession createVaadinSession(VaadinRequest request) {
        return new ApplicationSession(this);
    }
}

ApplicationSession.java:

package com.marcobsidian.briefmarken.base.application;

import com.vaadin.flow.internal.CurrentInstance;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinSession;

public final class ApplicationSession extends VaadinSession {

    public static ApplicationSession getCurrent() {
        return CurrentInstance.get(ApplicationSession.class);
    }

    public ApplicationSession(VaadinService service) {
        super(service);
    }
}

Also for the sake of simplicity I removed all for-this-question-irrelevant-things from the classes. There is actually a whole lot going on in these. I know why I want my own Session-Object :)

Tried:

  • Defining @Bean for VaadinServlet in Application.java

  • Manually Switching out Service-Class via VaadinService.setCurrent()

  • Excluding Classes via @SpringBootApplication

  • Obviously researching a lot but havent found any Vaadin23-Articles about this topic so far

Expected:

  • Vaadin uses my custom Objects over VaadinServlet, VaadinServletService and VaadinService, but it never ended up doing that
  • 2
    I don't understand why all this boilerplate is needed when you could just call VaadinSession.getCurrent().get/setAttribute to store your custom information in the session. – Knoobie Feb 20 '23 at 21:45
  • I tried looking into this solution again. It solves one part of the Problem, the Session part, but I also need certain things initialized during SessionService Setup. I mainly need to connect to different schemes of a Database and need to initialize various HibernateConfigs for that and then possibly fire Database Updates on the Database BEFORE the Login Users are loaded. – marcobsidian Feb 21 '23 at 08:07
  • Using the Servlet and ServletService seemed like the easiest way to ensure that its all happening in the correct order. I will look into other ways. Anyways, there are probably people that eventually want to do the thing i wanted to do, so an answer to this question would still be awesome :) – marcobsidian Feb 21 '23 at 08:08
  • 1
    From the code shown here, it looks like you never actually make your own classes known to replace the defaults. Add a class beside your `Application`, annotate it with `@Configuration` and add methods annodated with `@Bean` returning e.g. your own vaadin servlet (which most likely you want to subclass from `SpringServlet` and not `VaadinServlet`) – cfrick Feb 21 '23 at 09:33
  • Your requirement looks like the perfect fit for the usage of Spring FilterChain (you are using Spring anyway) – Knoobie Feb 21 '23 at 10:49

0 Answers0