JAVA Spring 简单策略工厂模式小案例

        可以使用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);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值