在bean类中加注解@Scope(“prototype”) 发现仍然是单例模式。
代码如下:
service
/**
* @author Kern
* @Title: ExcelTemplateServiceImpl
* @ProjectName swms-java
* @Description: TODO
* @date 2019-7-815:14
*/
@Scope("prototype")
@Service
public class ExcelTemplateServiceImpl implements IExcelTemplateService {
private static Logger logger = LoggerFactory.getLogger(ExcelTemplateServiceImpl.class);
@Autowired
private ISysDocParamConfService docParamConfService;
@Autowired
private ISysDocConfService docConfService;
//根据POI封装的一些工具类
private POIStyleTools styleTools;
private POIDataTools dataTools;
private POIAutoTools autoTools;
//头部注释行样式和字体
private CellStyle topicContextStyle;
private Font topicContextFont;
//表头样式和字体
private CellStyle headContextStyle;
private Font headContextFont;
//正文样式和字体
private CellStyle contextStyle;
private Font contextFont;
//默认的workbook,用于生成样式和字体
private Workbook defWorkbook;
private final static String DEF_DICT_ARR_SHEET_NAME = "hidden";
/**
* 初始化方法 谨慎修改
*/
@PostConstruct
public void init() {
defWorkbook = new HSSFWorkbook();
styleTools = POIBox.openStyleTool();
dataTools = POIBox.openDataTool();
autoTools = POIBox.openAutoTool();
topicContextStyle = styleTools.getDefCellStyle(defWorkbook, false, true);
topicContextFont = styleTools.getFont(defWorkbook, "宋体", (short)11);
topicContextStyle.setFont(topicContextFont);
headContextStyle = styleTools.getDefCellStyle(defWorkbook, true, true);
styleTools.setBackGroundColor(headContextStyle, HSSFColor.BLUE_GREY.index);
headContextFont = styleTools.getFont(defWorkbook, "宋体", (short)13);
headContextFont.setColor(HSSFColor.WHITE.index);
headContextStyle.setFont(headContextFont);
contextStyle = styleTools.getDefCellStyle(defWorkbook, false, true);
contextFont = styleTools.getFont(defWorkbook, "宋体", (short)11);
contextStyle.setFont(contextFont);
}
@Override
public void download(Integer templateType, HttpServletResponse response) throws BizException {
//TODO 下载文档模板
}
}
contorller
@RequestMapping("/file")
@RestController
@Api(value = "/file", basePath = "file", description = "文档文件管理", produces = MediaType.APPLICATION_JSON_VALUE)
public class FileManageController {
@Autowired
private IExcelTemplateService excelTemplateService ;
@GetMapping("/template/{templateType}")
@ApiOperation(value = "/template/{templateType}",notes = "模板下载")
public Response templateDownload(
@PathVariable("templateType") Integer templateType,
HttpServletResponse response) throws BizException {
this.excelTemplateService.download(templateType, response);
return ResponseUtil.returnSuccess(ResponseCodeEnum.SUCCESS.getMsg());
}
}
不知道因为什么原因,@scope注解的类无法实现由@autowird注入的bean的单例和多例的配置,将代码修改为由bean工厂的getBean方法获取bean,则可以实现多例模式 。service不变,controller 修改如下
controller
@RequestMapping("/file")
@RestController
@Api(value = "/file", basePath = "file", description = "文档文件管理", produces = MediaType.APPLICATION_JSON_VALUE)
public class FileManageController {
@Autowired
private BeanFactory beanFactory;
@GetMapping("/template/{templateType}")
@ApiOperation(value = "/template/{templateType}",notes = "模板下载")
public Response templateDownload(
@PathVariable("templateType") Integer templateType,
HttpServletResponse response) throws BizException {
IExcelTemplateService excelTemplateService = beanFactory.getBean(IExcelTemplateService.class);
excelTemplateService.download(templateType, response);
return ResponseUtil.returnSuccess(ResponseCodeEnum.SUCCESS.getMsg());
}
}
后记
@Autowired 在项目启动时已经将bean实例注入到引用类的成员变量中,因此在调用时是直接引用了已被初始化的成员变量,不会再从bean工厂中获取,因此没办法实现多例。也就是说多例模式,必须从bean工厂获取bean才能够实现。