How to get data for a particular log4j2 property from an alternative source (application.properties) - spring

Having a standard configuration for log4j2 and spring property-file on classpath application.property.
log4j2.xml
<Properties>
...
<Property name="APP_LOG_ROOT">${bundle:application:log.root.dir}</Property>
...
</Properties>
application.properties
...
log.root.dir=/opt/tomcat/logs
...
The data is read into the log4j2.xml correctly, but what if I want to get an alternative property when creating an artifact with maven and put diferent application.property:
mvn clean install -Dapplication.properties.path=file:/some_path/application.properties
?
After that, I can correctly read the new properties.
#Value("${log.root.dir}")
private String ololo;
but the log4j2 cannot do this on its own.

If you want to use any value from Spring's Environment in a Log4j2 configuration file, you need to use the Spring Boot Lookup.
After adding log4j-spring-boot to your classpath, you just need to replace
${bundle:application:log.root.dir}
with
${spring:log.root.dir}

Related

How to set a spring datasource url to a resources folder?

I have a hsqldb file in my project resources folrder "src/main/resources/mydb.data"
In my application.properites I need to set the path with spring.datasource.url=
How I can achieve that?
spring.datasource.url=.... ?
One solution would be to use Maven resource filtering. Just use something like this in your application.properties:
spring.datasource.url=jdbc:hsqldb:file:#basedir#/src/main/resources/mydb.data
Instead of using basedir you can define something like darasource-url in your pom.xml and use this property in your application.properties:
pom.xml
<project>
<!-- ... -->
<properties>
<datasource-url>jdbc:hsqldb:file:${basedir}/src/main/resources/mydb.data</datasource-url>
</properties>
<!-- ... -->
</project>
application.properties:
spring.datasource.url=#datasource-url#
Notice: Using the spring-boot-starter-parent as parent, you have to use #..# instead of ${..}, see 2.1.1. Automatic Property Expansion Using Maven in the Spring Boot How-to-Guides.

External log4j2.xml file path in application.properties

I am using log4j2 in my project and I have externalized the application.properties and log4j2.xml and I want to provide log4j2.xml file path in application.properties
log4j2.xml and application.properties are in the config folder in the same dir as a jar.
Here is my dependancy
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Directory Structure :
/tmp/myproj/conf/application.properties
/tmp/myproj/conf/log4j2.xml
/tmp/myproj/my.jar
Any help would be appreciated.
Spring Boot expects the log4j2-spring.xml configuration file to be on the classpath. However, you can store it in a different location and point to it using the logging.config property in application.properties.
Try providing below configuration to you application.properties. Here log4j2-spring.xml is on project classpath. If not then try giving your full path as (suppose) "C:/tmp/myproj/conf/log4j2.xml".
**logging.config=classpath:log4j2-spring.xml**
logging.level.org.springframework.web=INFO
logging.file=logs/spring-boot-logging.log
The configuaration in bold should work for you.
Then you need to configure your log4j2-spring.xml as per your requirement.
(simple and also for appenders, see here https://howtodoinjava.com/log4j2/log4j-2-xml-configuration-example/ ).
Note: I have used log4j2-spring.xml in place of you log4j2.xml. you can replace at your will.

How do I set a mybatis property in spring boot application.properties file?

I am working on a spring boot application which uses mybatis. My mybatis mapper xml has a SQL query which contains a database schema name that needs to be set from the application properties. I have a schema name and I want to reference it in my mapper.xml as ${schema-name}. I can do this in mybatis-config.xml - something like this:
<configuration>
<PropertiesSource url='my properties file"/>
<properties>
<property name='schema-name' value='${database.schema}'/>
</properties>
</configuration>
But I don't want to use mybatis-config.xml. I want to use the spring boot application.properties for mybatis configuration.
Is there a way to set the mybatis configuration property in spring boot application.properties? Can someone help me with this?
If you use the mybatis-spring-boot-starter 1.2.0, you can specify as follows:
mybatis.configuration-properties.schema-name=abc
or
mybatis.configuration.variables.schema-name=abc
If you use the mybatis-spring-boot-starter 1.1.1, you can specify as follow:
mybatis.configuration.variables.schema-name=abc
If you use the mybatis-spring-boot-starter 1.0.x, you cannot specify on application.properties. (please update to 1.1.1+)
Please try it.
Thanks.
In the application.properties file put:
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://host/table_name
spring.datasource.username=user
spring.datasource.password=password

Netbeans 8 won't reload static Thymeleaf files

I am using Spring Boot and Thymeleaf via Maven. I can't seem to get Netbeans to automatically re-deploy any of my Thymeleaf template files when I make changes. In order to see the changes I need to do a full clean/build/run. This takes way too long.
The templates are in src/main/resources/templates. I have an application.properties file in src/main/resources/ with spring.thymeleaf.cache=false and spring.template.cache=false.
I have "Compile on save", "Copy resources on save" and "Deploy on save" turned on in the project settings.
My maven build produces a war file that Netbeans deploys to Tomcat and I am using the annotation #EnableAutoConfiguration.
Netbeans does hot deploy changes to the Java classes but not for any of the static files in src/main/resources/.
Software in use:
Mac OS X 10.9.4
Java 1.8
Netbeans 8.0.1
Tomcat 8.0.12
Spring Boot 1.1.7
Thymeleaf 2.1.3 (via Spring Boot)
Any guidance is much appreciated.
An option would be to look into configuring Thymeleaf's FileTemplateResolver
To do that with Spring Boot, define a bean implementing the ITemplateResolver interface with the name defaultTemplateResolver, when present, Spring Boot would take it instead of its default, here is how that would be done, and assuming you have component scanning active so this configuration class will be picked up automatically:
#Configuration
public class ThymeleafConfiguration {
#Bean
public ITemplateResolver defaultTemplateResolver() {
TemplateResolver resolver = new FileTemplateResolver();
resolver.setSuffix(".html");
resolver.setPrefix("path/to/your/templates");
resolver.setTemplateMode("HTML5");
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(false);
return resolver;
}
}
The prefix should be a relative path that when added to your runtime working directory (cwd), would resolve to the templates directory. If you are unsure, set that to the full absolute path, but then there would be no point of the above bean. Since setting the spring.thymeleaf.prefix property to an absolute path would probably have the same effect.
Have been looking for a solution to my eclipse+thymeleaf+sprint boot reloading templates dynamically for a log while....
Finally I found this question here and spring.thymeleaf.cache=false and spring.template.cache=false fixed my issue.
Besides setting the Thymeleaf views as non-cacheable by ie. spring.thymeleaf.cache=false in your application.properties,
try explicitly defining the resource directory in your pom.xml:
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
...
</build>
To deal with that, the spring-boot-maven-plugin in the pom.xml should seems like this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
</dependencies>
</plugin>
And add this to your application properties:
spring.thymeleaf.cache=false
It usually works to Spring beans too.
Just to say this works nicely for me using an external instance of Tomcat:
Run Tomcat with JRebel or Spring Loaded javaagent as a VM option
Turn off "Compile on save", "Copy resources on save" and "Deploy on save"
Add a custom action in Netbeans that executes the compile goal
Run that when you want to see an update
http://wiki.netbeans.org/MavenBestPractices#Binding_Maven_goals_to_IDE_actions
https://github.com/spring-projects/spring-loaded
https://zeroturnaround.com/software/jrebel/quickstart/standalone/
Or you can you use the embedded tomcat with spring-boot-maven-plugin and Spring Loaded instead, then you won't need the compile action:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html
I had this problem too. I notice Netbeans reload automaticaly webpages that are in
/src/main/webapp/
You have to move all your templates from /src/main/resources/templates to this directory.
Also you have to change the spring boot property on application.properties file:
spring.thymeleaf.prefix=templates/
That works for me
I had this same issue on Netbeans 8.0.2 and Windows. I was building a WAR to be deployed to Tomcat, but I wanted to try out Spring Boot. It looks like newer versions of Netbeans might resolve this with the Spring Boot plugin or using Eclipse. It seemed nutty to swap IDEs over something tiny like this. I tried all of the suggestions I could find; spring loaded, caching properties, extending the TemplateResolver...I couldn't get any of them to work. I finally stumbled on this blog and following these instructions solved my issue.

How to access properties from settings.xml to spring.xml and pom.xml to spring.xml

I am new to maven spring.I have two questions.
How to access property set in settings.xml to spring.xml
How to accesss property set in pom.xml to spring.xml
Thank you.
You want to filter your resources, a maven term that means replacing property placehoders in one file with values from your maven pom. To do this you use the resources plugin in your build configuration.

Resources