I think there is a misunderstanding of what @Profile does. Beans marked with @Profile are loaded only when this profile is active, but all other Beans (without @Profile ) are still always loaded regardless of the selected profile.
I see several ways to solve this problem:
1) Mark all those Beans with @Profile("dev") also with @Primary , so Spring knows which one to choose when two Beans of the same type are loaded (since you don't want to use the production profile).
2) Note the Beans that should not be loaded when the dev profile is active with @Profile("!dev") - applicable only for Spring 3.2 and higher (see https://github.com/spring-projects/spring-framework / commit / bcd44f3798ed06c0704d2a3564b8a9735e747e87 ).
Or...
3) Use a production profile and simply activate it, for example, in the web.xml (something that you probably do not use locally).
Just create some @Configuration classes and mark the entire class with a profile (this will also help tie everything together). A typical example for a database. Create one configuration class for the production database (something with JNDI and Oracle) and one for local development and testing (HSQLDB).
You mark the JNDI configuration class with @Profile("production") and the other with @Profile("dev") - there is no need to mark separate beans, just separate them logically among two different @Configuration classes.
This worked very well for us, also in combination with integration testing.
source share