Spring not initialising

When we have the following class, the app loads but MainView is null because it has not initialised spring via the application context

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

import org.springframework.beans.factory.annotation.Autowired;

import com.vaadin.annotations.Push;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.shared.ui.ui.Transport;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.UI;

@Theme(“celer”)
@SpringUI()
@Push(transport=Transport.WEBSOCKET)
public class VaadinhttpsUI extends UI {

@WebServlet(initParams = {
@WebInitParam(name = “contextConfigLocation”, value = “/WEB-INF/applicationContext.xml”)
}, value = “/*”, asyncSupported = true)
@VaadinServletConfiguration(productionMode = true, ui = VaadinhttpsUI.class)
public static class Servlet extends VaadinServlet {
}

@Autowired
private MainView mainView;
@Override
protected void init(VaadinRequest vaadinRequest) {
setContent(this.mainView);
}

}

Hi,
perhaps your servlet should extend SpringVaadinServlet

HTH
Marco

Thanks that just leads to:

Jan 23, 2018 8:36:38 PM com.vaadin.server.DefaultErrorHandler doDefault
SEVERE:
java.lang.IllegalStateException: Spring WebApplicationContext not initialized for UI provider. Use e.g. ContextLoaderListener to initialize it.
at com.vaadin.spring.server.SpringUIProvider.(SpringUIProvider.java:80)
at com.vaadin.spring.server.SpringVaadinServlet$1.sessionInit(SpringVaadinServlet.java:101)
at com.vaadin.server.VaadinService.onVaadinSessionStarted(VaadinService.java:864)
at com.vaadin.server.VaadinService.createAndRegisterSession(VaadinService.java:822)
at com.vaadin.server.VaadinService.doFindOrCreateVaadinSession(VaadinService.java:774)
at com.vaadin.server.VaadinService.findOrCreateVaadinSession(VaadinService.java:714)

Vaadin docs says

The application should not have a servlet extending VaadinServlet, as Vaadin servlet has its own SpringVaadinServlet that is deployed automatically

https://vaadin.com/docs/framework/advanced/advanced-spring.html

So my suggestion may not be needed.

Is your MainView annotated as @SpringComponent and @UIScope?
In log can you see spring initialization?

Must say tnis is very hard to get a simple project deployed. Eclipse is fine seems to work but when it comes to getting it out not great. Must be a simpler way.

yes correct so I did try that but if you do that it will use spring-boot. The only issue with that is spring-boot does not seem to accept the java classpath. So say when you build your war and deploy it and you have a file called. myfunnydata.dat you can’t ready it because it gives classpath errors.

How did you set the classpath with just -classpath?

Using Spring Initializr makes starting a new project very easy, even with vaadin.
Just to better understand, your project is running inside eclipse but not when deployed on an external application server?

That’s weird.
Never got this kind of problems with spring boot; also packaging as war I was always able to read resources from classpath.

Sorry,
I misunderstood your deployment configuration.
When I need external classpath for a spring boot executable jar I use the “-Dloader.path” property, and also configure spring-boot-maven-plugin with layout=zip configuration.

But I never tested this with war packaging

This seems to work for me with war packaging (and without zip layout)

java -cp "../target/spring-test-1.0-SNAPSHOT.jar:./target/demo-0.0.1-SNAPSHOT.war" org.springframework.boot.loader.WarLauncher

From a class inside demo-0.0.1-SNAPSHOT.war I can access a file in spring-test-1.0-SNAPSHOT.jar

HTH
Marco

I don’t know gradle very well.
With maven I think you could skip repackage goal of spring-boot plugin and then use assembly plugin to package the as you want (eg as a zip with lib dir with all dependencies, conf dir, …)

HTH
Marco

So look at this example:

java -DApplicationProfileId=default -cp “…/…/src/main/profiles” -jar test.war

$ ls -la …/…/src/main/profiles/
total 8
drwxr-xr-x 3 user staff 102 10 Jan 18:45 .
drwxr-xr-x@ 7 user staff 238 27 Dec 13:17 …
-rw-r–r-- 1 user staff 1266 15 Jan 09:55 default-config.xml
$

. ____ _ __ _ _
/\ / __ _ () __ __ _ \ \ \
( ( )_
_ | '_ | '| | ’ / ` | \ \ \
\/ )| |)| | | | | || (| | ) ) ) )
’ |
| .__|| ||| |_, | / / / /
=========|
|==============|/=////
:: Spring Boot :: (v1.5.9.RELEASE)

2018-01-24 08:37:59.559 INFO 47302 — [ main]
c.c.appconfig.AppContextPreparer : Preparing environment for module [celertech-riskmonitor]
using profileId [default]

2018-01-24 08:38:00.388 ERROR 47302 — [ main]
o.s.boot.SpringApplication : Application startup failed

java.lang.RuntimeException: An error occurred reading the default-config.xml config file: class path resource [default-config.xml]
cannot be opened because it does not exist


…Caused by: java.io.FileNotFoundException: class path resource [default-config.xml]
cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) ~[spring-core-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]

But that file does exist.

Okay that does not work same result

java -DApplicationProfileId=default -Dloader.path=…/…/src/main/profiles -jar test.war



…Caused by: java.io.FileNotFoundException: class path resource [default-config.xml]
cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) ~[spring-core-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]

Wow that was it, okay so it was the org.springframework.boot.loader.WarLauncher that was needed.

Do you know how to run it as a flat project? i.e with all the jars exploded via gradle? so package as zip

So the only way to get this to work was to add this dep into the compile and package.

But then when you startup the app you get the following

“org.springframework.boot:spring-boot-loader:1.5.9.RELEASE”,

nested exception is java.lang.NoSuchMethodError: com.vaadin.ui.Grid.addComponentColumn(Lcom/vaadin/data/ValueProvider;)Lcom/vaadin/ui/Grid$Column;] with root cause

That makes me thing there is a transitive under this that is causing a conflict.

Sorry, I can’t figure out the problem.
Can you share your build file (maven or gradle)?

Sure let me package it up. Really this is not an easy process maybe gradle but should be simple. Spring boot I think causes more issues than it is worth. Let me package up a build.gradle.