Spring Slicing programming

Source: Internet
Author: User
Tags mul

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.