<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">
<context:annotation-config />
<context:component-scan base-package="org.jixiuf" />
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 配置事务特性 ,配置add、delete和update开始的方法,事务传播特性为required-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!--
配置那些类的方法进行事务管理,当前cn.com.jobedu.oa.service包中的子包、类中所有方法需要,还需要参考tx:advice的设置
-->
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution (* org.jixiuf.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
</aop:config>
<!-- 定义数据源的Bean ,给Hibernate的sessionFactory-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver">
</property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test">
</property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize">
<value>10</value>
</property>
<property name="minPoolSize">
<value>5</value>
</property>
<property name="maxPoolSize">
<value>30</value>
</property>
<property name="acquireIncrement">
<value>5</value>
</property>
<property name="maxIdleTime">
<value>10</value>
</property>
<property name="maxStatements">
<value>0</value>
</property>
</bean>
<!-- 定义Hibernate的sessionFactory,通过该Bean,可以获得Hibernate的Session
另外在其中加入了四个事件监听器,用于当通过hibernarte 修改数据库数据时,将修改的数据同步到compass 索引中,
这个过程 不需要写一句代码 ,由监听器"org.compass.gps.device.hibernate.embedded.CompassEventListener"
自动 完成数据的同步, 用户需要做的只是能过 compass 到相应的索引目录中进行查询操作
-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!--设置二级缓冲-->
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<!--设置二级缓冲,打开查询缓冲-->
<prop key="hibernate.cache.use_query_cache">true</prop>
<!--设置显示Hibernate操作的SQL语句-->
<prop key="hibernate.show_sql">true</prop>
<prop key="hbm2ddl.auto">create</prop>
<!--- 这个属性一定要配,只有配了这个属性,这几个监听器才真正有用 -->
<prop key="compass.engine.connection">c:/target</prop>
</props>
</property>
<!--- 监听器,用于监听 数据库中的数据变化 ,并实时更新相应的数据到compass索引中 -->
<property name="eventListeners">
<map>
<entry key="post-update">
<bean
class="org.compass.gps.device.hibernate.embedded.CompassEventListener" />
</entry>
<entry key="post-insert">
<bean
class="org.compass.gps.device.hibernate.embedded.CompassEventListener" />
</entry>
<entry key="post-delete">
<bean
class="org.compass.gps.device.hibernate.embedded.CompassEventListener" />
</entry>
<entry key="post-collection-recreate">
<bean
class="org.compass.gps.device.hibernate.embedded.CompassEventListener" />
</entry>
<entry key="post-collection-remove">
<bean
class="org.compass.gps.device.hibernate.embedded.CompassEventListener" />
</entry>
<entry key="post-collection-update">
<bean
class="org.compass.gps.device.hibernate.embedded.CompassEventListener" />
</entry>
</map>
</property>
<property name="annotatedClasses" ref="annotatedClasses" />
</bean>
<!-- annocation pojo class in hibernate hibernate 相应的pojo对象,通过 annotation 进行配置 , -->
<bean id="annotatedClasses"
class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="sourceList">
<list>
<value>org.jixiuf.pojo.User</value>
<value>org.jixiuf.pojo.Role</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" name="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
<bean id="annotationConfiguration"
class="org.compass.annotations.config.CompassAnnotationsConfiguration">
</bean>
<bean id="compass" class="org.compass.spring.LocalCompassBean">
<property name="connection">
<!--这个值 一定要与上面sessionfactory 中的目录相对应,使得此compass的代码才能真正查到数据, -->
<value>file://c:/target</value>
</property>
<property name="classMappings">
<list>
<value>org.jixiuf.pojo.User</value>
<value>org.jixiuf.pojo.Role</value>
</list>
</property>
<property name="compassConfiguration" ref="annotationConfiguration" />
<property name="compassSettings">
<props>
<prop key="compass.transaction.factory">
org.compass.spring.transaction.SpringSyncTransactionFactory
</prop>
<prop key="compass.engine.analyzer.MMAnalyzer.CustomAnalyzer">net.paoding.analysis.analyzer.PaodingAnalyzer </prop>
</props>
</property>
<property name="transactionManager" ref="transactionManager" />
</bean>
<!--
这里的东西用不到,
因为数据库中的数据会实时更新到索引中去,
如果还想进行一次全局的索引数据重建,可以通过 HibernateHelper.getCompassGps(sf) 类进行操作,不必使用 spring.getBean(compassGps)
<bean id="hibernateGpsDevice" class="org.compass.gps.device.hibernate.HibernateGpsDevice">
<property name="name">
<value>hibernateDevice</value></property>
<property name="sessionFactory" ref="sessionFactory" />
<!--
<property name="nativeExtractor"><bean class="org.compass.spring.device.hibernate.SpringNativeHibernateExtractor" /></property>
-->
</bean>
<bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps" >
<property name="compass"><ref bean="compass" /></property>
<property name="gpsDevices">
<list>
<ref bean="hibernateGpsDevice" />
</list>
</property>
</bean>
-->
<bean id="compassTemplate"
class="org.compass.core.CompassTemplate">
<property name="compass" ref="compass" />
</bean>
一个查询UserService
package org.jixiuf.service;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.compass.core.Compass;
import org.compass.core.CompassCallback;
import org.compass.core.CompassException;
import org.compass.core.CompassHitsOperations;
import org.compass.core.CompassSession;
import org.compass.core.CompassTemplate;
import org.hibernate.SessionFactory;
import org.jixiuf.dao.UserDao;
import org.jixiuf.pojo.User;
import org.springframework.stereotype.Component;
@Component("userService")
public class UserService {
UserDao userDao;
CompassTemplate compassTemplete;
public CompassTemplate getCompassTemplete() {
return compassTemplete;
}
@Resource(name = "compassTemplate")
public void setCompassTemplete(CompassTemplate compassTemplete) {
this.compassTemplete = compassTemplete;
}
public UserDao getUserDao() {
return userDao;
}
@Resource(name = "userDao")
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void saveUser(User transientUser) {
userDao.saveUser(transientUser);
}
public List<User> findAllUserFromCompass() {
List<User> users = new ArrayList<User>();
//这里不必写事务的处理代码 ,因为由spring 进行了声明式事务
CompassHitsOperations hits = this.getCompassTemplete().executeFind(
new CompassCallback<CompassHitsOperations>() {
public CompassHitsOperations doInCompass(
CompassSession session) throws CompassException {
return session.queryBuilder().alias(
User.class.getSimpleName()).hits();
}
});
User user = null;
for (int i = 0; i < hits.length(); i++) {
user = (User) hits.data(i);
users.add(user);
}
return users;
}
}
------------------------------------------------------------------------------------------------------
如果担心 数据不同步,要定时进行索引的重建
可以在定时器中调用()
@Test
public void testIndex() {
CompassGps gps = HibernateHelper.getCompassGps(sf);
gps.index();
}