In our web development we are in the public some of the methods we need to extract this to achieve the redundancy of the code today I made a collation using the actual application of AOP in the project
First, the XML configuration scan
<?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>
Active slice display column 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; /** * voting Slice class * @author zhangxiaoqiang * */@Aspect @componentpublic class voteinterceptor{ @Autowired private systemservice systemservice; @Autowired private voteservicei voteservice; /* * * The method class that you want to slice * */&NB sp; @Pointcut ("Execution (public * Com.leimingtech.core.service.ContentsServiceI.saveContent (..)) ") public void mymethod () {}; /** * The following is the use of the weaving point syntax, see the document contains . is specified in the method before the execution * remember the following such a generic, * means all * @param map */ @Before ("MyMethod () &&args (map,..)") public void beforemethod (Map<string,object> map) { } /** * after normal execution * After saving the content, save voting * @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 (); //content 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); } } /** * after normal execution */ @AfterReturning ("MyMethod ()") Public void afterreturnning () { } /** * Call * If an exception is thrown / @Afterthrowing ("MyMethod ()") public void afterthrowing () { } }
Java Web Slice Programming