How to load Spring boot application static contain automatically while server is running? - spring-boot

I have created a spring boot application in which at location src/resource/static I have kept my all html, js and css files, Whenever I do any changes in these files I need to restart the spring boot application. Is there any easy way so that I will not require to restart my application.

As per the spring boot documentation, you need to move the static contents under web apps folder. Also make following settings
Projects->Build automatically
if you using eclipse
1) If you are using thyme leaf it would be very easy just add
spring.resources.static-locations
in application properties.
2) If you want auto restart try adding
spring.devtools.restart.additional-paths
to your
application.properties
References :
http://www.logicbig.com/tutorials/spring-framework/spring-boot/boot-live-reload/
http://www.concretepage.com/spring-boot/spring-boot-automatic-restart-using-developer-tools-with-maven
Using Thymeleaf:
http://blog.codeleak.pl/2016/12/thymeleaf-reload-templates-and-static-resources.html

Related

Multiple web applications in one tomcat instance start with a properties file from another applications

We have multiple web applications in one tomcat instance on 1 server, all running a spring-boot application inside.
Whenever we start tomcat and it starts to boot up all the spring-boot applications we mostly see that each application might use property files/settings from another application.
What especially happens is that we see it sometimes use the database information from other applications being used, resulting in a database which holds tables from other applications. This is scary since we might start a database migration or something.
We also see that the logs are written to the wrong project log file.
We define these settings using a application.properties like (or sometimes application-test.properties or application-secret.properties):
spring.datasource.username
spring.datasource.password
logging.file.name
Anyone have an idea why this is happening?
We found 2 possible causes for this behavior:
if Tomcat is started in a directory where application property files are present, or where application property files are placed in a /config subdirectory, like a WEB-INF/classes directory, these application property files are used by every Spring Boot application deployed in the tomcat instance. To fix this, make sure the Tomcat start script changes the working directory to a directory not containing application property files.
if the 'startStopThreads' attribute of the Tomcat Engine element in server.xml is set to values higher than 1, Spring Boot applications seem to occasionally and randomly use application property files of other Spring Boot applications deployed in the Tomcat instance. When 'startStopThreads' is set to 1, we don't see this behavior.

Where is the work directory of embedded tomcat for a spring boot / spring mvc application?

I would like to ask where is the location where you can see the work directory of a spring boot / spring mvc applicaiton. Specifically I want to know Transpiled JSP files converted to java files.
I can see the class files inside the target directory, but cannot find the transpiled jsp files. Is there a class call where I can see it in my environment? Like System.getProperty("user.dir") or new java.io.File(".").getCanonicalPath(). I tried both and it only showed me the code path.
Also, I know the Eclipse Server path. But in spring boot, it does not seem to run using a server instance of tomcat. So the following path is not existing?
The following directory is also blank.
projectworkspace/.metadata/.plugins/org.eclipse.wst.server.core/
The base dir is set via the property server.tomcat.basedir, see: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html
The work dir is derrived from there by appending /work.

Reload static content spring boot application

I am using Netbeans and I am developing my first web application using spring boot. I was keeping my HTML, js, CSS in "webapp" folder and then I refactored my project and I put all static content in /resources/static. Since then, I have to rebuild my project every time because the static content isn't reloaded.
Can I easily bypass this problem if I'll use browser-sync plugin for Gulp?
Add the following to src/main/resources/application.properties:
spring.web.resources.static-locations[0]=file:src/main/resources/static/
spring.web.resources.static-locations[1]=classpath:/static/
The "file:" causes the content to be reloaded on refreshing the browser,
see related issue.
Alternatively, the file resource locations can be discovered at runtime and added programmatically.
See also documentation and tutorial.
Note that prior to Spring Boot 2.4, the property was called "spring.resources.static-locations".
Normally the static content is copied to the build directory ( target if you are using maven) by the spring-boot plugin. You can find your files at {build-directory}/classes/static: These are the files that you should modify to reflect changes. You should also copy your changes to resources/static, because whenever you restart spring boot, the files are copied.
if an application.yml file is used for configuration, insert:
spring:
web:
resources:
static-locations[0]: "file:src/main/resources/static/"
static-locations[1]: "classpath:/static/"

properties file reload on change in server, Spring

i am using Spring 4.2.5 and jboss 7 server.
Is it possible to change in application.properties files and make server to reload changed data in application.properties file automatically on refresh without restarting server?
Please suggest on this. Any idea or steps or api to use anything to move forward.
Regards
Ideal way would be use spring cloud config
#EnableConfigServer. You will have separate config server pointed all the properties pointed. And a client will be able to access the properties. This allows you to change the properties on the fly in server with out need for any restart.
http://jeroenbellen.ghost.io/manage-and-reload-spring-application-properties-on-the-fly/

How to only auto configure the embedded container?

I'm trying bootify my app, which is XML configured. I'd love to have an embedded tomcat server that I can just run through the main method.
The simplest way to do it is to bootstrap the app using the existing XML config through #ImportResource("classpath:app-servlet.xml").
I cannot use auto configuration. We have certain circular dependencies that are not trivial to fix at this point.
The problem is that the embedded tomcat server only gets automatically configured if you use #EnableAutoConfiguration.
Is there a way to only auto configure the embedded server? I tried looking that Spring Boot's sources, namely EmbeddedServletContainerAutoConfiguration, including extending it and "running" it through my setup, but it only runs the customizers, not the ServletInitializer, therfore I'm getting an error "Root context already initialized".
Any help would be greatly appreciated.

Resources