可以使用ApplicationContextHolder的getApplicationContext().getBean()方法加载类方法实现策略分配
直接上代码
1、定义返回类型,满足业务需求的对象。
a)提取公共属性到父类
@Data
public class SchemaStatParserVO {
//定义具体的属性
}
b)定义子类,定制化需求对象
public class ClickSchemaStatParserVO extends SchemaStatParserVO{
//子类特色属性定义
}
public class MysqlSchemaStatParserVO extends SchemaStatParserVO{
//定义子类属性
}
2、定义公共类,定制公共逻辑接口。
a)接口定义
public interface ISQLStatementParseService {
SchemaStatParserVO SQLParser(String type);
}
b)实现定义
@Service
public class HiveStatementParseServiceImpl implements ISQLStatementParseService {
@Override
public SchemaStatParserVO SQLParser(String type) {
//定制化子类实现属性
return null;
}
@Service
public class MysqlStatementParseServiceImpl implements ISQLStatementParseService {
@Override
public SchemaStatParserVO SQLParser(String type) {
//定制化子类实现属性
return null;
}
3、创建策略工厂类
生成 spring上下文 类ApplicationContextHolder
package com.yezhongye.plus.config;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationContextHolder implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
}
import com.yezhongye.plus.cache.DatabaseType;
import com.yezhongye.plus.config.ApplicationContextHolder;
import com.yezhongye.plus.service.ISQLStatementParseService;
import com.yezhongye.plus.service.impl.ClickStatementParseServiceImpl;
import com.yezhongye.plus.service.impl.HiveStatementParseServiceImpl;
import com.yezhongye.plus.service.impl.MysqlStatementParseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SQLStatementParseFactory {
@Autowired
private ApplicationContextHolder applicationContextHolder;
public ISQLStatementParseService getSQLStatementParser(Integer databaseType){
switch (databaseType){
case 1:
return applicationContextHolder.getApplicationContext().getBean(MysqlStatementParseServiceImpl.class);
case 2:
return applicationContextHolder.getApplicationContext().getBean(ClickStatementParseServiceImpl.class);
case 3:
return applicationContextHolder.getApplicationContext().getBean(HiveStatementParseServiceImpl.class);
default:
return null;
}
}
}
4、测试调用
a)测试类创建
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@MapperScan("com.yezhongye.plus.mapper")
public class DemoApplicationTests {
}
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yezhongye.plus.DemoApplicationTests;
import com.yezhongye.plus.bean.ClickSchemaStatParserVO;
import com.yezhongye.plus.bean.Employee;
import com.yezhongye.plus.bean.SchemaStatParserDTO;
import com.yezhongye.plus.cache.DatabaseType;
import com.yezhongye.plus.factory.SQLStatementParseFactory;
import com.yezhongye.plus.mapper.EmployeeMapper;
import com.yezhongye.plus.service.EmployeeService;
import com.yezhongye.plus.service.ISQLStatementParseService;
import com.yezhongye.plus.utils.FastJsonUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDateTime;
import java.util.List;
public class EmployeeTest extends DemoApplicationTests {
@Autowired
private SQLStatementParseFactory sqlStatementParseFactory;
@Test
public void sqlParseTest(){
ISQLStatementParseService clickService = sqlStatementParseFactory.getSQLStatementParser(1);
}
}