SPRING4-AOP Notice

Source: Internet
Author: User

1. Create a MAVEN project, project name Springdemo50,

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8F/2B/wKioL1jWEFPBqIDGAAA_HT2_rVY098.png-wh_500x0-wm_ 3-wmp_4-s_212420823.png "title=" Qq20170325143724.png "alt=" Wkiol1jwefpbqidgaaa_ht2_rvy098.png-wh_50 "/>


2. Configure Maven, modify the Pom.xml file in the project, modify the content as follows
<project xmlns= "http://maven.apache.org/POM/4.0.0"  xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance " xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ Xsd/maven-4.0.0.xsd ">  <modelVersion>1.0.0</modelVersion>  <groupId> Shequ</groupid>  <artifactid>springdemo13</artifactid>  <version> 0.0.1-snapshot</version>    <properties>  <java.version>1.7 </java.version>  <project.build.sourceencoding>utf-8</project.build.sourceencoding >  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>   </properties>    <repositories>  <repository>   <id>codelds</id>  <url>https://code.lds.org/nexus/content/groups/main-repo </url>  </repository&gT;  </repositories>    <dependencies>  <dependency>   <groupid>javax.annotation</groupid>  <artifactid>jsr250-api</ artifactid>  <version>1.0</version>  </dependency>     <dependency>  <groupId>org.springframework</groupId>  < artifactid>spring-test</artifactid>  <version>4.1.4.release</version>   </dependency>    <dependency>  <groupid>junit</ Groupid>  <artifactid>junit</artifactid>  <version>4.10</version >  </dependency>    <dependency>  <groupId> org.springframework</groupid>  <artifactid>spring-core</artifactid>   <version>4.1.4.release</version>  </dependency>    <dependency>         <groupId>org.springframework</groupId>         <artifactid>spring-context</artifactid>        <version >4.1.4.RELEASE</version>    </dependency>         <dependency>        <groupId> Org.springframework</groupid>        <artifactid> spring-jdbc</artifactid>        <version>4.1.4.release< /version>    </dependency>        < dependency>        <groupid>mysql</groupid>         <artifactid>mysql-connector-java</artifactid>        < version>5.1.34</version>    </dependency>          </dependencies>  <build/></project>


3. Create the entity bean under Src/main/java Customer, package name (Com.mycompany.shequ.bean)

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8F/2B/wKioL1jWERqQCGc9AABfHNrOq_4817.png-wh_500x0-wm_ 3-wmp_4-s_792200928.png "title=" Qq20170325144041.png "alt=" Wkiol1jwerqqcgc9aabfhnroq_4817.png-wh_50 "/>


4. The content of the entity Bean Customer is as follows
package com.mycompany.shequ.bean;import org.springframework.beans.factory.annotation.value;import  org.springframework.stereotype.Component; @Component ("Customerbean") public class customer  {private string name;private string email;public string getname ()  {return  name;} Public void setname (String name)  {this.name = name;} Public string getemail ()  {return email;} @Value ("#{(' [email protected] '  matches  ' ^[_a-za-z0-9-]+ (\\.[ _a-za-z0-9-]+) " +" *@[a-za-z0-9]+ (\\.[ a-za-z0-9]+) * (\\.[ A-za-z]{2,}) $ ')  ?  ' [email protected] ': ' Illegal '} ') Public void setemail (String email)  {this.email = email;}} 


5. Create the interface Icustomerdao under Src/main/java, package name (Com.mycompany.shequ.dao)

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8F/2B/wKioL1jWEWuAqrcvAABhP8obvLU531.png-wh_500x0-wm_ 3-wmp_4-s_1223318701.png "title=" Qq20170325144203.png "alt=" Wkiol1jwewuaqrcvaabhp8obvlu531.png-wh_50 "/>


The contents of 6.ICustomerDao are as follows
Package Com.mycompany.shequ.dao;public interface Icustomerdao {public void showMe ();


7. Create Icustomerdao implementation class Customerdaoimpl under Src/main/java, package name

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/8F/2D/wKiom1jWEbeTO_QpAABi_rBqd8o543.png-wh_500x0-wm_ 3-wmp_4-s_2533526828.png "title=" Qq20170325144319.png "alt=" Wkiom1jwebeto_qpaabi_rbqd8o543.png-wh_50 "/>


The contents of the 8.ICustomerDao implementation class Customerdaoimpl are as follows
Package Com.mycompany.shequ.dao.impl;import Com.mycompany.shequ.dao.icustomerdao;public Class CustomerDaoImpl Implements Icustomerdao {public void ShowMe () {System.out.println ("I ' m Customerdaoimpl");}}


9. Create a business bean Icustomerservice interface under Src/main/java, package name (Com.mycompany.shequ.service)

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/8F/2D/wKiom1jWEhHSMS_kAABhdyZwGM0321.png-wh_500x0-wm_ 3-wmp_4-s_2928338038.png "title=" Qq20170325144449.png "alt=" Wkiom1jwehhsms_kaabhdyzwgm0321.png-wh_50 "/>


The contents of the 10.ICustomerService interface are as follows
Package Com.mycompany.shequ.service;public interface Icustomerservice {public void showMe ();


11. Create the implementation class Customerserviceimpl of the business Bean Icustomerservice interface under Src/main/java, package name (COM.MYCOMPANY.SHEQU.SERVICE.IMPL)

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8F/2D/wKiom1jWEnHSdVOtAAB44sCmOtA477.png-wh_500x0-wm_ 3-wmp_4-s_2830734644.png "title=" Qq20170325144623.png "alt=" Wkiom1jwenhsdvotaab44scmota477.png-wh_50 "/>


12. The contents of the implementation class Customerserviceimpl for the business Bean Icustomerservice interface are as follows
Package Com.mycompany.shequ.service.impl;import Com.mycompany.shequ.dao.icustomerdao;import Com.mycompany.shequ.service.icustomerservice;public class Customerserviceimpl implements Icustomerservice {private Icustomerdao customerdao;public Icustomerdao Getcustomerdao () {return customerdao;} public void Setcustomerdao (Icustomerdao customerdao) {This.customerdao = Customerdao;} public void ShowMe () {customerdao.showme ();}}


13. Create a pre-notification implementation class Methodbefore, package name (Com.mycompany.shequ.service.impl) under Src/main/java

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/8F/2B/wKioL1jWEwKg161cAAB6NFMYORg148.png-wh_500x0-wm_ 3-wmp_4-s_1608441639.png "title=" Qq20170325144850.png "alt=" Wkiol1jwewkg161caab6nfmyorg148.png-wh_50 "/>


14. The contents of the pre-notification implementation class Methodbefore are as follows
Package Com.mycompany.shequ.service.impl;import Java.lang.reflect.method;import org.springframework.aop.methodbeforeadvice;/** * methods prior to execution notification * @author Administrator * */public class Methodbefore Implements Methodbeforeadvice {public void before (Method arg0, object[] arg1, Object arg2) throws Throwable {SYSTEM.OUT.PR Intln ("Pre-notification is executed");}}


15. Create a post-notification implementation class Methodafter, package name (Com.mycompany.shequ.service.impl) under Src/main/java

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/8F/2D/wKiom1jWE6qgWg1qAAB7lujWKUY764.png-wh_500x0-wm_ 3-wmp_4-s_67473826.png "title=" Qq20170325145138.png "alt=" Wkiom1jwe6qgwg1qaab7lujwkuy764.png-wh_50 "/>


16. Post-Notification implementation class Methodafter content as follows
Package Com.mycompany.shequ.service.impl;import Java.lang.reflect.method;import org.springframework.aop.afterreturningadvice;/** * Notification After completion of method execution * @author Administrator * */public class Methodafter Implements Afterreturningadvice {public void afterreturning (Object arg0, Method arg1, object[] arg2,object arg3) throws Th rowable {System.out.println ("Post notification is executed");}}


17. Create exception notification implementation Class ThrowException, package name (Com.mycompany.shequ.service.impl) under Src/main/java

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8F/2B/wKioL1jWFA_gZh0TAAB6kk596Xs692.png-wh_500x0-wm_ 3-wmp_4-s_1049567979.png "title=" Qq20170325145320.png "alt=" Wkiol1jwfa_gzh0taab6kk596xs692.png-wh_50 "/>


18. Exception notification implementation class ThrowException content is as follows
Package Com.mycompany.shequ.service.impl;import org.springframework.aop.throwsadvice;/** * Notify when exception is thrown * @author Administrator * */public class ThrowException implements Throwsadvice {public void afterthrowing ( IllegalArgumentException e) throws Throwable{system.out.println ("exception notification executed");}}


19. Create a surround Notification implementation class Aroundmethod under Src/main/java, package name (COM.MYCOMPANY.SHEQU.SERVICE.IMPL)

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/8F/2B/wKioL1jWFHmwCNoXAAB6IqjCuOI786.png-wh_500x0-wm_ 3-wmp_4-s_537276557.png "title=" Qq20170325145504.png "alt=" Wkiol1jwfhmwcnoxaab6iqjcuoi786.png-wh_50 "/>


20. Surround notification Implementation class Aroundmethod content as follows
Package Com.mycompany.shequ.service.impl;import Java.util.arrays;import Org.aopalliance.intercept.methodinterceptor;import org.aopalliance.intercept.methodinvocation;/** * Surround notification * @author Administrator * */public class Aroundmethod implements Methodinterceptor {public Object Invoke (Methodinvocation method) T Hrows throwable {System.out.println ("Surround notification is executed"); System.out.println ("Method name:" +method.getmethod (). GetName ()); System.out.println ("Method parameter:" +arrays.tostring (Method.getarguments ())); Object result = Method.proceed (); return result;}}


21. Under Src/main/resource, create the core configuration file Applicationcontext.xml,

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8F/2D/wKiom1jWFM2zhPnCAABCdE6e7So804.png-wh_500x0-wm_ 3-wmp_4-s_3701593708.png "title=" Qq20170325145625.png "alt=" Wkiom1jwfm2zhpncaabcde6e7so804.png-wh_50 "/>


22. configuration file Applicationcontext.xml,
<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:context="/HTTP/ Www.springframework.org/schema/context "xmlns:util=" Http://www.springframework.org/schema/util "xsi: Schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ spring-beans-4.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/ spring-util-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/ Spring-context-4.0.xsd "><!-- customer dao bean --><bean id=" CustomerDao "  class= "Com.mycompany.shequ.dao.impl.CustomerDaoImpl" ></bean><!-- customer  Business Layer  bean --><bean id= "CustomerService"  class= " Com.mycompany.shequ.service.impl.CustomerServiceImpl "><property name=" Customerdao " ref=" CustomeRdao "></property></bean><!--  method before execution  --><bean id=" Methodbeforebean "  class= "Com.mycompany.shequ.service.impl.MethodBefore" ></bean><!--  method execution notification after completion  --> <bean id= "Methodafterbean"  class= "Com.mycompany.shequ.service.impl.MethodAfter" ></bean> <!--  Notify  --><bean id= "Throwexceptionbean"  class= "after throwing an exception" Com.mycompany.shequ.service.impl.ThrowException "></bean><!--  Surround notification  --><bean id = "Aroundmethodbean"  class= "Com.mycompany.shequ.service.impl.AroundMethod" ></bean><bean  Id= "Customerserviceproxy"  class= "Org.springframework.aop.framework.ProxyFactoryBean" ><property  name= "target"  ref= "CustomerService" ></property><property name= "InterceptorNames" ><list><!-- methodbeforebean is the name of the predecessor notification Bean  --><value>methodbeforebean</value ><!-- methodafterbean is the name of the post-notification bean  --><value>methodafterbean</value><!-- throwexceptionbean is the name of the exception notification Bean  --><value >throwExceptionBean</value><!-- aroundmethodbean is the name of the surround notification bean  --><value> Aroundmethodbean</value></list></property></bean></beans>


23. Create a test file under Src/test/java apptest, package name (com.mycompany.shequ.test)

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/8F/2B/wKioL1jWFQ2QaIwDAABJz1SwNtg213.png-wh_500x0-wm_ 3-wmp_4-s_1144667781.png "title=" Qq20170325145733.png "alt=" Wkiol1jwfq2qaiwdaabjz1swntg213.png-wh_50 "/>


24. The contents of the test file apptest are as follows
Package Com.mycompany.shequ.test;import Org.junit.test;import Org.springframework.context.configurableapplicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import    Com.mycompany.shequ.service.icustomerservice;public class Apptest {@Testpublic void Beantest () { Configurableapplicationcontext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); CustomerService customerservice = (icustomerservice) context.getbean ("Customerserviceproxy"); Customerservice.showme ();}}


25. Right-click on the Beantest method of the test class apptest and output the result

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/8F/2B/wKioL1jWFX_iEAVdAAEMJ6W8b3Q344.png-wh_500x0-wm_ 3-wmp_4-s_2223817579.png "title=" Qq20170325145926.png "alt=" Wkiol1jwfx_ieavdaaemj6w8b3q344.png-wh_50 "/>

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8F/2D/wKiom1jWFYmAlsKYAABwCKMO5XE979.png-wh_500x0-wm_ 3-wmp_4-s_825644703.png "title=" Qq20170324102905.png "alt=" Wkiom1jwfymalskyaabwckmo5xe979.png-wh_50 "/>

This article from the "Vegetarian Yan" blog, declined to reprint!

SPRING4-AOP Notice

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.