Check if addon is loaded

I am creating an add-on and i want to check if another add-on is loaded, so i can provide extra functionality.

Is there a “Jmix way”, beyond the classic “Java way”, to check if an add-on is loaded?

A utility class maybe.

Hi!

Thank you for your question.
This is not a “Jmix way”, but it is a way that we often use.

Let’s imagine that you want to check in Add-on 1 whether Add-on 2 is loaded.

  1. Add a compileOnly dependency for Add-on 2 to Add-on 1:

    compileOnly 'my.addon.name:my-addon-name-starter'

  2. Then create a spring bean in Add-on 1 that will only be created when the Add-on 2 is loaded. You can use the @ConditionalOnClass annotation to do this. The value of this annotation can be set to Add-on 2 configuration class, since it must exist if the add-on is loaded:

    @Component("addon1_MyExtraLogicBean")
    @ConditionalOnClass(Addon2Configuration.class)
    public class MyExtraLogicBean {
    
        private AnyBean1 anyBean1;
        private AnyBean2 anyBean2;
    
        public MyExtraLogicBean(AnyBean1 anyBean1, AnyBean2 anyBean2) {
            this.anyBean1 = anyBean1;
            this.anyBean2 = anyBean2;
        }
    
       // methods
       }
    
  3. In this bean, you will be able to use classes from Add-on 2, as they will definitely be loaded. Next, use optional autowiring for this created bean in places where you want to create an extension point for functionality:

       @Autowired(required = false)
       MyExtraLogicBean myExtraLogicBean;
    

This is not the only correct way. You can use the method that is convenient for you.

Best regards,
Dmitriy

2 Likes