开发者

Read template from the database - VelocityEngine

开发者 https://www.devze.com 2023-04-13 00:13 出处:网络
I am storing the templates I use with velocity in the DB and I wan to configure it to read the templates from the DB.

I am storing the templates I use with velocity in the DB and I wan to configure it to read the templates from the DB.

my bean definition i开发者_JAVA技巧s:

<bean id="velocityEngineBasedOnDB" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <map>
            <entry key="resource.loader" value="ds"/>
            <entry key="ds.resource.loader.class" value="org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader"/>
            <entry key="ds.resource.loader.resource.table" value="tb_velocity_template"/>
            <entry key="ds.resource.loader.resource.keycolumn" value="id_template"/>
            <entry key="ds.resource.loader.resource.templatecolumn" value="template_definition"/>
            <entry key="ds.resource.loader.resource.datasource" value-ref="dataSource"/>
        </map>
    </property> 
 </bean>

but I got java.lang.ClassCastException: 'resource.datasource' doesn't map to a String object.

So how I configure the data source as string?


Works for me

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
   <property name="velocityProperties">
        <map>
           <entry key="resource.loader" value="ds"/>
           <!--<entry key="ds.resource.loader.class" value="org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader"/>--><!--should be left out-->
           <entry key="ds.resource.loader.resource.table" value="templates" />
           <entry key="ds.resource.loader.resource.keycolumn" value="tid" />
           <entry key="ds.resource.loader.resource.templatecolumn" value="template" />
           <entry key="ds.resource.loader.resource.timestampcolumn" value="updated" />
           <entry key="ds.resource.loader.instance" value-ref="dataSourceLoader" />  
           <entry key="ds.resource.loader.cache" value="true" />  
        </map>
    </property>

</bean>

Data Source Loader:

<bean id="dataSourceLoader" class="org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader"> <property name="dataSource" ref="myDataSource"/><!-- myDataSource is a com.mchange.v2.c3p0.ComboPooledDataSource type--> </bean>


You have to configure another bean with id "datasource" and that will allow Spring to instantiate an instance of a class inheriting from java.sql.Datasource. It may be an OracleDataSource, for example if your database is Oracle.


you should set ds.resource.loader.instance ref a spring bean,eg:

<bean id="resourceLoader"
  class="xx.common.mail.MongoDataSourceResourceLoader"
  p:mongoTemplate-ref="mongoTemplate"/>

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
    <map>
        <entry key="resource.loader" value="ds"/>
        <entry key="ds.resource.loader.instance" value-ref="resourceLoader"/>
    </map>
</property>


<entry key="ds.resource.loader.resource.datasource" value-ref="dataSource"/>

value-ref doesnt work for me

change to value="datasource" and try.

It may be that value-ref support is added in higher versions (if any). I am using Velocity 1.7


<!-- myDataSource is a com.mchange.v2.c3p0.ComboPooledDataSource type-->
<bean id="dataSourceLoader" class="org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader">
        <property name="dataSource" ref="myDataSource"/>
</bean>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号