0

I am developing a springboot app that has dependency on another springboot application. I want to include Most beans in the parent springboot app but one.

How can I exclude one springboot bean that the parent package has scanned without touching the ParentApplication class?

Ways I have tried but doesn't work:

1: using exclude filtering in my application class to filter out the particular bean class.

2: I also tried to exclude both the bean class and the parent configuration class.

3: add DisposableBean interface to the bean class I want to exclude and destroy it in run time.

below are my application starter configuration class and parent one.

my MyApplication.class: package com.myapp;

@ComponentScan(
    basePackages = {"com.parent",{my own packages..}},
    excludeFilters= {
    @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value= {TheClassToExclude.class}),
    @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value= {ParentApplication.class})}   
)
@SpringBootApplication(exclude=ParentApplication.class)
public class MyApplication{

public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
}

@PostConstruct
public void init() {
    System.out.println("App is initialized.");
}

}

my ParentApplication.class

    package com.parent;


    @EnableRetry
    @EnableScheduling
    @SpringBootApplication(exclude = { HibernateJpaAutoConfiguration.class })
    @ComponentScan(basePackages = {all the base package including the TheClassToExclude}
    @PropertySource({all resources})
    public class ParentApplication {

    public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }
    @PostConstruct
    public void haha() {
        System.out.println("configuration class created");
    }

The console prints out: "configuration class created", so ParentApplication is initiated by springboot for some reason, so is the Class I want to exclude.

Dong Hang
  • 9
  • 5
  • you have two `main` methods, which one do you actually run? in any case I would advice you to separate the common logic away (on the jar-level as separate project or at least a package level) from the initialization classes that are annotated with `@SpringBootApplication` – Vladimir L. Aug 28 '18 at 22:19
  • MyApp.class is my application's entry point. ParentApp.class is the parent application's entry point. my goal is to change the entry point from the parent class to mine and exclude one bean class. – Dong Hang Aug 29 '18 at 14:18

1 Answers1

0

just for reference - I think that since both classes are annotated with @SpringBootApplication, thus are both @Configuration classes and will take part in the automatic Spring component scan - and it is not clear which of this classes will be scanned first in order to "exclude" the other one - unless... you explicitly specify the entry point, and thus, the first SpringBootApplication class to load like here

You can see which classes get instantiated by Spring component scanning and in what order by setting logging.level.org.springframework=DEBUG in application.properties

hello_earth
  • 1,442
  • 1
  • 25
  • 39