last day (14 days later) » 

12:35 PM
0
A: Profile methods not loading

M. DeinumYour configuration should look something like this. @Configuration @PropertySource("classpath:persistence-${envTarget:default}.properties") @EnableJpaRepositories(basePackages = {"dk.fitfit.campusfood.model", "dk.fitfit.campusfood.repository"}) public class PersistenceConfig { private static...

 
Your on unix so a . probably won't work try SPRING_PROFILES_ACTIVE instead of spring.profiles.active.
 
According to logger.info("env.getActiveProfiles(): {}", env.getActiveProfiles()) the profile is active.
 
Ok. But what happens, you get a stacktrace or is it still using the other DataSource?
 
Just using the wrong dataSource. When I'm testing locally I expect an exception to be thrown... but it's simply using the wrong dataSource. And same goes when testing remotely.
 
12:35 PM
Looking at your code at least all your package/component-scans are flawed. It isn't a pattern but a base package, remove the ending .*in those.
So it still is using H2 instead of PostgreSQL? You would expect an exception because you don't have PostgreSQL on your machine? Which profiles are active? Do you see in your logs that the OpenShiftPersistenceConfig is even loaded or not?
 
You mean @ComponentScan from ApplicationConfig.java? No, I would expect an exception because System.getenv("OPENSHIFT_POSTGRESQL_DB_URL") will be null locally. And yes, my logs shows: INFO : dk.fitfit.campusfood.config.PersistenceConfig$OpenShiftPersistenceConfig - OpenShiftPersistenceConfig loaded!
 
Tip instead of System.getenv simply use the Environment that will also look in the system properties. Doe you also see the datasource info message? Try adding @Primary to the dataSource() method in the OpenShiftPersistenceConfig config class or move the normal datasource to another configuration class like DefaultPersistenceConfig so that just one of them is loaded.
Another, unrelated thing, is that you seem to be mixing spring versions (4.0.6 and 3.2.10) probably due to some transitive dependencies. That will probably lead to some other errors later.
 
How should I use Environment... it's an interface? (sorry for being a n00b here) I see PersistenceConfig.dataSource() but not OpenShiftPersistenceConfig.dataSource()... as I'd love to see! :). Thanks ill fix the version issue.
I found no mention of 3.2.10. Where do you see that?
 
Just like you do the other properties env.getProperty("OPENSHIFT_POSTGRESQL_DB_URL").
If I look at the dependencies I see 3.2.10 version of spring-jdbc, spring-orm and spring-tx. Probably due to the fact that Sprnig Data JPA depends on those and you haven't overridden them. Tip use the Spring BOM to fix versions. Or even better the Spring IO Platform bom to have even more versions, that are garantueed to work together.
 
Regarding using Environment. I get a null pointer exception if I try to autowire it into my inner class and I can't declare it as static in my outer class... so how can I use it in my inner class?
Do I have dependencies elsewhere beside my pom.xml file? I'll definitely look into Spring BOM... both of them.
 
12:39 PM
Hmm you should be able to autowire it in your internal class. It is just as any other class (at least it should be).
 
Ok, ill look into that later. I'm still quite confused about where you see the older spring versions?
not to mention the whole dataSource issue :)
 
mvn dependency:tree
 
But where do I define them? Can I change something in my pom?
 
Add them to the pom, or just add one of the boms I pointed to so that versions are managed correctly.
Regarding the configuration move the default datasource to its own public static class just like the openshift one
and let that load if the @Profile("!openshift")
@Configuration
public static class DefaultPersistenceConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.build();
}
}
add that to your PersistenceConfig next to the OpenShiftPersistenceConfig and remove the dataSource() method.
that will make it work
 
 
3 hours later…
3:58 PM
Thanks a lot! Now I got some missing driver issue... but that's a different issue :)
I'm truly grateful for the information you shared. It means a lot to me and I learned several things along the way :)
 

last day (14 days later) »