JAVA_WEB项目之Spring中使用AOP编程运用到Lucene中实现解耦
2014-07-25 17:03
711 查看
上一篇介绍了JAVA_WEB项目之使用Spring的xml配置方式在项目中管理Lucene检索框架,下面我们简单介绍如何运用AOP编程到Lucene的业务方法中,上一篇中的代码块中:
这一块代码我们可以把他归结为类似于事务那样使用Spring中的AOP动态在运行的时候把他切入到该方法中。下面我直接给出application-lucene.xml配置文件信息:
其他的相关实体类,工具类和上一篇的几乎一样,只有实现类的save方法稍微要修改一下:
结果是:
@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>
相关文章推荐
- 利用Spring的AOP编程和JAVA自定义注解,实现Web项目的HTTP请求头域检测
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- JAVA_WEB项目之使用Spring的xml配置方式在项目中管理Lucene检索框架
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- ThreadLocal的实现原理,及使用实例,解决spring,hibernate非web项目下的懒加载 no session or session was closed(2)!
- JAVA_WEB项目之Lucene使用中文分词器IKAnalyzer3.2.8
- 2.1、Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职
- eclispe中的maven项目使用spring报java.lang.ClassNotFoundException: org.springframework.web.context.ContextLo
- 在maven项目中使用apache cxf中遇到异常 java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast to javax.servlet.Filter
- 在spring中使用MethodInterceptor实现aop(附项目中demo)
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- Java使用简单工厂模式对面向接口编程模式的深度解耦实现
- eclispe中的maven项目使用spring报java.lang.ClassNotFoundException: org.springframework.web.c
- 使用spring实现事务管理(@transactional)的遇到的两个异常:java.lang.noclassdeffounderror:org/objectweb.asm
- JAVA_WEB项目之Lucene实现检索结果排序和关键字在索引库中多字段查询结果进行高亮显示
- java web 项目实现 耦合-解耦 解决方案----工厂模式入门级别