Setting additional profiles before the run command results in error
ADarkDividedGem opened this issue · 3 comments
Firstly thanks for the great work you have done.
Recently I decided to set the active profile in my Spring Boot application programmatically so I didn't have to change the setting manually each time I changed the deployment.
This meant changing how the application was run which originally used the basic line:
SpringApplication.run(BasicApplication.class, args);
To the following:
import it.ozimov.springboot.mail.configuration.EnableEmailTools;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableEmailTools
public class BasicApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(BasicApplication.class);
String profile = "production";
if(SystemUtils.IS_OS_WINDOWS){
profile = "development";
}
application.setAdditionalProfiles(profile);
application.run(args);
}
}
The above code then returns the following error message:
APPLICATION FAILED TO START
Description:
Field mailSender in uk.co.freescanner.web.controller.ContactController required a bean of type 'org.springframework.mail.javamail.JavaMailSenderImpl' that could not be found.
- Bean method 'mailSender' not loaded because AnyNestedCondition 0 matched 2 did not; NestedCondition on MailSenderAutoConfiguration.MailSenderCondition.JndiNameProperty @ConditionalOnProperty (spring.mail.jndi-name) did not find property 'jndi-name'; NestedCondition on MailSenderAutoConfiguration.MailSenderCondition.HostProperty @ConditionalOnProperty (spring.mail.host) did not find property 'host'
Action:
Consider revisiting the conditions above or defining a bean of type 'org.springframework.mail.javamail.JavaMailSenderImpl' in your configuration.
Is it possible that by using application.run(args);
instead of SpringApplication.run(BasicApplication.class, args);
that it has thrown off the auto configuration some how?
Hi @ADarkDividedGem,
I much appreciated your gratitude. I would like to deserve more time to this project (e.g. I would like to migrate to Spring Boot 2.x), but I'm completely alone and due to other projects I can't afford everything alone at the moment.
Let's focus on the issue you are facing. I assume you use Spring Boot 1.5.x.
I have a few questions:
- Which OS are you running?
- If you run the app using the following code works?
import it.ozimov.springboot.mail.configuration.EnableEmailTools;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableEmailTools
public class BasicApplication {
public static void main (Stirng ... args) {
SpringApplication.run(BasicApplication.class, args);
}
}
- If you execute the following main works?
import it.ozimov.springboot.mail.configuration.EnableEmailTools;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableEmailTools
public class BasicApplication {
public static void main (Stirng ... args) {
SpringApplication application = new SpringApplication(BasicApplication.class);
application.run(args);
}
}
Finally, can you try something like
@Configuration
public class OSBasedApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
String profile = "production";
if(SystemUtils.IS_OS_WINDOWS){
profile = "development";
}
servletContext.setInitParameter("spring.profiles.active", profile);
}
}
Thanks for the quick reply, after coming back to the problem with fresh eyes and thanks to your examples, a new perspective, I got it working.
Turns out there was nothing wrong with your code, instead it was just my misunderstanding of how profiles worked. After reading the documentation on Profile-specific Properties I assumed you needed to rename application.properties
to application-default.properties
for it to be recognised as the default properties file when adding additional profiles.
Once I renamed it back to application.properties
the defaults properties for spring.mail.
were loaded back in and the application ran fine.
Sorry for taking up your time, like you I am also working solo on my project so I appreciate all the help I can get.
You can actually have one property file per profile or a single one that is multiprofile (and I think also a common properties and then those for the different profiles).
Glad everything works.
Best
R