我尝试创建一个可以执行此操作的 Spring bean(如下所示),但它不起作用。Spring 似乎想调用 License.setLicense(Reader) 而不是 License.setLicense(String)。我得到的错误是Failed to convert property value of type 'java.lang.String' to required type 'java.io.Reader' for property 'license'。
<bean id="asposeLicense" class="com.aspose.cells.License">
<property name="license" value="Aspose.Cells.lic" />
</bean>
然而,我确实得到了这个更通用的(Java)解决方案:
网页.xml:
<!-- does things needing doing when application starts or stops -->
<listener>
<listener-class>
com.xyz.listener.ApplicationStartupListener
</listener-class>
</listener>
ApplicationStartupListener.java(新类):
package com.xyz.listener;
import java.io.InputStream;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.aspose.cells.License;
public class ApplicationStartupListener implements ServletContextListener {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void contextInitialized(ServletContextEvent event) {
logger.info("Initializing application context...");
try {
// set license for Aspose.Cells (the Excel API)
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("excel/Aspose.Cells.lic");
License license = new License();
license.setLicense(inputStream);
logger.info("Aspose.Cells license set? " + License.isLicenseSet());
} catch (Exception e) {
logger.error("Error encountered trying to set Aspose.Cells license!", e);
}
logger.info("Application context initialized");
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
}