-
Notifications
You must be signed in to change notification settings - Fork 24
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
Comments
ghost
assigned subchen
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>
* <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>
* </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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
配置如下:
感谢 @应卓(yingzhor@gmail.com) 贡献代码
The text was updated successfully, but these errors were encountered: