您的位置:首页 > 编程语言 > Java开发

JAVA_WEB项目之Spring中使用AOP编程运用到Lucene中实现解耦

2014-07-25 17:03 856 查看
上一篇介绍了JAVA_WEB项目之使用Spring的xml配置方式在项目中管理Lucene检索框架,下面我们简单介绍如何运用AOP编程到Lucene的业务方法中,上一篇中的代码块中:

@Override
public void save(Goods t) {
// TODO Auto-generated method stub
super.save(t);
//把商品同步添加到索引库中
luceneService.addDocument(t);
}


这一块代码我们可以把他归结为类似于事务那样使用Spring中的AOP动态在运行的时候把他切入到该方法中。下面我直接给出application-lucene.xml配置文件信息:

<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<bean id="configureLucene" class="com.shop.lucene.ConfigureLucene" init-method="init"></bean>
<bean id="documentUtil" class="com.shop.lucene.DocumentUtil"></bean>
<bean id="luceneUtil" class="com.shop.lucene.LuceneUtil" init-method="init" destroy-method="destory">
<property name="configureLucene" ref="configureLucene"></property>
</bean>
<bean id="highlighterUtil" class="com.shop.lucene.HighlighterUtil" init-method="init">
<property name="configureLucene" ref="configureLucene"></property>
</bean>
<bean id="luceneService" class="com.shop.lucene.LuceneServiceImpl">
<property name="documentUtil" ref="documentUtil"></property>
<property name="luceneUtil" ref="luceneUtil"></property>
<property name="highlighterUtil" ref="highlighterUtil"></property>
</bean>
<!-- aop配置 -->
<aop:config>
<!--配置切面  -->
<aop:aspect ref="luceneService">
<!-- 表示有正确的返回值的时候把ethod里面的方法切入到切面表达式指定的连接点(切入点)方法中-->
<aop:after-returning method="addDocumentProxy" pointcut="execution(* com.shop.service.impl.GoodsServiceImpl.save(..))"/>
</aop:aspect>
</aop:config>
</beans>


其他的相关实体类,工具类和上一篇的几乎一样,只有实现类的save方法稍微要修改一下:

package com.shop.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.shop.lucene.LuceneService;
import com.shop.pojo.Goods;
import com.shop.service.GoodsService;

@Service("goodsService")
public class GoodsServiceImpl extends BaseServiceImpl<Goods> implements GoodsService{
//	@Resource
//	private LuceneService luceneService;
public List<Goods> queryGoodsByCommendAndOpenAndCid(int cid) {
String hql="From Goods g Left join Fetch g.category where g.commend=true and g.open=true and g.category.id=:cid order by g.date";
Session session= sessionFactory.getCurrentSession();
return session.createQuery(hql).setInteger("cid", cid).setFirstResult(0).setMaxResults(3).list();
}

@Override//不能删除,虽然父类由此方法,但是aop编程不能把方法切入父类的方法中
public void save(Goods t) {
// TODO Auto-generated method stub
super.save(t);
/**
* 注意这里:如果我们使用aop编程把同步到索引库中的代码切入到此save方法中,如何把对象传入到通知的方法中(也就是添加到索引库中的方法addDocument)
* 解答是:我们可以用JoinPoint把对象传到通知大方法中。我们也可以验证传入的是否同一个对象
*/
System.out.println("对象t是:"+t);
//把商品同步添加到索引库中
//luceneService.addDocument(t);

}

}


结果是:

对象t是:com.shop.pojo.Goods@1bb0662
获取目标对象:com.shop.service.impl.GoodsServiceImpl@14bcae9
获取当前连接点的方法信息:void com.shop.service.BaseService.save(Object)
获取当前连接点的参数信息:com.shop.pojo.Goods@1bb0662
com.shop.action.BaseAction<com.shop.pojo.Goods>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐