基于springboot的抽象工厂实现

前言

为什么要加基于springboot,因为当你用平时写的工厂来return new 带有 mapper等 spring容器的注入等等,就会发现为null。。下面是一个示例,方法随便看看就好,重要是工厂类

抽象类
package awsa.oss.service;

/**
 * @author Jatham
 * @date 2020-04-16 15:22
 */
@Slf4j
@Component
public  abstract class BaseGeneraterPdf {

    @Resource
    protected TabProjectMapper tabProjectMapper;


    public void makePdf() {
          
			generatePdf();
            // 5.关闭文档
            
            document.close();

    }
    public abstract void generatePdf();
}

实现类
package awsa.oss.service.Impl;

import awsa.oss.mapper.TabProjectMapper;
import awsa.oss.entity.TabProjects;
import awsa.oss.service.BaseGeneraterPdf;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author Jatham
 * @date 2020-04-16 15:28
 */
/*给service加上名字*/
@Service("approval_MATERIAL_DETAIL_service")
public class MaterialsDetailPdf extends BaseGeneraterPdf {

	/**正常new对象  mapper会为空*/
    @Autowired
    TabProjectMapper tabProjectMapper;

    @Override
    public void generatePdf()  {
    			业务逻辑
    }
}

工厂类(错误)
package awsa.oss.factory;

import awsa.oss.comm.Enums.TypePdf;
import awsa.oss.service.BaseGeneraterPdf;
import awsa.oss.service.Impl.MaterialsDetailPdf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Jatham
 * @date 2020-04-16 15:37
 */
@Slf4j
@Component
public class PdfFactory implements ApplicationContextAware {
    
    public static BaseGeneraterPdf createPdf(int pdfIndex) {
        TypePdf typePdf = TypePdf.contains(pdfIndex);
        switch (typePdf) {
            /*项目分包中间结算*/
            case PRO_MID_SETTLEMENT:
                return new MaterialsDetailPdf();//就这种写法,mapper就会为空,因为没有交给spring容器管理
            default:
                log.error("匹配失败 , 错误编号{}",typePdf);
                return null;
        }
    }
}

正确写法

package awsa.oss.factory;

import awsa.oss.comm.Enums.TypePdf;
import awsa.oss.service.BaseGeneraterPdf;
import awsa.oss.service.Impl.MaterialsDetailPdf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Jatham
 * @date 2020-04-16 15:37
 */
@Slf4j
@Component
public class PdfFactory implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

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

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name ) {
        try {
//            return getApplicationContext().getBean("approval_" + name + "_service");
            return getApplicationContext().getBean(name);

        }catch(Exception e) {
            return null;
        }
    }

    public static<T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

使用方法

@GetMapping("/showPdf")
    public void testPdf(@RequestParam String projectCode,
                        @RequestParam Integer pdfIndex) throws Exception {

        TypePdf typePdf = TypePdf.contains(pdfIndex);
        BaseGeneraterPdf baseGeneraterPdf = (BaseGeneraterPdf) PdfFactory.getBean(“approval_MATERIAL_DETAIL_service”);
//        if( bizHandler != null ) {
//            return bizHandler.executeApproval(reqEntity);
//        }
        baseGeneraterPdf.makePdf(1, projectCode);


    }

博主公众号
求关注
在这里插入图片描述

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页