Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加 Spring FactoryBean 的集成支持 #31

Closed
subchen opened this issue Dec 4, 2013 · 3 comments
Closed

增加 Spring FactoryBean 的集成支持 #31

subchen opened this issue Dec 4, 2013 · 3 comments
Assignees
Labels
Milestone

Comments

@subchen
Copy link
Owner

subchen commented Dec 4, 2013

配置如下:

<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>

感谢 @应卓(yingzhor@gmail.com) 贡献代码

@ghost ghost assigned subchen Dec 4, 2013
@subchen
Copy link
Owner Author

subchen commented Dec 4, 2013

最好能支持直接配置属性

<bean id="jetEngine" class="jetbrick.template.JetEngineFactoryBean">
  <property name="configProperties">
    <props>
        <prop key="template.path">${template.path}</prop>
        ...
    </props>
  </property>
</bean>

@yingzhuo
Copy link

yingzhuo commented Dec 4, 2013

收到,我尽快更新。

@yingzhuo
Copy link

yingzhuo commented Dec 4, 2013

代码已经修改完毕,请查收

源代码

package jetbrick.template.spring;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Properties;

import jetbrick.template.JetEngine;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;


/**
 * 
 * 你可以按照以下方式配置JetEngine在Spring上下文的实例(单例) <br>
 * <strong>注意:</strong>当同时指定configFile和configProperties时,configProperties中的配置会<strong>覆盖</strong>configFile中的配置
 * 
 * <pre>
 *  &lt;bean id="jetEngine" class="jetbrick.template.spring.JetEngineFactoryBean" /&gt;
 *  
 *  &lt;bean id="jetEngine" class="jetbrick.template.spring.JetEngineFactoryBean"&gt;
 *    &lt;property name="configFile" value="classpath:META-INF/jetbrick-template.properties" /&gt;
 *  &lt;/bean&gt;
 *
 *  &lt;bean id="jetEngine" class="jetbrick.template.spring.JetEngineFactoryBean"&gt;
 *     &lt;property name="configFile" value="file:/path/to/jetbrick-template.properties" /&gt;
 *  &lt;/bean&gt;
 *  
 *  &lt;bean id="jetEngine" class="jetbrick.template.spring.JetEngineFactoryBean"&gt;
 *    &lt;property name="configFile" value="classpath:jetbrick-template.properties" /&gt;
 *      &lt;property name="configProperties"&gt;
 *        &lt;props&gt;
 *          &lt;prop key="compile.debug"&gt;true&lt;/prop&gt;
 *        &lt;/props&gt;
 *    &lt;/property&gt;
 *  &lt;/bean&gt;
 * </pre>
 * 
 * @author 应卓(yingzhor@gmail.com)
 *
 */
public class JetEngineFactoryBean implements FactoryBean<JetEngine>, InitializingBean {

    private Resource configFile = null;
    private JetEngine singleton = null;
    private Properties configProperties = null;

    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);
    }

    // 配置有效性检验,如果配置不正确抛出异常使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);
        }
    }

    public JetEngine getObject() throws Exception {
        return singleton;
    }

    public Class<?> getObjectType() {
        return JetEngine.class;
    }

    public boolean isSingleton() {
        return true;
    }

    // getter and setter
    // -------------------------------------------------------------------------------------------------

    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;
    }

}

配置示例

<bean id="jetEngine" class="jetbrick.template.spring.JetEngineFactoryBean" />

<bean id="jetEngine" class="jetbrick.template.spring.JetEngineFactoryBean">
    <property name="configFile" value="classpath:META-INF/jetbrick-template.properties" />
</bean>

<bean id="jetEngine" class="jetbrick.template.spring.JetEngineFactoryBean">
    <property name="configFile" value="file:/path/to/jetbrick-template.properties" />
</bean>

<bean id="jetEngine" class="jetbrick.template.spring.JetEngineFactoryBean">
    <property name="configFile" value="classpath:jetbrick-template.properties" />
    <property name="configProperties">
        <props>
            <prop key="compile.debug">true</prop>
        </props>
    </property>
</bean>

@subchen subchen closed this as completed in b1515c8 Dec 4, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants