Java web切面編程

來源:互聯網
上載者:User

標籤:hang   取出   autowired   class   exe   eth   sse   author   win   

在我們的 web開發中  我們在 對公用的 一些方法 我們需要抽取出來   這樣達到 代碼的冗餘   今天 我利用項目上用的AOP的 實際 應用做了一個整理

首先  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: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/context            http://www.springframework.org/schema/context/spring-context-3.0.xsd            http://www.springframework.org/schema/aop              http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <aop:aspectj-autoproxy />    <context:component-scan base-package="com.leimingtech.platform.core.interceptors" />    <context:component-scan base-package="com.leimingtech.cms.interceptors" />

</beans>

   活動切面示列  import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component; import com.leimingtech.core.common.ContentClassify;import com.leimingtech.core.base.ContextHolderUtils;import com.leimingtech.core.entity.ContentsEntity;import com.leimingtech.core.entity.VoteEntity;import com.leimingtech.core.service.SystemService;import com.leimingtech.core.service.VoteServiceI;import com.leimingtech.core.util.StringUtils; /** * 投票切面類 * @author zhangxiaoqiang * */@Aspect@Componentpublic class VoteInterceptor{    @Autowired    private SystemService systemService;    @Autowired    private VoteServiceI voteService;    /*    *    *所要切面的 那個方法 類    *    */    @Pointcut("execution(public * com.leimingtech.core.service.ContentsServiceI.saveContent(..))")      public void myMethod(){};    /**     * 下面用到的是織入點文法, 看文檔裡面有. 就是指定在該方法前執行     * 記住下面這種通用的, *表示所有     * @param map     */    @Before("myMethod()&&args(map,..)")    public void beforeMethod(Map<String,Object> map){            }    /**     * 正常執行完後     * 儲存內容之後,儲存投票     * @param map     */    @After("myMethod()&&args(map,..)")    public void after(Map<String,Object> map){        ContentsEntity contents = (ContentsEntity) map.get("contents");        VoteEntity vote = (VoteEntity) map.get("vote");        HttpServletRequest request = ContextHolderUtils.getRequest();        //內容id        String contentsId = request.getParameter("contentsId");        if(StringUtils.isNotEmpty(contentsId)){            contents= voteService.get(ContentsEntity.class, String.valueOf(contentsId));        }        String classify = contents.getClassify();        if(ContentClassify.CONTENT_VOTE.equals(classify)){            voteService.saveVote(contents, vote);        }    }    /**     * 正常執行完後     */    @AfterReturning("myMethod()")    public void afterReturnning(){            }    /**     * 拋出異常時才調用     */    @AfterThrowing("myMethod()")    public void afterThrowing(){            }    }   

Java web切面編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.