Wrapping the application.name value with '#', what does it mean? - spring

What does adding the '#' to name value of application.properties to the beginning and to the end mean?
I couldn't find usage of something like this. I checked the Spring docs as well, but I couldn't find it. Is this usage a generic thing for programming or specific to application.properties of Spring?

Please go through the documentation
Rather than hardcoding some properties that are also specified in your
project’s build configuration, you can automatically expand them by
instead using the existing build configuration. This is possible in
both Maven and Gradle.
The format you mentioned is for Maven
You can automatically expand properties from the Maven project by
using resource filtering. If you use the spring-boot-starter-parent,
you can then refer to your Maven ‘project properties’ with #..#
placeholders
Update
With Spring Boot Actuator dependency added to pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
and
info endpoint exposed (for http : management.endpoints.web.exposure.include=info )
a quick verification of this can be done.
Add the following property to pom.xml
<properties>
<app.info.test>Test Value</app.info.test>
</properties>
and following entries in the application.properties file
info.app.name= Test App
info.app.java.source=1.8
info.app.test=#app.info.test#
Hitting http://localhost:8080/actuator/info will give the following response
{"app":{"name":"Test App","java":{"source":"1.8"},"test":"Test Value"}}

Straight Forward answer is the value which you store with #name# in application.properties are initialized when your project build start (based on same parameter name you pass with command).
It's used when you want to pass value of that variable at time of build
or value which are different based on environment.If you don't do that then it value becomes fixed.
when you're want to pass some parameter which are constant or repetitively use in your application like mail configuration or AWS configuration, version or etc. that things define in application.properties file.

Related

Spring boot tests - run tests 2 time with different application-xxx.properties each time

I have a spring boot app that has tests for database stuff and I'm supporting mysql and mssql.
I have src/text/resources/application-mysql.properties and src/text/resources/application-mssql.properties
What environment variable can I set when I run my tests to tell Spring which test properties file to use?
Property files in the format application-*.properties are activated using Spring Profiles. Same thing for YAML files, by the way! It is important to know that application.properties is still loaded first and any profile-specific properties will overwrite previously loaded properties (kind of the whole point of Spring Profiles).
There are multiple ways to enable profiles:
To answer your question, you can set the SPRING_PROFILES_ACTIVE environment variable to enable profiles. For example, export SPRING_PROFILES_ACTIVE=mysql. You can also specify multiple profiles (and they are loaded in the same order) by separating them with a comma: export SPRING_PROFILES_ACTIVE=localdefaults,local.
You can also use the JVM parameter, spring.profiles.active. The value follows the same format as that of the environment variable. For example, -Dspring.profiles.active=mysql.
You can use the #ActiveProfiles annotation on your test class. For example:
// Other annotations...
#ActiveProfiles("mysql")
public class MyTest {
If you want to enable profiles during a build, you can set the spring.profiles.active property in Maven. For example:
<profiles>
<profile>
<id>mysql</id>
<properties>
<spring.profiles.active>mysql</spring.profiles.active>
</properties>
</profile>
...
</profiles>
Here's a weird one I recently learned. You can also set active profiles with the spring.profiles.active in a properties file. I imagine this has its uses, but have never used this approach.
Read more about everything I have covered:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.properties-and-configuration
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/ActiveProfiles.html

Cannot find template location :( please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)

I created a spring boot application and while running it I am getting this warning.
2022-10-27 00:35:12.520 WARN 11512 --- [ restartedMain] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)
Here, you can see my application.properties file
spring.datasource.url=jdbc:mysql://localhost:3306/springbootblogapp?
createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
spring.jpa.open-in-view=false
Even though there were some related questions here, the provided answers for them are not working for me, because the warnings mentioned there were a little bit deviated from the warning which I got.
Please help me.
My initial comment, transferred to an answer:
Here is the text of the warning message shown in your question:
2022-10-27 00:35:12.520 WARN 11512 --- [ restartedMain] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)
Did you try what the warning message suggests - for example, by adding:
spring.thymeleaf.check-template-location=false
to your application's config? Or by creating the directory templates mentioned in the warning message (and adding a template there)? By default this directory is expected to be created in the resources location, so that it is on the classpath of the application at runtime.
Update based on your follow-up comment:
Why do you get this warning?
When you create a Spring Boot application, you can choose various additional dependencies (for example by using Spring Initializr).
One such additional dependency is Thymeleaf.
For Maven (assuming you are using Maven), that is the following artifact in your POM:
<artifactId>spring-boot-starter-thymeleaf</artifactId>
There is no point in choosing this option if you don't set up the basic location where you are going to store your Thymeleaf templates.
If you choose to configure the option:
spring.thymeleaf.check-template-location=false
then you are telling Spring not to bother checking if there is a valid configured location for your Thymeleaf templates.
So, even if there is not a valid configured location, you won't see that warning.
If you decide you do want to use Thymeleaf after all, you will need to fix this, by providing that default folder templates on the runtime classpath - or by specifying a custom location via your properties file.
You may see similar warnings (or even errors) with other Spring Boot dependencies you have chosen. For example, if you choose to include the JDBC API, but do not configure a valid data source, then I believe that causes an error (if I recall).

How to pass value to maven pom.xml at rum time from java file?

I have a java file where a variable taken value at run time.I search for a service using web service discovery and keep its url in a variable.
Now I need to pass this value to pom.xml.
abc.java has code with
String url= http://xx.xx.xx.xx:55939/ABCDevice?wsdl
Pom.xml is:
<wsdlOptions>
<wsdlOption>
<wsdl>url</wsdl> <!-- get urlvalue from java file -->
<wsdlLocation>classpath:com/admin/discovery/ABCService.wsdl
</wsdlLocation>
</wsdlOption>
</wsdlOptions>
In wsdl i want to pass string value "http://xx.xx.xx.xx:55939/ABCDevice?wsdl" which is determined only after run time.
How can i do so ?
I don't consider this as an Apache Maven specific issue, but a general Java issue (Maven probably made you aware of it).
During build-time you have no idea what the url should be. Depending on the type of application you have several options:
JNDI (in case of a webcontainer)
A properties file on a predefined location
System properties
As arguments (in case of executable jar)
Adjust web.xml before deploying (some webcontainers can help you with this)
...
In you use a framework like Spring there are easy ways to inject one the options above.

Maven PDF Plugin custom properties are not filtered in the generated PDF

I am trying to generate a PDF and the main generation works fine, but I face the issue trying to use custom defined properties.
I do have the my properties defined in pom.xml as documented, but when I use them in the content files in the final result I don't see them substituted, but staying with the variable i.e ${myProperty}.
e.g. I have the property defined in pom.xml
<properties>
<myProperty>My other value</myProperty>
</properties>
If I use the same property in pdf.xml it is being properly substituted(i.e on the title tah), but if I
set it in an .apt.vm file as ${myProperty} it doesn't.
The apt file is also with .vm extension as documented on the site.
On the other hand system properties as ${project.name} work fine.
Do you have an idea what am I missing?

Property-placeholder default value separator

Small question.
We use Spring 3.1 and seem to be having an issue with the value separator (The default value that is).
<context:property-placeholder ignore-resource-not-found="false" ignore-unresolvable="false" location="${app-conf-base-path:classpath:}environment/app-conf-${spring.profiles.active:test}.properties" />
We filter the properties being deployed based on maven environment profiles. If no maven environment profile was provided all environment property files will loaded in the war and put on the classpath.
The actual environment specific settings are loaded at runtime using spring profiles. The part that is giving us an issue though is ${app-conf-base-path:classpath:} because it seems he can not resolve this. I believe this is because the placeholder is split using the last occurrence of the default value separator.
Why we want this is because we want him to look on the app-conf-base-path unless this is not defined as JVM parameter. In that case he should look in the classpath.
I tried:
Placeholder configurer with a value separator == '?' with no avail.
Anyone got an idea of an alternative?
I solved the problem by making sure external configuration files are added to the classpath of the server when loading it's classloaders. This way I don't need a placeholder to point to a file:// type location.
I added a export line to the setclasspath.sh of tomcat to add the external file to the environment variable CLASSPATH.

Resources