Java tutorial
/** * jetbrick-template * http://subchen.github.io/jetbrick-template/ * * Copyright 2010-2014 Guoqiang Chen. All rights reserved. * Email: subchen@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrick.template; import java.beans.ConstructorProperties; import java.io.File; import java.io.FileNotFoundException; import java.util.Properties; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; /** * <p>???JetEngineSpring(?) </p> * <p><strong>?</strong>?configFileconfigPropertiesconfigProperties?<strong></strong>configFile?</p> * * <pre> * <bean id="jetEngine" class="jetbrick.template.JetEngineFactoryBean" /> * * <bean id="jetEngine" class="jetbrick.template.JetEngineFactoryBean"> * <property name="configFile" value="classpath:META-INF/jetbrick-template.properties" /> * </bean> * * <bean id="jetEngine" class="jetbrick.template.JetEngineFactoryBean"> * <property name="configFile" value="file:/path/to/jetbrick-template.properties" /> * </bean> * * <bean id="jetEngine" class="jetbrick.template.JetEngineFactoryBean"> * <property name="configProperties"> * <props> * <prop key="compile.debug">true</prop> * ... * </props> * </property> * </bean> * </pre> * * @since 1.1.1 * @author ?(yingzhor@gmail.com) */ public class JetEngineFactoryBean implements FactoryBean<JetEngine>, InitializingBean { private JetEngine singleton = null; private Resource configFile = null; private Properties configProperties = null; @Override public void afterPropertiesSet() throws Exception { if (configFile == null && configProperties == null) { singleton = JetEngine.create(); return; } Properties effectProps = new Properties(); if (configFile != null) { check(configFile.getFile()); effectProps.load(configFile.getInputStream()); } if (configProperties != null) { effectProps.putAll(configProperties); } singleton = JetEngine.create(effectProps); } public JetEngineFactoryBean() { } @ConstructorProperties({ "configFile" }) public JetEngineFactoryBean(Resource configFile) { this.configFile = configFile; } @ConstructorProperties({ "configProperties" }) public JetEngineFactoryBean(Properties configProperties) { this.configProperties = configProperties; } @ConstructorProperties({ "configFile", "configProperties" }) public JetEngineFactoryBean(Resource configFile, Properties configProperties) { this.configFile = configFile; this.configProperties = configProperties; } // ???spring? private void check(File file) throws Exception { // ?? if (file.exists() && file.isDirectory()) { String msg = "config file exists but it is a directory."; throw new FileNotFoundException(msg); } } @Override public JetEngine getObject() throws Exception { return singleton; } @Override public Class<?> getObjectType() { return JetEngine.class; } @Override public boolean isSingleton() { return true; } public Resource getConfigFile() { return configFile; } public void setConfigFile(Resource configFile) { this.configFile = configFile; } public Properties getConfigProperties() { return configProperties; } public void setConfigProperties(Properties configProperties) { this.configProperties = configProperties; } }