我在运行时设置一些选项时遇到了类似的问题。我想出了一个自定义VelocityBuilder和外部velocity.properties文件的问题,您可以在其中放置所有运行时属性。 下面是代码:
public class BaseVelocityBuilder implements VelocityBuilder {
private VelocityEngine engine;
private Log logger = LogFactory.getLog(getClass());
@Autowired
private WebApplicationContext webApplicationContext;
public VelocityEngine engine() {
if(engine == null) {
engine = new VelocityEngine();
Properties properties = new Properties();
InputStream in = null;
try {
in = webApplicationContext.getServletContext().getResourceAsStream("/WEB-INF/velocity.properties");
properties.load(in);
engine.init(properties);
} catch (IOException e) {
e.printStackTrace();
logger.error("Error loading velocity engine properties");
throw new ProgramException("Cannot load velocity engine properties");
}
IOUtils.closeQuietly(in);
}
return engine;
}
}
看到这一行:
in = webApplicationContext.getServletContext().getResourceAsStream("/WEB-INF/velocity.properties");
properties.load(in);
engine.init(properties);
所以我在/ WEB-INF一个velocity.properties文件,其中我把一些配置:
resource.loader = webinf, class
webinf.resource.loader.description = Framework Templates Resource Loader
webinf.resource.loader.class = applica.framework.library.velocity.WEBINFResourceLoader
webapp.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader
webapp.resource.loader.path =
file.resource.loader.description = Velocity File Resource Loader
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path =
class.resource.loader.description = Velocity Classpath Resource Loader
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
runtime.log='/pathYouWant/velocity.log'
最后在application.xml中:
<bean class="applica.framework.library.velocity.BaseVelocityBuilder" />
通过这种方式,您可以为不同的应用程序创建不同的文件日志,并且当您在生产中进行战争时,由于生产服务器的env配置,sysadm可以更改属性。
这对我来说很合适。该位置是否具有正确的权限?作为替代方案,您还可以使用log4j并将其合并到常规应用程序日志中。 – 2011-06-12 20:41:19