pf4j / pf4j-spring

Plugin Framework for Spring (PF4J - Spring Framework integration)
Apache License 2.0
346 stars 105 forks source link

Hi author, Can I send a pull request to PF4J-Spring to support the Spring parent-child context ? #66

Closed Ted-Engineer closed 1 year ago

Ted-Engineer commented 3 years ago

Hi author: In my cases,many plugins need to use services provided by the host application,so I had the idea of supporting a Spring parent-child context

Example Code: ` Desc: Then SpringPlugin#getApplicaitonContext and SpringPlugin#createApplicationContext methods can take Parent-ApplicaitonContext as a input parameter.

public abstract class SpringPlugin extends Plugin {

private ApplicationContext selfApplicationContext;

public SpringPlugin(PluginWrapper wrapper) {
    super(wrapper);
}

/** Desc: <br>
 * 〈support Parent-ApplicationContext>
 *
 * @param parentApplicationContext  Parent-ApplicationContext

 * @return: org.springframework.context.ApplicationContext
 * @since : 1.0.0
 * @author : Ted
 * @date : 2021/6/22 13:04
 */
public final ApplicationContext getApplicationContext(ApplicationContext parentApplicationContext) {
    if (selfApplicationContext == null) {
        selfApplicationContext = createApplicationContext(parentApplicationContext);
    }

    return selfApplicationContext;
}

@Override
public void stop() {
    // close applicationContext
    if ((selfApplicationContext != null) && (selfApplicationContext instanceof ConfigurableApplicationContext)) {
        ((ConfigurableApplicationContext) selfApplicationContext).close();
    }
}

/** Desc: <br>
 * 〈support Parent-ApplicationContext〉
 *  You can ignore the Parent-ApplicationContext
 * @param parentApplicationContext Parent-ApplicationContext

 * @return: org.springframework.context.ApplicationContext
 * @since : 1.0.0
 * @author : Ted
 * @date : 2021/6/22 1:05 下午
 */
protected abstract ApplicationContext createApplicationContext(ApplicationContext parentApplicationContext);

} `

` SingletonSpringExtensionFactory Desc: I think this class is For Spring,so you don't have to use the PluginManager , we can replace SpringPluginManager with PluginManager

public class SingletonSpringExtensionFactory extends SpringExtensionFactory {

private final List<String> extensionClassNames;

private Map<String, Object> cache;

public SingletonSpringExtensionFactory(**SpringPluginManager pluginManager**) {
    this(pluginManager, true);
}

public SingletonSpringExtensionFactory(**SpringPluginManager pluginManager**, String... extensionClassNames) {
    this(pluginManager, true, extensionClassNames);
}

public SingletonSpringExtensionFactory(**SpringPluginManager pluginManager**, boolean autowire, String... extensionClassNames) {
    super(pluginManager, autowire);

    this.extensionClassNames = Arrays.asList(extensionClassNames);

    cache = new HashMap<>(); // simple cache implementation
}

@Override
@SuppressWarnings("unchecked")
public <T> T create(Class<T> extensionClass) {
    String extensionClassName = extensionClass.getName();
    if (cache.containsKey(extensionClassName)) {
        return (T) cache.get(extensionClassName);
    }

    T extension = super.create(extensionClass);
    if (extensionClassNames.isEmpty() || extensionClassNames.contains(extensionClassName)) {
        cache.put(extensionClassName, extension);
    }

    return extension;
}

} `

` SpringExtensionFactory Desc: I think this class is For Spring,so you don't have to use the PluginManager , we can replace SpringPluginManager with PluginManager /**

The pull request link :https://github.com/pf4j/pf4j-spring/pull/68