Unable to read values from profile in Spring 1.3.6 - java

I am using Spring-boot 1.3.6 . When I configure profiles,it's not able to read values from properties file under profiles folder. However the profile config works fine with Spring-boot 1.2.2.

spring boot external configurations
If you view the link you will see several ways you can use externalized properties in the order of importance.

Related

How to containerize an environment-dependent legacy application writen in Java

We have a legacy application written in Java using Apache Struts 1.x and Spring 2.x that we want to containerize.
The problem we have is in the way this project is configured. It is done through Maven properties and profiles (one for each environment) that are turned into properties files. These properties, at compile time, are placed inside the WAR.
What would be the correct way to create an image of this application without modifying the project code? That is, that somehow the configuration is externalized, for example, in environment variables. Maybe it should be in a volume?
So far what we have achieved is a two-stage Dockerfile, where it first compiles with Maven with a specific profile and then copies the WARs in the second stage from a Tomcat image. But doing it this way the generated Docker image is not environment independent, which is what we want to achieve.
Thanks!
Spring Cloud Config Server provides an HTTP resource-based API for external configuration (name-value pairs or equivalent YAML content). The server is embeddable in a Spring Boot application, by using the #EnableConfigServer annotation.
So will deploy the Spring Cloud config server on one container containing all the environment configurations - https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html
Once it's deployed, you can easily deploy your application docker image in different environments using a bootstrap.yml file that accepts the cloud-config server based on the profiles (dev/uat/staging/prod)
###
server:
port: 9092
spring:
application:
name: application-name
cloud:
config:
urI: REPLACE_CLOUD_CONFIG_URI (https://<spring_cloud_config_url>:8888/)
profiles:
active: REPLACE_PROFILE (dev/uat/staging/prod)
management:
endpoints:
web:
exposure:
include: refresh

intellij spring boot run cannot load application.yml automatically

my application.yml locate
my application.yml
spring boot run server port
Intellij spring boot run configuration
Hi.
My project is Intellij spring boot 1.5.7 Gradle Project
When Intellij spring boot run cannot load application.yml automatically.
but gradle bootRun is very well run&load.
Please suggestion me.
You should specify profile configuration. From your application.yml, you have multiple configuration separated by "---", this is used for multiple profile configuration for different spring profile. Try removing "---" and specify profile as following :
Multiple configuration is used when you have different configuration for different profile, as following:

Eclipse does not pass mail properties to Spring Boot after migration

I am migrating my Spring Boot application from version 1.5.7 to 2.0.0 and I noticed that it no longer takes mail properties from ENV variables for some reason.
I am using java.mail.Sender and have the following propeties in my application.properties file:
spring.mail.host=smtp.example.com
spring.mail.username=username
spring.mail.password=password
spring.mail.port=587
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.defaultEncoding=UTF-8
This is there just to mock the mail properties in tests. I am injecting the real ones using the same keys as ENV variables: spring.mail.host=smtp.google.com, etc.
But when I try to send the email, I see that it is still using smtp.example.com. I thought that ENV variables had higher priority than values from application.properties. Did something change? Everything worked fine in Spring Boot 1.5.7.
EDIT:
The following command works so it is definitely some problem with Eclipse:
SPRING_PROFILES_ACTIVE=development SPRING_MAIL_HOST=smtp.gmail.com SPRING_MAIL_USERNAME=xxx SPRING_MAIL_PASSWORD=xxx ./gradlew clean bootRun
What I don't understand is why the exact same configuration works, when I switch back to 1.5.7. It is also strange that when passign env variables via Eclipse run configuration, it works for profile. So some env variables are applied and some not...
I was able to recreate this issue. Created a Spring boot App with 1.5.X and injected Environment variables from Eclipse. Now, when I migrate to 2.X release, the environment variables are not getting injected.
On further analysis, found out this interesting thread
One of the Spring-boot developers made this comment
Hence my conclusion is when we are using 2.X release, there is one of the component within Spring-boot-parent which is making the spring boot maven plugin to fork it and run in a separate JVM. Thus, the environment variable is not getting passed.
That answers the question why profile value is picked-up from the environment section. Profile flag is always passed as an argument irrespective of whether the app runs in the maven JVM or a new one
To confirm this, you can add the config entries to the JVM argument tab like the one below
You will now be able to see the new values passed to spring boot
I don't know much about your configurations, but if the project structure is okay with correct dependencies and the application.properties exit under src/main/resources and your startup class annotated with #SpringBootApplication, it should work fine.
you can test if the application reads your properties file by injecting a variable String with annotation #Value inside any class and log or print it.
#Value("${spring.mail.host}")
private String host;
first Make sure your IDE is running on Java 8 or later version .
With Spring Boot 2.0, many configuration properties were renamed/removed and developers need to update their application.properties/application.yml accordingly. To help you with that, Spring Boot ships a new spring-boot-properties-migrator module. Once added as a dependency to your project, this will not only analyze your application’s environment and print diagnostics at startup, but also temporarily migrate properties at runtime for you. This is a must have during your application migration:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
<scope>runtime</scope>
</dependency>
runtime("org.springframework.boot:spring-boot-properties-migrator")
Note Once you’re done with the migration, please make sure to remove this module from your project’s dependencies.
For more information follow this link
Spring Boot 2.X migration guide

What's the difference between -Drun.profiles and -Dspring.profiles.active on Spring?

I'm trying to understand the difference in Spring between -Drun.profiles and -Dspring.profiles.active.
Another answer in SO does not explain so much about the difference.
In my tests, both of them can be used to select a profile:
mvn spring-boot:run -Drun.profiles=prod
or
mvn spring-boot:run -Dspring.profiles.active=prod
So, what's the difference?
spring.profiles.active is one of the properties that Spring Boot applications support out of the box. Its used to specify at the level of Spring Boot application which profiles should be run.
Spring Boot supports many different properties, a full list can be found here.
Now, you won't find run.profiles among these properties, because its just a property that Spring Boot Maven plugin supports (and yes, it 'translates' it to the list of profiles to be used as well, so these properties might look similar), but the point is that -Drun.profiles will only work if you start the spring boot application with Maven plugin.
In production, however, the chances are that there won't be Maven at all, and the application will run as is (as a big jar) or even packed as Docker image or something. So for non maven-plugin usage you should use spring.profiles.active
The last point, that even in Maven --spring.profiles.active can be used, but it doesn't work out of the box. You should pass this parameter like this:
mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=production"
See this item in Github.
Hope this clarifies the differences between the two.

How to correctly create a simple Spring project running in the console using the last STS version (3.8.4.RELEASE)?

today I downloaded the last STS version (3.8.4.RELEASE) and I have some doubts because it seems to me that something is changed.
I have to create a simple batch application (running in the console).
It seems to me that to create this kind of project in this new realease I have to do:
File --> Spring Legacy Project --> Simple Spring Maven
It seems to me that there is no other way to create a simple console project. Why this kind of project are putted under the Spring Legacy Project? Can I do the same thing in a more modern way?
Another doubt is related to the Spring Version into the pom.xml file:
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
Why the default version is the 3.2.3.RELEASE? Can I replace it with the more m odern Spring 4^
As its name says "Spring Legacy Project" is a legacy functionality. It create old Spring projects.
Spring now is promoting Spring Boot applications, these applications have everything you need configured by default. You only need to add the dependencies that you need.
Now you need to use:
File -- > New -- Spring Starter Project
On the first view you configure the project. Where is located, maven or gradle, etc.
On the second view you choose the libraries that you will use in your porject. You choose if it is a web project, or if you need jdbc or jpa, or batch processing.
In your case, if you don't need a web application, you just don't added to the project.
You can see more about Spring boot here
https://projects.spring.io/spring-boot/

Categories