This article is connected: https://www.cnblogs.com/qzhc/p/11969734.html
Next, I will use a very simple example
1, built environment
1.1, the first step: to prepare the necessary code
Business layer codes:
AccountServiceImpl.java
package com.henu.service.impl; import com.henu.service.AccountService; public class AccountServiceImpl implements AccountService { public void saveAccount() { System.out.println("执行了保存"); } }
Test categories:
AOPtest.java
package com.henu.test; import com.henu.service.AccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AOPtest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); AccountService as = (AccountService) ac.getBean("accountService"); as.saveAccount(); } }
1.2, introducing the required jar package
My demo is managed by maven: pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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>4.0.0</modelVersion> <groupId>com.henu</groupId> <artifactId>day03_spring_adviceType</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> </dependencies> </project>
1.3, create a configuration file and import spring ioc constraints and configuration of spring
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置spring的Ioc,把service对象配置起来--> <bean id="accountService" class="com.henu.service.impl.AccountServiceImpl"></bean>
1.4, write a class as a public notice
logger.java
Package com.henu.utils; // log public class Logger { / ** * Pre-notification * * / public void befterPrintLog () { System.out.println ( "pre-notification starts logging the logger" ); } / ** * rear notification * * / public void afterReturningPrintLog () { System.out.println ( "post-start logging the notification logger" ); } / ** * abnormality notification * * / public void afterThrowingPrintLog () { System.out.println ("Abnormality notification starts logging the logger" ); } / ** * Final Notice * * / public void afterPrintLog () { System.out.println ( "final notification starts logging the logger" ); } }
2, aop configuration steps (give the code, speaking)
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置spring的Ioc,把service对象配置起来--> <bean id="accountService" class= "com.henu.service.impl.AccountServiceImpl" > </ the bean > <-! -> <-! configuration logger class -> < the bean ID = "logger" class = "com.henu.utils. Logger " > </ the bean > <-! configure the AOP -> < AOP: config > ! <- configuration cut -> < AOP: Aspect ID =" logAdvice " REF =" Logger " > ! <- configuration type of the notification, the notification method and associated methods and the starting point -> <! - configuration pre-notification: the method is performed prior to entry point -> <aop:before method= "befterPrintLog" the pointcut = "Execution (com.henu.service.impl * * * (..)..)" > </ AOP: before > <-! arranged after advice: normal execution of the method after the pointcut . It is performed with only one exception -> < AOP: After returning- Method = "afterReturningPrintLog" the pointcut = "Execution (com.henu.service.impl * * * (..)..)" > </ AOP: after- returning > ! <- configuration abnormality notification: after abnormal execution entry point method. It performs a post only -> < AOP: After the throwing- Method = "afterThrowingPrintLog" the pointcut = "Execution (com.henu.service.impl * * * (..)..)" > </ AOP: The final configuration notification: normally performed regardless of whether the starting point, it will perform at its rear -> < AOP: After Method = "afterPrintLog" the pointcut = "Execution (com.henu.service.impl * * * (..).. ) " > </ AOP: After > </ AOP: Aspect > </ AOP: config > </ Beans >
2.1, together with the notification class configuration bean tag
<!--配置logger类 --> <bean id="logger" class="com.henu.utils.Logger"></bean>
2.2, using the aop: config statement aop configuration
aop:config:
Role : Configuration for declaring the start aop
<aop:config>
<! - configuration code is written here ->
</aop:config>
2.3, using aop: aspect the configuration section
aop: aspect: action: a configuration section. Properties: the above mentioned id: section to provide a unique identification. ref: reference id configured notification class bean. < AOP: Aspect ID = "logAdvice" REF = "Logger" > <-! Type Configuration write notification here -> </ AOP: Aspect >
2.4, using aop: xxx configuration corresponding to the type of notification
aop: before action: a pre-configured notification. Enhanced specified entry point method is performed before attributes: Method: name specifies the notification method for enhancing class ponitcut-ref: for expression pointcuts reference poinitcut: pointcut expression for specifying point execution time : executed before performing the method pointcut <AOP: beforemethod= "beginTransaction"the pointcut-REF= "PT1"/> AOP: after returning- action: to configure notification rear properties: method: Specifies the name of the notification process. pointct: define a pointcut expression pointcut-ref: reference expression pointcuts execution time point: pointcut after the method is normally performed. It performs only one abnormality notification<AOP: After returning-Method= "the commit"
REF-the pointcut = "PT1" /> AOP: After the throwing- effect: for configuring abnormality notification attribute: Specifies the name of the notification method: method. pointct: define a pointcut expression pointcut-ref: reference expression pointcuts execution time point: pointcut method of execution after an exception. It can only perform a post-notification < AOP: After the throwing- Method = "ROLLBACK" the pointcut-REF = "PT1" /> AOP: After role: for configuring notifications final properties: Method: Specifies the name of the notification process. pointct: define a pointcut expression pointcut-ref: reference expression pointcuts execution time points: whether there is abnormal, it will perform at its rear Whether entry point method is performed. < AOP: the After
method="release" pointcut-ref="pt1"/>
2.5, an entry point expression Description
execution perform matching method (common):
execution (expression)
expression syntax: execution ([Modifier] return type package name class name method name (parameter).)
writing Description: match-mode :
public void COM. henu.service.impl.AccountServiceImpl.saveAccount () access modifiers may be omitted void com.itheima.service.impl.AccountServiceImpl.saveAccount () returns the value may be used asterisk denotes any return value * com.henu.service.impl. AccountServiceImpl.saveAccount () package name can use the asterisk denotes any package, but there are a few levels package, you need to write a few *. * *. *. *. *. AccountServiceImpl.saveAccount () use .. to indicate the current package, its subpacket * com..AccountServiceImpl.saveAccount () class and method names may be used to implement wildcard asterisk * .. * *. * () parameter list can use the * represent the parameters may be any data type, but must there are parameters * com .. *. * (* ) parameter list can be used .. parameters can indicate whether there can be any type of parameter
. * Com .. * * (..
) the whole way wildcard:
* * .. * * (..)
Note:
Under normal circumstances, we are all on the method of enhanced business layer, so the starting point expressions are business layer is cut to the implementation class.
Execution ( * com.henu.service.impl. *. * (..) )
Mentioned here are basically similar, and less to what later supplemented.