1, create a new project, import the corresponding jar package
Aopalliance.jar
Aspectjweaver.jar
Commons-logging-1.2.jar
Spring-aop-4.0.0.release.jar
Spring-aspects-4.0.0.release.jar
Spring-beans-4.0.0.release.jar
Spring-context-4.0.0.release.jar
Spring-core-4.0.0.release.jar
Spring-expression-4.0.0.release.jar
2. Create a class that requires slicing programming
package com.sun.spring.stu.aop;public interface arithmaticcaculator { int add (INT I, INT J); int sub (Int i, int  J); int mul (int i, int j); int Div (int i, int j);} package com.sun.spring.stu.aop;import org.springframework.stereotype.component; @Component (" Arithmaticcaculator ") Public class arithmaticcaculatorimpl implements arithmaticcaculator { @Override public int add (int i, int  J) { return i+j; } @ Override public int sub (INT I, INT J) { return i-j; } @Override public &nbSp;int mul (INT I, INT J) { return i*j; } @Override public int div (int i, int  J) { return i/j; }}
3, establish the Section class
Package Com.sun.spring.stu.aop;import Java.util.arrays;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import Org.springframework.stereotype.Component, @Aspect @componentpublic class Loggingaspect {@Before ("Execution (public int com.sun.spring.stu.aop.arithmaticcaculator.* (int,int)) "public void Beforemethod (Joinpoint joinpoint) {String m Ethodname = Joinpoint.getsignature (). GetName (); object[] args = Joinpoint.getargs (); System.out.println ("the method" + methodname+ "Begain with:" +arrays.aslist (args)); }}
4. Configure IOC container
<?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:context= "Http://www.springframework.org/schema/context" xmlns:aop= "http// Www.springframework.org/schema/aop " xsi:schemalocation=" http://www.springframework.org/ schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http:// www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-4.0.xsd "> <context:component-scan base-package=" COM.SUN.SPRING.STU.AOP "></context:component-scan> <aop:aspectj-autoproxy ></aop:aspectj-autoproxy&gT;</beans>
5. Call the class that requires the slice programming, and naturally call the related code in the slice class
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); Arithmaticcaculator caculator = (arithmaticcaculator) ctx.getbean ("Arithmaticcaculator"); int result = Caculator.add ( 21, 90); System.out.print ("The result:" +result);
6. Results:
2016-2-6 12:01:53 Org.springframework.context.support.ClassPathXmlApplicationContext Preparerefresh
Information: Refreshing org[email protected]2be9cb75:startup date [Sat. 12:01:53 CST 2016]; Root of context Hierarchy
2016-2-6 12:01:53 Org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitions
Info: Loading XML Bean Definitions from class path resource [Applicationcontext.xml]
The method add Begain with:[21, 90]
The result:111
This article is from the "job" blog, so be sure to keep this source http://sunchaojob.blog.51cto.com/6249392/1741307
Spring Slicing programming