Eric B.
Eric B.

Reputation: 24451

Is there a way to use Spring @Profile annotation at a package level?

I'm trying to put all my bean definitions for a specific profiles together, and would rather not push them all into one giant AppConfig.java class. I was wondering if there was a way to annotate at a package level using package-info.java and have all configuration files within that package inherit the profile.

I've tried the following in package-info.java:

@Profile("test")
package com.system.configuration.test;
import org.springframework.context.annotation.Profile;

But the @Configuration classes within the package seem to be used whether it is the "test" profile or not.

Is the only choice to annotate each class individually?

Upvotes: 5

Views: 1849

Answers (1)

Jakub Kubrynski
Jakub Kubrynski

Reputation: 14149

You can do it in different way by creating separate @Configuration classes for different profiles:

@Configuration
@Profile("test")
@ComponentScan("com.system.configuration.test")
public class TestProfile {
}

And then on your main configuration class you need to do imports:

@Configuration
@Import(TestProfile.class)
public class MainConfiguration {
}

Upvotes: 2

Related Questions