标签:cat .class struct int ffffff 构造 autowired ini web.xml
spring.version为4.3.10.RELEASE
aspectjweaver.version为1.8.10
libreoffice.version为5.2.0(juh、jurt、ridl、unoil四个相关jar包)
libreoffice核心包jodconverter-core为3.2-xwiki-SNAPSHOT
一、相关配置类 AppConfiguration
@Configuration //相当于beans.xml
@ComponentScan({"com.jingluu.converter.doc"}) //相当于 <context:component-scan base-package="com.jingluu.converter.doc"/>
@PropertySource({"classpath:converter.properties"}) //相当于 <context:property-placeholder location="classPath:converter.properties" />
二、web初始化加载 AppWebInitializer implements WebApplicationInitializer
@Override public void onStartup(ServletContext servletContext) throws ServletException { //注解配置web.xml上下文 AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext(); //注册配置类 /* * 相当于加载beans.xml * <context-param> * <param-name>contextConfigLocation</param-name> * <param-value>classpath:spring/applicationContext.xml</param-value> * </context-param> */ rootAppContext.register(AppConfiguration.class); //设置ServletContext /* * servlet上下文,相当于web.xml */ rootAppContext.setServletContext(servletContext); //添加监听器 /* * 相当于添加一个上下文监听 * <listener> * <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> * </listener> */ ContextLoaderListener contextLoaderListener = new ContextLoaderListener(rootAppContext); /* * 将配置的一个监听的机制存放到web.xml中 */ servletContext.addListener(contextLoaderListener); }
三、ConverterProperties (获取我们在配置文件中内容)
@Component public class ConverterProperties { @Value("${converter.office.home}") private String officeHome; @Value("${converter.office.ports}") private String officePorts; public String getOfficeHome() { return officeHome; } public String getOfficePorts() { return officePorts; } }
四、DocConverter 和 DocConverterImpl
public interface DocConverter { /** * 文本转换 * @param from 初始文件路径 * @param to 传出文件路径 */ void converter(String from,String to); /** * 文本转换 * @param from * @param to */ void converter(File from,File to); }
/** * office文档转换实现类 * @author kif * */ @Component public class DocConverterImpl implements DocConverter{ @Autowired private ConverterProperties converterProperties; //文件管理 private OfficeManager officeManager; //文件转换 private OfficeDocumentConverter offDocConverter; /** * @PostConstruct 声明该方法在构造函数执行完之后被调用,且仅调用一次 * 相当于<init-method></init-method> */ @PostConstruct public void start(){ //配置 DefaultOfficeManagerConfiguration cfg = new DefaultOfficeManagerConfiguration(); //指定OpenOffice或LibreOffice安装的根目录 cfg.setOfficeHome(converterProperties.getOfficeHome()); //LibreOffice处理进程的端口,多个用逗号隔开(如果设置了多个端口,将会启动多个处理进程) String portsNumbers = converterProperties.getOfficePorts(); if(portsNumbers != null && !"".equals(portsNumbers.trim())){ String[] ports = portsNumbers.split(","); int[] portList = new int[ports.length]; for(int i=0;i<ports.length;i++){ portList[i] = Integer.valueOf(ports[i]); } cfg.setPortNumbers(portList); } //officeManager officeManager = cfg.buildOfficeManager(); //OfficeDocumentConverter文档转换器 offDocConverter = new OfficeDocumentConverter(officeManager); System.out.println("Converter initialized ..."); //启动文档转换器进程 officeManager.start(); System.out.println("converter start ..."); } /** * 进行转换 */ public void converter(String from, String to) { this.converter(new File(from), new File(to)); } @Override public void converter(File from, File to) { offDocConverter.convert(from, to); } /** * 终止处理进程 * @PreDestroy 声明该方法在Bean销毁前被调用,仅调用一次 */ @PreDestroy public void stop(){ officeManager.stop(); System.out.println("converter stop ..."); } }
标签:cat .class struct int ffffff 构造 autowired ini web.xml
原文地址:http://www.cnblogs.com/kongkongFabian/p/7497273.html