自定义ApplicationConfigLoader

在spring-boot环境下,如果不希望通过baskserver.home目录存储默认的配置,而是希望利用spring的属性配置文件配置baskserver的系统属性,则可以利用ApplicationConfigLoader的实现类实现BaskServer相关的系统属性配置。

但是这样的话baskserver所需的系统表需要手动初始化到数据库中,初始化的办法参考:

自定义初始化

利用SPI定义一个ApplicationConfigLoader的实现类:

package com.basksoft.core.config.bootstrap;

import com.basksoft.core.config.bootstrap.ApplicationConfig;

public interface ApplicationConfigLoader {
    ApplicationConfig load();
}

记得在项目的resources目录下添加META-INF/services/com.basksoft.core.config.bootstrap.ApplicationConfigLoader文件,并且配置好ApplicationConfigLoader的实现类。

利用该实现类可以实现jdbc,jndi或自定义connection的数据源配置,这个和向导安装的数据源连接类型是一一对应的:

选择数据连接类型

ApplicationConfigLoader的参考代码:

package com.basksoft.baskserver.demo.config;

import com.basksoft.baskserver.demo.ToolSpring;
import com.basksoft.core.config.ApplicationConfig;
import com.basksoft.core.config.ConfigType;
import com.basksoft.core.config.bootstrap.ApplicationConfigLoader;

public class BaskConfigLoader implements ApplicationConfigLoader {

    @Override
    public ApplicationConfig load() {
        ApplicationConfig config = new ApplicationConfig();
        //可以读取自定义配置,例如spring相关配置信息
        config.getProperties().setProperty("baskserver.store.database.platform", "mysql");
        //ConfigType.jdbc
        config.setConfigType(ConfigType.jdbc);
        config.getProperties().setProperty("baskserver.store.database.driver", getProperty("baskserver.sampledb.driverClassName"));
        config.getProperties().setProperty("baskserver.store.database.url", getProperty("baskserver.sampledb.url"));
        config.getProperties().setProperty("baskserver.store.database.username", getProperty("baskserver.sampledb.username"));
        config.getProperties().setProperty("baskserver.store.database.password", getProperty("baskserver.sampledb.password"));
        config.getProperties().setProperty("baskserver.store.database.initialsize", "0");
        config.getProperties().setProperty("baskserver.store.database.maxTotal", "50");
        config.getProperties().setProperty("baskserver.store.database.minIdle", "2");
        config.getProperties().setProperty("baskserver.store.database.validationQuery", "select 1");
        config.getProperties().setProperty("baskserver.store.database.maxIdle", "10");

        //ConfigType.jndi
//        config.setConfigType(ConfigType.jndi);
//        config.getProperties().setProperty("baskserver.store.database.jndiname", "java:comp/env/sample");

        //ConfigType.connection
//        config.setConfigType(ConfigType.connection);
//        config.getProperties().setProperty("baskserver.store.database.classname", "com.basksoft.baskreport.demo.connectionprovider.TestConnectionProvider");

        return config;
    }
    private static String getProperty(String key) {
        return ToolSpring.getEnvironment().getProperty(key);
    }
}

ToolSpring参考代码:

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class ToolSpring implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;
    private static Environment env;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (ToolSpring.applicationContext==null) {
            ToolSpring.applicationContext = applicationContext;
        }
    }

    public static Environment getBean(Class<Environment> clazz) {
        return ToolSpring.applicationContext.getBean(clazz);
    }

    public static Environment getEnvironment() {
        return env;
    }
    public static void setEnvironment(Environment environment) {
        env = environment;
    }
}

其中ToolSpring的env变量可以利用BaskServerConfig初始化的时候注入:

@Configuration
public class BaskServerConfig implements WebMvcConfigurer,EnvironmentAware {

    @Bean
    public FilterRegistrationBean<BaskFilter> registerReportFilter() {
        ...省略
    }

    @Override
    public void setEnvironment(Environment environment) {
        ToolSpring.setEnvironment(environment);
    }
}

results matching ""

    No results matching ""