Location>code7788 >text

[Java] Plug-in extension system, one of the three core components of the Solon framework

Popularity:594 ℃/2025-01-23 23:36:34

1. Solon’s three core components

core components illustrate
Plugin plug-in extension mechanism Provide an extended system of "coding style"
Ioc/Aop application container Provides an automatic assembly system based on injected dependencies
Context+Handler universal context processing interface Provide "open processing" adaptation system (commonly known as three-in-one)

2. Solon Plugin plug-in extension mechanism

Several Java extension mechanisms:

Extension mechanism describe Features experience style applicability
Java SPI Java comes with In units of interfaces Configuration style Applicable to all Java ecosystems (most universal)
Spring Factories Provided by the Spring framework in components Configuration style Applicable to the Spring ecosystem
Solon Plugin Provided by Solon framework in modules coding style Applicable to Solon Ecosystem

Solon Plugin is an "enhanced" mode of Java SPI that emphasizes coding style. The plug-in module meta-information configuration will declare an implementation class of the Plugin interface. When the application is started, the meta-information directory will be scanned to discover all declared plug-in implementations.

Plugin interface definition:

public interface Plugin {
     //start up
     void start(AppContext context) throws Throwable;
     //Pre-stop
     default void prestop() throws Throwable{}
     //stop
     default void stop() throws Throwable{}
 }

Meta information configuration declaration of Plugin implementation class: withMETA-INF/solonis a dedicated directory; usepropertiesFormat; configure the implementation class and priority of the plug-in.

# META-INF/solon/{packname}.properties

 ={PluginImpl} #Plug-in implementation class configuration
 =1 #Plug-in optimization level configuration.  The larger the value, the higher the priority. The default is 0.

3. Solon Plugin plug-in example

Take a plug-in related to data caching and transactions as an example to implement the overall assembly (coding style) in modules:

public class DemoSolonPlugin implements Plugin {
     @Override
     public void start(AppContext context) {
         if (() != null) {
             //Add transaction control support
             if (().source().isAnnotationPresent()) {
                 //Add annotation interceptor
                 (, , 120);
             }

             //Add cache control support
             if (().source().isAnnotationPresent()) {
                 //Add annotation interceptor
                 (, new CachePutInterceptor(), 110);
                 (, new CacheRemoveInterceptor(), 110);
                 (, new CacheInterceptor(), 111);
             }
         }
        
         //Automatically build data source according to configuration
         ();
     }
 }

Plug-in application instructions:

@EnableTransaction
@EnableCaching
public class App {
    public static void main(String[] args) {
        (, args);
    }
}

@Component
public class DemoService {
    @Cache
    public String test() {
        return new Date().toString();
    }
    
    @Tran
    public void post() {
        ...
    }
}