标签:AOP
概念解释1、AOP面向方面的编程。就是讲编程逻辑拆分成不同的方法。而这些方法就是横切关注点。注意,AOP编程的前提条件是OOP编程。
2、Aspect就是对横切关注点(method)的通用额外业务操作。如LoggingAspect.
3、Join point就是单个的方法,即是横切关注点。如:student.getAget();
4、Advice就是横切关注点的通知类型。如:before、after等。
5、Pointcut就是N个横切关注点。如:
<aop:pointcut id="selectAll" expression="execution( com.demo.enity.Student.(..))"/>
6、Target object关注的对象。如:Student对象。
XML实例
一、被切面的目标对象
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
System.out.println("Age : " + age);
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
System.out.println("Name : " + name);
return name;
}
public void printThrowException() {
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}
二、切面module
public class LoggingAspect {
public void beforeAdvice() {
System.out.println("Going to setup student profile.");
}
public void afterAdvice() {
System.out.println("Student profile has been setup.");
}
public void afterReturningAdvice(Object retVal) {
System.out.println("Returning:" + retVal.toString());
}
public void AfterThrowingAdvice(IllegalArgumentException ex) {
System.out.println("There has been an exception: " + ex.toString());
}
public void aroundAdvice() {
System.out.println("Student profile around setup.");
}
}
三、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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<bean id="logging" class="com.demo.aop.LoggingAspect"/>
<bean id="student" class="com.demo.enity.Student">
<property name="age" value="18"/>
<property name="name" value="Jack"/>
</bean>
<aop:config>
<aop:aspect id="log" ref="logging">
<aop:pointcut id="selectAll" expression="execution(* com.demo.enity.Student.*(..))"/>
<aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
<aop:after pointcut-ref="selectAll" method="afterAdvice" />
<!--<aop:around pointcut-ref="selectAll" method="aroundAdvice"/>-->
<aop:after-returning pointcut-ref="selectAll" returning="retVal" method="afterReturningAdvice"/>
<aop:after-throwing pointcut-ref="selectAll" throwing="ex" method="AfterThrowingAdvice"/>
</aop:aspect>
</aop:config>
</beans>
四、测试
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
Student student = context.getBean(Student.class);
student.getName();
student.getAge();
student.printThrowException();
}
五、依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
注解实例
一、被切面的目标对象
@Component
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
System.out.println("Age : " + age);
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
System.out.println("Name : " + name);
return name;
}
public void printThrowException() {
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}
二、切面module
@Aspect
@Component
public class AnnotatedLogging {
@Pointcut("execution(* com.demo.enity.Student.getName(..))")
private void getName() {
}
@Pointcut("execution(* com.demo.enity.Student.getAge(..))")
private void getAge() {
}
@Pointcut("execution(* com.demo.enity.Student.*(..))")
private void selectedAll() {
}
@Before("getName()")
public void doBeforeTask() {
}
@After("getAge()")
public void doAfterTask() {
}
@AfterReturning(pointcut = "getName()", returning = "retVal")
public void doAfterReturnningTask(Object retVal) {
System.out.println("AnnotatedLogging-->Returning:" + retVal.toString());
}
@AfterThrowing(pointcut = "selectedAll()", throwing = "ex")
public void doAfterThrowingTask(Exception ex) {
System.out.println("AnnotatedLogging-->There has been an exception: " + ex.toString());
}
@Around("getAge()")
public void doAroundTask() {
System.out.println("AnnotatedLogging-->Student profile around setup.");
}
}
三、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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<context:component-scan base-package="com.demo"/>
<aop:aspectj-autoproxy/>
</beans>
四、测试
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-annotation.xml");
Student student = context.getBean(Student.class);
student.getName();
student.getAge();
student.printThrowException();
}
五、依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
参考:https://www.tutorialspoint.com/spring/aop_with_spring.htm
标签:AOP
原文地址:http://blog.51cto.com/881206524/2120292