thymeleaf caching with spring boot - spring-boot

I have found many, many posts about disabling template caching with Thymeleaf for development, but I can't get it to work. My stack:
Spring Boot 1.5.7
spring-boot-devtools
Thymeleaf
IntelliJ IDEA Ultimate 2017.2
I don't need to auto-rebuild: I've mapped the "rebuild" command in IntelliJ to ⌘-S and this works great for hot-swapping Java classes. However, css and html templates still only show changes if I restart my server.
My application.properties:
spring.thymeleaf.prefix=/WEB-INF/templates/
spring.thymeleaf.cache=false
I've tried:
• moving the templates folder into webapp/resources (and adjusting the templates root)
• wiring up different kinds of template resolvers (e.g. FileTemplateResolver)
• configuring the template resolver in a #Configuration file instead of letting Spring Boot use defaults and application.properties values
Here are the relevant entries in my pom.xml file:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Is there something I'm missing here?

In case anyone else runs into this problem, it is specific to IntelliJ:
I was using a "Tomcat" template under Launch Configurations. Once I changed it to a "Spring Boot" configuration template, it behaves as expected.

Related

Azure AppConfiguration with Spring Boot 2.5.x

Question
How to integrate an Azure AppConfiguration with SpringBoot 2.5.x or higher?
Info
Im trying to use an Azure AppConfiguration resource with a Spring Boot 2.5.4 project. Unfortunately I cant get it to read a setting from the AppConfiguration or even connect to it as far as I can tell.
The project is newly created with the Spring Initializr where I only added
Spring Boot Starter Web
Spring Boot Starter Security
Spring Boot Starter WebSocket
Afterwards I tried following the Microsoft Quickstart documentation with no success. The documentation mentions that its using Spring 2.4.x so I assume some changes broke it.
I also tried to identify the issue by looking through some Azure Spring Boot Code Samples.
All the examples so far use the bootstrap.properties file which I learned during my search so far is deprecated. Moving the settings to the application.yml or enabling use-legacy-processing: true did not work either. Any ideas?
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-cloud-appconfiguration-config</artifactId>
<version>2.0.0</version>
</dependency>
...
application.yml
spring:
config:
use-legacy-processing: true
profiles:
active: "develop"
application:
name: "MySampleService"
cloud:
azure:
appconfiguration:
stores:
- connection-string: "SomeAzureAppConfigurationResourceConnectionString"
label: ${spring.profiles.active}
#mysampleservice:
# message: "this is a message from file"
AppConfiguration Resource
Im not entirely sure about the format for the setting name. I tried to build the format based on this documentation.
The configuration classes should be fine since commenting in the mysampleservice causes the value of message beeing used.
Any hints are appreciated!
Some more info to elaborate on the accepted answer
The documentation linked in the answer refers to two different packages. The one linked right at the start in the maven repository is spring-cloud-azure-appconfiguration-config while the one used further down is azure-spring-cloud-appconfiguration-config. The second one works with the bootstrap.properties file.
Working pom.xml and bootstrap.properties:
...
<dependencies>
<!-- Dependency to load configuration from azure app configuration resource. Note that additional settings are required in bootstrap.properties
Documentation of settings: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/appconfiguration/azure-spring-cloud-starter-appconfiguration-config
-->
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-cloud-appconfiguration-config-web</artifactId>
<version>2.1.0</version>
</dependency>
...
# Use this to enable or disable the cloud config, disabling it results in application.yaml beeing used.
spring.cloud.azure.appconfiguration.enabled=true
# Connection string to azure app configuration resource
spring.cloud.azure.appconfiguration.stores[0].connection-string= Endpoint=https://myofficeconfiguration.azconfig.io;Id=zUcT-l9-s0:PFYfW7WM0/Pz7WZOnH3v;Secret=JTB9myJqGekDAJ5m8Z1vjmkJZrPd88JbOEE3EqoqJYs=
# Configured filters for settings in the previous defined app configuration resource
spring.cloud.azure.appconfiguration.stores[0].selects[0].key-filter = /mysampleservice/
spring.cloud.azure.appconfiguration.stores[0].selects[0].label-filter = Sample
spring.cloud.azure.appconfiguration.stores[0].selects[1].key-filter = /notificationservice/
spring.cloud.azure.appconfiguration.stores[0].selects[1].label-filter = Sample2
bootstrap.yml/bootstrap.properties can still be used, they are no longer part of the base Spring packages.
Also, you want to use this doc for 2.0.0 and newer https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/appconfiguration/azure-spring-cloud-starter-appconfiguration-config.

Why does spring dependencies on Maven Central differ from actual local?

I am using spring-boot-starter-parent 2.3.9.RELEASE which according to Maven Central should give me spring-boot-starter-security 2.3.9.RELEASE which in turn should give me spring-security-config 5.3.8.RELEASE.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
However, when I look at Eclipse I see spring-security-config 5.3.5.RELEASE. I dont understand why my local is not reflecting spring-security-config 5.3.8.RELEASE for this artifact even though spring boot starter parent and spring boot starter security (both parents of this) are correctly showing 2.3.9.RELEASE. Also I'm not sure I understand what "Managed from 5.3.8.RELEASE" means in the screenshot. Could someone assist ?
This means that an entry in your <dependencyManagement> section of the POM overrode the version.
This may be a direct entry or a BOM, which you recognise by <scope>import</scope>.

ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

I have written a spring batch application using Spring boot. When I am trying to run that application using command line and classpath on my local system it is running fine. However, when I tried to run it on linux server it is giving me following exception
Unable to start web server; nested exception is
org.springframework.context.ApplicationContextException:
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
Below is the way I am running it:
java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log
Case 1:
#SpringBootApplication annotation missing in your spring boot starter class.
Case 2:
For non-web applications, disable web application type in the properties file.
In application.properties:
spring.main.web-application-type=none
If you use application.yml then add:
spring:
main:
web-application-type: none
For web applications, extends *SpringBootServletInitializer* in the main class.
#SpringBootApplication
public class YourAppliationName extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(YourAppliationName.class, args);
}
}
Case 3:
If you use spring-boot-starter-webflux then also add spring-boot-starter-web as dependency.
Probably you missing #SpringBootApplication in your spring boot starter class.
#SpringBootApplication
public class LoginSecurityAppApplication {
public static void main(String[] args) {
SpringApplication.run(LoginSecurityAppApplication.class, args);
}
}
The solution is:
I explicitly set the below property to none in application.yml file.
spring:
main:
web-application-type: none
My solution had to do with a bad dependency. I had:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
In my pom and I had to comment out the exclusion to get it working. It must look for this tomcat package for some reason.
In my case the issue resolved on commenting the tomcat dependencies exclusion from spring-boot-starte-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions> -->
</dependency>
You probably use this in your project:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
in which case you'll have to also add:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
and the magic happens :)
PS: that's because Spring will use by default web-MVC instead of web-flux when both are available
Adding following bean worked for me.
#Bean
public ServletWebServerFactory servletWebServerFactory() {
return new TomcatServletWebServerFactory();
}
I was running non web spring application using SpringApplication.run(MyApplication.class, args); without #SpringBootApplication annotation.
Annotate class public static void main with, for example: #SpringBootApplication
To convert an Spring boot wen application to standalone:
Either configure application.properties:
spring.main.web-application-type=none
Or Update application context with NONE web context.
ApplicationContext ctx = new SpringApplicationBuilder(MigrationRunner.class)
.web(WebApplicationType.NONE).run(args);
Using application context, you can get your beans:
myBean bean = (MyBean) ctx.getBean("myBean", MyBean.class);
bean.call_a_method(..);
I had this problem during migration to Spring Boot. I've found a advice to remove dependencies and it helped. So, I removed dependency for jsp-api Project had. Also, servlet-api dependency has to be removed as well.
compileOnly group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2'
As for me, I removed the provided scope in tomcat dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope> // remove this scope
</dependency>
I did right click on my project in IntelliJ IDEA then Maven -> Reload project, problem solved.
In case you're using IntelliJ and this is happening to you (like it did to my noob-self), ensure the Run setting has Spring Boot Application and NOT plain Application.
I was getting same error while using tomcat-jasper newer version
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>10.0.6</version>
</dependency>
I replaced with the stable older version it worked fine for me.
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.46</version>
</dependency>
Apart from the possible solutions in other answers, it is also possible that somehow Maven dependency corruption has occurred on your machine. I was facing the same error on trying to run my (Web) Spring boot application, and it got resolved by running the following -
mvn dependency:purge-local-repository -DreResolve=true
followed by
mvn package
I came onto this solution looking into another issue where Eclipse wouldn't let me run the main application class from the IDE, due to a different error, similar to one on this SO thread -> The type org.springframework.context.ConfigurableApplicationContext cannot be resolved. It is indirectly referenced from required .class files
Similar to the solution of making sure org.springframework.boot:spring-boot-starter-tomcat was installed, I was missing org.eclipse.jetty:jetty-server from my build.gradle
org.springframework.boot:spring-boot-starter-web needs a server be it Tomcat, Jetty or something else - it will compile but not run without one.
I wanted to run the WAR type spring boot application, and when I was trying to run the app as spring boot application I was getting above error. So declaring the web application type in application.properties has worked for me.
spring.main.web-application-type=none
Possible web application type:
NONE - the application should not run as a web application and should not start an embedded web server.
REACTIVE - the application should run as a reactive web application and should start an embedded reactive web server.
SERVLET - the application should run as a servlet-based web application and should start an embedded servlet web server.
In my case, the problem was I didn't had a Tomcat server separately installed in my eclipse. I assumed my Springboot will start the server automatically within itself.
Since my main class extends SpringBootServletInitializer and override configure method, I definitely need a Tomcat server installed in my IDE.
To install, first download Apachce Tomcat (version 9 in my case) and create server using Servers tab.
After installation, run the main class on server.
Run As -> Run on Server
I was trying to create a web application with spring boot and I got the same error.
After inspecting I found that I was missing a dependency. So, be sure to add following dependency to your pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Missing dependency could be cause of this issue
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
I encountered this problem when attempint to run my web application as a fat jar rather than from within my IDE (IntelliJ).
This is what worked for me. Simply adding a default profile to the application.properties file.
spring.profiles.active=default
You don't have to use default if you have already set up other specific profiles (dev/test/prod). But if you haven't this is necessary to run the application as a fat jar.
Upgrading spring-boot-starter-parent in pom.xml to the latest version fixed it for me.
In my case, I was using an TOMCAT 8 and updating to TOMCAT 9 fixed it:
<modelVersion>4.0.0</modelVersion>
<groupId>spring-boot-app</groupId>
<artifactId>spring-boot-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<tomcat.version>9.0.37</tomcat.version>
</properties>
Related issues:
https://github.com/spring-projects/spring-boot/issues?q=missing+ServletWebServerFactory+bean
https://github.com/spring-projects/spring-boot/issues/22013 - Spring Boot app as a module
https://github.com/spring-projects/spring-boot/issues/19141 - Application fails to load when main class extends a base class annotated with #SpringBootApplication when spring-boot-starter-web is included as a dependency
My problem was the same as that in the original question, only that I was running via Eclipse and not cmd. Tried all the solutions listed, but didn't work. The final working solution for me, however, was while running via cmd (or can be run similarly via Eclipse). Used a modified command appended with spring config from cmd:
start java -Xms512m -Xmx1024m <and the usual parameters as needed, like PrintGC etc> -Dspring.config.location=<propertiesfiles> -jar <jar>
I guess my issue was the spring configurations not being loaded correctly.
In my case, the gretty plugin (3.0.6) was still active. Gretty somehow influences the embedded tomcat dependency. Removing gretty fixed the error
Just comment the provided like below

Does supply a sample that could run it successfully is so difficult or just spring is so difficult to use naturally?

I git cloned spring-boot and wanted to start spring-boot-sample-web-ui, after importing it to intellij idea and run SampleWebUiApplication, first I met this error
Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
Then added this dependency
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
</dependency>
Run it again have another error again
Caused by: java.lang.ClassNotFoundException: org.springframework.http.client.support.BasicAuthorizationInterceptor
Does supply a sample that could start it successfully is so difficult or just spring is so difficult to use naturally?
The recommended approach if you are new to Spring / Spring Boot is to look at the Getting Started guides. I haven't had an issue yet with one of those and having them work properly. Based on the sample you listed I am guessing the Serving Web Content with Spring MVC would be a good one to look at.
I did try the spring-boot-sample-web-ui project in my local environment using Eclipse. I didn't run into any issues with it so it looks like it might be an issue with the way you imported / loaded the project in IntelliJ.
I resolved it by change parent in pom
<parent>
- <!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-samples</artifactId>
- <version>1.4.0.BUILD-SNAPSHOT</version>
+ <artifactId>spring-boot-starter-parent</artifactId>
+ <version>1.4.0.RELEASE</version>
</parent>

spring boot fails to run - IllegalAccessError on startup

I have a strange issue and that I have not been able to resolve. I am trying to use the sample JPA sprint boot (v0.5.0-M6) project as a starting point for an application I am writing. I grabbed the JPA sample and got that to run locally. I then proceeded to add my code into that project. I imported into eclipse and run as spring-boot. Then I get this error:
Exception in thread "main" java.lang.IllegalAccessError: tried to access class org.springframework.core.io.DefaultResourceLoader$ClassPathContextResource from class org.springframework.boot.context.embedded.EmbeddedWebApplicationContext
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getResourceByPath(EmbeddedWebApplicationContext.java:386)
at org.springframework.core.io.DefaultResourceLoader.getResource(DefaultResourceLoader.java:100)
at org.springframework.context.support.GenericApplicationContext.getResource(GenericApplicationContext.java:211)
at org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer.load(ConfigFileApplicationContextInitializer.java:192)
at org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer.load(ConfigFileApplicationContextInitializer.java:134)
at org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer.initialize(ConfigFileApplicationContextInitializer.java:121)
at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:403)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:287)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:749)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:738)
From what I can tell, this is the wrong application context, since I am not using XML configuration but annotations to drive the configuration. Spring boot is automatically selecting this one and I need to tell it not to use the above. At least that is what I think I need to do.
I did search here and in the spring.io forums but no one seems to have the same issue.
Question: What drives the selection of an application context with the auto configuration?
What should I be looking at to resolve the above issue? What else do I need to provide to here help debug the auto configuration issue?
TIA,
Scott
I got the same problem.
if you use maven check your pom.xml
remove conflict version in Spring Lib.
<properties>
<hibernate.version>4.2.0.Final</hibernate.version>
<mysql.connector.version>5.1.21</mysql.connector.version>
<spring.version>3.2.2.RELEASE</spring.version>
</properties>
i remove this line
<spring.version>3.2.2.RELEASE</spring.version>
and in maven dependency just
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
Hope this help.
I was facing the same problem, and solved fixing the referencing to the boot-starter-parent pom.
At the pom.xml file I used:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.5.0.M6</version>
</parent>
I have some urgent issues to fix now, so I didn't inspected this parent pom to see what's so important here, but I hope this can help you - don't forget to verify the version you're using!

Resources