Zbb_ioc_aop
Https://pan.baidu.com/disk/home#/all?vmode=list&path=%2Fcode%2Fspring
Pom.xml
<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/maven-v4_0_0.xsd" ><modelversion >4.0.0</modelversion><groupid>com.zbb</groupid><artifactid>zbb_test01</ Artifactid><packaging>war</packaging><version>0.0.1-snapshot</version><name> zbb_test01 Maven webapp</name><url>http://maven.apache.org</url><dependencies><!-- Https://mvnrepository.com/artifact/junit/junit--><dependency><groupid>junit</groupid>< artifactid>junit</artifactid><version>4.12</version><scope>test</scope></ dependency><!--Https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api--><dependency ><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId>< Version>4.0.1</version><scope>provided</scope></dependency><!--https://mvnrepository.com/artifact/ Org.springframework/spring-webmvc--><dependency><groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid><version>5.0.8.release</version></dependency ><!--AOP Aspect Annotation Guide Package--><dependency><groupid>org.aspectj</groupid><artifactid> aspectjrt</artifactid><version>1.8.6</version></dependency><dependency>< groupid>org.aspectj</groupid><artifactid>aspectjweaver</artifactid><version>1.8.9 </version></dependency></dependencies><build><finalname>zbb_test01</finalname ></build></project>
Xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "Webapp_ ID "version=" 3.0 "><display-name>springMVC</display-name><welcome-file-list>< welcome-file>index.jsp</welcome-file></welcome-file-list><!--Spring Configuration--><context-param ><param-name>contextconfiglocation</param-name><param-value>classpath: Applicationcontext.xml</param-value></context-param><listener><listener-class> org.springframework.web.context.contextloaderlistener</listener-class></listener><!--Spring-mvc Configure--><servlet><servlet-name>spring-mvc</servlet-name><servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlEt-class><init-param><param-name>contextconfiglocation</param-name><param-value> Classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup ></servlet><servlet-mapping><servlet-name>spring-mvc</servlet-name><url-pattern >/</url-pattern></servlet-mapping></web-app>
Applicationcontext.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" xmlns:tx= "Http://www.springframework.org/schema/tx" Xmlns:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA" xmlns:security= "http://www.springframework.org/ Schema/security "xsi:schemalocation=" http://www.springframework.org/schema/aophttp://www.springframework.org/ schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/securityhttp://www.springframework.org/ schema/security/spring-security-3.2.xsdhttp://www.springframework.org/schema/beanshttp:// www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/data/jpahttp:// www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/txhttp:// Www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/ Spring-context-4.0.xsd "><!--activates annotation-based bean configuration--><context: Annotation-config/><!--Scans for application @Components to deploy--><context:component-scan Base-package= "COM.ZBB"/></beans>
Spring-mvc.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:mvc= "Http://www.springframework.org/schema/mvc" xmlns:context= "Http://www.springframework.org/schema/context" xsi: Schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ Spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context /spring-context-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/ SPRING-MVC-4.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.3.xsd "> <!--Scan Controller class--><context:component-scan base-package=" COM.ZBB "/><!-- Start ASPECTJ support only--><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!--enabled for scanned beans-->< Mvc:annotation-driven/><!--JSP View parser--><bean id= "Jspviewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" Viewclass "value=" Org.springframework.web.servlet.view.JstlView "/><property name=" prefix "value="/web-inf/views/"/>< Property name= "suffix" value= ". JSP"/></bean></beans>
Loggeraspect.java
Package Com.zbb.aspect;import Java.util.arrays;import Java.util.list;import org.aspectj.lang.joinpoint;import Org.aspectj.lang.proceedingjoinpoint;import Org.aspectj.lang.annotation.after;import Org.aspectj.lang.annotation.afterreturning;import Org.aspectj.lang.annotation.afterthrowing;import Org.aspectj.lang.annotation.around;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import Org.aspectj.lang.annotation.pointcut;import Org.springframework.core.annotation.order;import org.springframework.stereotype.Component; @Aspect// The label declares the Loggeraspect class as a slice @order (1)//sets the priority of the tangent plane: if there are multiple facets, you can control the order of execution of the slices by setting the priority (the smaller the value, the higher the priority) @Component// This tag puts the Loggeraspect class into the IOC container public class Loggeraspect {/** * defines a method for declaring pointcut expressions, which generally do not need to add additional code to declare pointcut expressions using @pointcut * The following notification uses the method name directly to refer to the current pointcut expression, and if other classes are used, add the package name */@Pointcut ("Execution (public * com.zbb.controller.*.* (..))") public void Declearjoinpointexpression () {} @Pointcut ("Execution (public * com.zbb.service.*.* (..))") public void DeclearjoinpoIntExpression2 () {}/** * pre-notification * * @param joinpoint */@Before ("Declearjoinpointexpression ()")//the label declares that the secondary method is a pre-notification: At the beginning of the target method Former execution public void Beformethod (Joinpoint joinpoint) {String methodName = Joinpoint.getsignature (). GetName (); list<object> args = Arrays.aslist (Joinpoint.getargs ()); System.out.println ("This method" + MethodName + "begin. param< "+ args +" > ");} /** * Post notification (does not access the return value of the method) * * @param joinpoint */@After ("Declearjoinpointexpression ()") public void, regardless of whether the method has an exception Aftermethod (Joinpoint joinpoint) {String methodName = Joinpoint.getsignature (). GetName (); System.out.println ("This method" + MethodName + "end."); /** * Return notification (code that executes at normal end of method) returns a notification that can access the return value of the method! * * @param joinpoit */@AfterReturning (value = "declearjoinpointexpression ()", returning = "result") public void Afterretur Nmethod (Joinpoint joinpoint, Object result) {String methodName = Joinpoint.getsignature (). GetName (); System.out.println ("This method" + MethodName + "end.result<" + result + ">");} /** * Exception Notification (method hairThe code that executes the exception) can access the exception object, and you can specify code that executes when a specific exception occurs * @param joinpoint * @param ex */@AfterThrowing (value = "declearjoinpointexpress Ion () ", throwing =" ex ") public void Afterthrowingmethod (Joinpoint joinpoint, Exception ex) {String methodName = joinpoint. Getsignature (). GetName (); System.out.println ("This method" + MethodName + "End.ex message<" + ex + ">");} /** * Surround notification (need to carry a parameter of type Proceedingjoinpoint type) * Surround notification contains a front, back, return, exception notification, Proceedingjoinpoin type parameters can decide whether to execute the target method * and the surround notification must have a return value, The return value is the return value of the target method * * @param joinpoint */@Around (value = "declearjoinpointexpression ()") Public Object Aroundmethod ( Proceedingjoinpoint point) {Object result = null; String methodName = Point.getsignature (). GetName (); try {//Pre-Notification System.out.println ("the method" + MethodName + "start.") param< "+ arrays.aslist (Point.getargs ()) +" > ");//execute target method result = Point.proceed ();//Return Notification SYSTEM.OUT.PRINTLN (" the Method "+ MethodName +" end. result< "+ result +" > ");} catch (Throwable e) {//Exception notification SYSTEM.OUT.PRINTLN ("this method" +MethodName + "End.ex message<" + E + ">"); throw new RuntimeException (e);} Post Notification System.out.println ("the method" + MethodName + "end."); return result;} /** * PRE-notification * * @param joinpoint */@Before ("DeclearJoinPointExpression2 ()")//the label declares that the secondary method is a pre-notification: executes public void before the target method starts BEFO RMETHOD2 (Joinpoint joinpoint) {String methodName = Joinpoint.getsignature (). GetName (); list<object> args = Arrays.aslist (Joinpoint.getargs ()); System.out.println ("This method" + MethodName + "begin. param< "+ args +" > ");} /** * Post notification (does not access the return value of the method) * * @param joinpoint */@After ("DeclearJoinPointExpression2 ()") public void, regardless of whether the method has an exception AFTERMETHOD2 (Joinpoint joinpoint) {String methodName = Joinpoint.getsignature (). GetName (); System.out.println ("This method" + MethodName + "end."); /** * Return notification (code that executes at normal end of method) returns a notification that can access the return value of the method! * * @param joinpoit */@AfterReturning (value = "DeclearJoinPointExpression2 ()", returning = "result") public void Afterretu RNMETHOD2 (Joinpoint joinpoint, Object result) {String metHodname = Joinpoint.getsignature (). GetName (); System.out.println ("This method" + MethodName + "end.result<" + result + ">");} /** * Exception Notification (the code in which the method takes exception execution) can access the exception object, and you can specify code that executes when a specific exception occurs * * @param joinpoint * @param ex */@AfterThrowing (value = "Declearjo InPointExpression2 () ", throwing =" ex ") public void AfterThrowingMethod2 (Joinpoint joinpoint, Exception ex) {String MethodName = Joinpoint.getsignature (). GetName (); System.out.println ("This method" + MethodName + "End.ex message<" + ex + ">");} /** * Surround notification (need to carry a parameter of type Proceedingjoinpoint type) * Surround notification contains a front, back, return, exception notification, Proceedingjoinpoin type parameters can decide whether to execute the target method * and the surround notification must have a return value, The return value is the return value of the target method * * @param joinpoint */@Around (value = "declearJoinPointExpression2 ()") Public Object aroundMethod2 ( Proceedingjoinpoint point) {Object result = null; String methodName = Point.getsignature (). GetName (); try {//Pre-Notification System.out.println ("the method" + MethodName + "start.") param< "+ arrays.aslist (Point.getargs ()) +" > ");//execute target method result = Point.proceed ();//Return notification SYStem.out.println ("the method" + MethodName + "end. result< "+ result +" > ");} catch (Throwable e) {///exception Notification SYSTEM.OUT.PRINTLN ("This method" + MethodName + "End.ex message<" + E + ">"); throw new RuntimeException (e);} Post Notification System.out.println ("the method" + MethodName + "end."); return result;}}
Testcontroller.java
Package Com.zbb.controller;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.restcontroller;import com.zbb.service.testservice;@ Restcontroller@requestmapping ("Test") public class TestController {@AutowiredTestService testservice;@ Requestmapping ("T1") public void T1 () {testservice.t2 ();}}
Testservice.java
Package Com.zbb.service;import Org.springframework.stereotype.Service, @Servicepublic class Testservice {public void T2 () {System.out.println ("Asdfafasdf");}}
zbb20180824 Spring IOC AOP Example