Движок скорости без init()

Я хотел бы знать, как работает Velocity Template без инициализации.

Инициализация отсутствует: как показано ниже

     Velocity.init();            
         OR
     VelocityEngine velocity = new VelocityEngine();
     velocity.init();

Код, работающий без init():

System.out.println("Hello World from Java Source");     
VelocityContext context = new VelocityContext();
context.put( "name", new String("Velocity") );

Template template = Velocity.getTemplate("default-template.vm");        
StringWriter sw = new StringWriter();
template.merge( context, sw );
System.out.println(sw.toString());

Вывод:

Hello World from Java Source
Hello World from Velocity Template

person sunleo    schedule 05.04.2014    source источник
comment
Вы случайно не узнали ответ на этот вопрос? Меня озадачивает тот же сценарий   -  person user979051    schedule 30.04.2014


Ответы (1)


метод getTemplate выполняет проверку инициализации и выполняет инициализацию, если Velocity еще не инициализирован.

Из скорости 1.7 Источник:

 public Template getTemplate(String name, String  encoding)
            throws ResourceNotFoundException, ParseErrorException
        {
            requireInitialization();

            return (Template)
                    resourceManager.getResource(name,
                        ResourceManager.RESOURCE_TEMPLATE, encoding);
        }



 private void requireInitialization()
    {
        if (!initialized)
        {
            try
            {
                init();
            }
            catch (Exception e)
            {
                getLog().error("Could not auto-initialize Velocity", e);
                throw new RuntimeException("Velocity could not be initialized!", e);
            }
        }
    }
person user979051    schedule 30.04.2014