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

上一篇介绍了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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值