membersound
membersound

Reputation: 86915

How to explicit include a component to be autowiried in Spring?

I created an api toolkit with lots of classes that I want to share to several projects. Some projects run in a tomcat environment, some are just comand line tools.

Thus, I'd like to include some specific classes on the tomcat projects, and exclude them in the cmd tools. Eg:

package my.base;

@Component
@WebListener
public class OrderlyShutdown extends ContextLoaderListener {

}

This is of course only to be included in tomcat. Is there a way I can tell my @Configuration class to include or exclude certain classes (not by package name!)?

Upvotes: 0

Views: 174

Answers (1)

axtavt
axtavt

Reputation: 242786

  • Since Spring 3.1 you can use @Profile to include or exclude certain components based on a list of active profiles:

    @Component
    @WebListener
    @Profile("tomcat")
    public class OrderlyShutdown extends ContextLoaderListener { ... }
    
  • Since Spring 4.0 you can also use @Conditional for more fine-grained control

Upvotes: 1

Related Questions