I'm using spring-cloud-aws-autoconfigure:2.1.0.RELEASE to connect to AWS. However when the app is running in an enviromnent other than AWS, I don't want the auto configuration to take place.
I tried turning off the auto configuration as suggested here and here with java configuration class, and also with spring.autoconfigure.excludes property in my yml file like this:
spring:
autoconfigure:
exclude:
- org.springframework.cloud.aws.autoconfigure.context.ContextCredentialsAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.context.ContextInstanceDataAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.messaging.MessagingAutoConfiguration
But none of those solutions seems to work. The autoconfiguration still takes place and consequently, the app fails to start.
Found a solution: I added this directly to my main application class:
import org.springframework.cloud.aws.autoconfigure.context.*;
#SpringBootApplication
#EnableAutoConfiguration(exclude = {
ContextCredentialsAutoConfiguration.class,
ContextInstanceDataAutoConfiguration.class,
ContextRegionProviderAutoConfiguration.class,
ContextResourceLoaderAutoConfiguration.class,
ContextStackAutoConfiguration.class,
MailSenderAutoConfiguration.class,
})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Found solution: I excluded every class I found in the autoconfiguration jar:
spring:
autoconfigure:
exclude:
- org.springframework.cloud.aws.autoconfigure.cache.ElastiCacheAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.context.ContextCredentialsAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.context.ContextInstanceDataAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.jdbc.AmazonRdsDatabaseAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.mail.MailSenderAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.messaging.MessagingAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.metrics.CloudWatchExportAutoConfiguration
Related
I have a files structure like this:
- main/resources
- application.yml
- externalApi.yml
- application-dev.yml
- externalApi-dev.yml
- test/resources
- application-test.yml
- externalApi-test.yml
Now in ApiWebClient class I want to use this additional file "externalApi.yml" based on active profile. I used #PropertySource like this:
#PropertySource(value = "classpath:externalApi-${spring.profiles.active}.yml", factory = YamlPropertySourceFactory.class)
public class ApiWebClientImpl implements ApiWebClient {
and everything looks fine, application starts. The problem starts in the tests:
#SpringBootTest(classes = TestApplication.class)
#EnableConfigurationProperties
#ConfigurationPropertiesScan(basePackages = "com.xyz.testapplication")
#ActiveProfiles(value = "test")
public class ApiWebClientTest {
When I ran it with maven (with "test" profile or without it) like this: mvn clean install -Ptest I've got error message that says
java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in value "classpath:externalApi-${spring.profiles.active}.yml
So I've added it manually (#ActiveProfiles somehow does not work):
#SpringBootTest(classes = AdapterTestApplication.class, properties = "spring.profiles.active=test")
and this is working properly but I don't want to update all of the spring boot tests with this part properties = "spring.profiles.active=test". What if I will have like 200 tests? How to do it automatically? Why it does not take proper config file when I run it? Maybe there is way to get rid off ${spring.profiles.active} in annotation?
I am trying to load 2 properties file in spring boot.
One of them contains the metadata (Database connection and other such properties). The other contains business logic (mapping between upstream and downstream Entity. This mapping is different in Dev and Prod, hence can't have a single resource file for these).
I want to use Spring Profiles for different environments (Dev, Stage, Prod).
So, I created 3 different folders in src/main/resources 1 for each environment.
Using spring profies, I am aware how to have env specific application-env.properties file. However, I am unable to move forward on how to use the same for my use case.
PS : Not adding any code snippet, because the question doesn't require one.
Here's an example from the docs:
$ java -jar myproject.jar --spring.config.location=\
optional:classpath:/default.properties,\
optional:classpath:/override.properties
You could also define this in your code before starting Spring Boot:
public static void main(String[] args) {
System.setProperty("spring.config.location", "optional:classpath:/default.properties,optional:classpath:/override.properties");
SpringApplication.run(Application.class, args);
}
To use custom prefixes for your app specific properties you can define #ConfigurationProperties class(es):
#Data
#ConfigurationProperties(prefix = "app.mapper")
public class MapperProperties {
private String foo;
}
and use it in any component:
#Component
#RequiredArgsConstructor
#EnableConfigurationProperties
public class YourComponent {
private final MapperProperties properties;
}
I have this configurations which needs to be used for a spring boot application.
server.port=8085
server.servlet.context-path=/authserver
#data source
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=<url>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
By default spring-boot picks up up the application.properties file located in src/main/resources/
I want to alter this path and direct spring boot to different application.properties file
I can achieve this using
java -jar app.jar --spring.config.location=classpath:/another-location.properties
Is there any any alternative solution I can achieve this without passing args through command line?
I was using this
#PropertySource("file:C:\Users\test\.test\test.properties")
#ConfigurationProperties(prefix = "spring")
public class Configuration {
private String ddlAuto;
private String url;
private String username;
private String password;
private String driverClassName;
}
in my Main class
#SpringBootApplication
#EnableConfigurationProperties(Configuration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
There after I tried executing the app commenting out all datasource properties in application.properties under src/main/resources/
But it keeps giving me the error mentioned bellow and application fails to start
I was referring this tutorial : https://www.mkyong.com/spring-boot/spring-boot-configurationproperties-example/
but as it's mentioned I get this error when i start the spring boot application
***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target org.springframework.boot.context.properties.bind.BindException:
Any help on this would be appreciated
The recommended way to have externalized properties is to use the spring.config.location system property, by starting your application like so:
java -jar -Dspring.config.location=/path/to/my/file.properties app.jar
The reason for this is that you don't add coupling between your code and your filesystem hierarchy.
Before Spring Boot 2.0 this property is additive, meaning that it will complement the default locations. After Spring Boot 2.0, spring.config.location replaces the default locations (e.g. classpath src/main/resources/application.properties). To keep the additive behaviour after 2.0, use spring.config.additional-location instead.
Please see here for official documentation on this matter.
I am able to make it work properly on Spring Boot 2.1.2.RELEASE. This is what I have done:
I have a test.properties in my /tmp folder with the following content:
test.myprop=hello
I also have the usual property file in the resources folder:
myprop=world
I have created a class for the custom property file:
#Configuration
#PropertySource("file:/tmp/test.properties")
#ConfigurationProperties(prefix = "test")
public class TestConfig {
private String myprop;
public String getMyprop() {
return myprop;
}
public void setMyprop(String myprop) {
this.myprop = myprop;
}
}
And then in my main class I have enabled to configuration properties:
#EnableConfigurationProperties(TestConfig.class)
#SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
Now I have this test controller:
#RestController
public class TestController {
#Value("${test.myprop}")
private String externalisedProp;
#Value("${myprop}")
private String prop;
#GetMapping("test")
public void test() {
System.out.println("externalised: " + externalisedProp);
System.out.println("not externalised" + prop);
}
}
Which, once called, is properly printing:
externalised: hello
not externalised: world
My TestConfig class is in the same package as the MyApp main class.
What I have done is very similar, almost identical, to your solution, are you sure your path is correct? Also, I can see that the content of your property file is not matching what you have in your config class, the prefix is different. Maybe that is the problem?
Edit:
I have tried to remove the #Configuration annotation from my property class (which you do not have as well) and it is not able to pick up the externalised properties anymore. The error is different though but you should try to add it.
Hi I have been using a custom resource loader with spring boot like below for quite some time
public static void main(String[] args) {
LOG.info("Starting up....");
SpringApplicationBuilder builder = new SpringApplicationBuilder(Service.class);
SpringApplication app = builder.application();
// TODO see if ProtocolResolver can be used here
app.setResourceLoader(new EnhancedResourceLoader());
app.run(args);
}
and my custom resource is something like
#Aws
#Configuration
#PropertySource({ "s3://${cloud.aws.vpc-name}/creds/${cloud.aws.stack-name}/testprops.properties", })
public static class Config {
}
This used to work fine before I did the upgrade . Now seems the custom s3 url doesn't get captured in my custom resource loader . Have there been any changes or is there a different way now to do this .Please help
AS of now to fix as per https://twitter.com/ankinson/status/821401622001684481 I have removed the dependency for spring boot devtools and it works .Guess will test the same in the next 1.5.x release if that fixes it .
I am creating simple Spring apps using Maven and have 2 config and properties. The hierarchy is:
- package.main
- App.java
- AppConfig.java
- app.properties
- package.main.model
- ModelConfig.java
- model.properties
App.java
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(applicationContext.getEnvironment().getActiveProfiles()[0]);
}
AppConfig.java
#Configuration
#Import(ModelConfig.class)
#PropertySource("classpath:/package/main/app.properties")
public class AppConfig {}
app.properties
spring.profiles.active = prod
ModelConfig.java
#Configuration
#PropertySource("classpath:/package/main/model/model.properties")
#ComponentScan
public class ModelConfig {}
model.properties
spring.profiles.active = dev
Why the model.properties is override the app.properties (the result is dev)?
How to make application.properties like in Spring Boot that cannot overrode by new properties?
it is by the order of beans defined being ModelConfig loaded it will cause the active profile to be set for 'dev'.
BTW, looking at your setup, configuring the active profile of beans via properties file like this not the good idea though, spring.profiles.active should be specified in
application.properties
file, not to loaded via separate property files, so when application starts it loads the bean correctly for the profile
or via command line arguments when you invoke
-Dspring.profiles.active
or programmatically
context.getEnvironment().setActiveProfiles("live");
some good examples here