为了能够使程序横向扩展,又不需要去改动现有的程序,aop就出来了
举个例子:必如说我们把代码都写完了,需要在某些功能执行后打印一些日志看看,这时候你难道一个一个文件的去改吗?aop就可以帮助我们直接切入,而不需要去改现有的文件,就像一把尖刀------>直入心脏
来先把依赖导了..
<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.wangjn</groupId>
<artifactId>spring-aop-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-aop-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring aop 编织依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</project>
介绍一下配置aop的两种方式:
第一种,使用 aop:aspect
首先,先建个接口
package com.wangjn;
import org.springframework.stereotype.Component;
/**
* Created by Administrator
*/
public interface MyAopDemo {
public void aopTest();
}
实现类:
package com.wangjn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Created by Administrator
*/
@Component
public class MyAopDemoImpl implements MyAopDemo{
private static final Logger logger = LoggerFactory.getLogger(MyAopDemoImpl.class);
@Override
public void aopTest() {
logger.info("Hello World!");
}
}
写个 横切点,也就是我们要切进的具体执行类:
package com.wangjn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Created by Administrator
*/
@Component("aspectTest")
public class AspectTest {
private static final Logger logger = LoggerFactory.getLogger(MyAopDemoImpl.class);
public void doit(){
logger.info("使用aspect 配置的AOP!");
}
}
重点在于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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<context:component-scan base-package="com"/>
<aop:config>
<aop:aspect id="log" ref="aspectTest">
<aop:pointcut id="addAllMethod" expression="execution(* com.wangjn.MyAopDemo.*(..))" />
<aop:before method="doit" pointcut-ref="addAllMethod" />
<aop:after method="doit" pointcut-ref="addAllMethod" />
</aop:aspect>
</aop:config>
</beans>
再来写个测试类:
package com.wangjn;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator
*/
public class Start {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
MyAopDemo demo = (MyAopDemo) context.getBean("myAopDemoImpl");
demo.aopTest();
}
}
执行 main 方法,完美切入..
再来说说使用advisor的aop 实现,其实与上面的方法最大的不同在于需要自己去实现各种 advice(通知)
advice 类型有以下几种:
- 前置(BeforeAdvice):在目标方法执行前实施
- 后置(AfterReturningAdvice):在目标方法执行后实施
- 环绕(MrthodInterceptor):在目标方法执行前后实施
- 异常抛出(ThrowsAdvice):在目标方法抛出异常后实施
- 引介(IntroductionIntercrptor):在目标类中添加一些新的方法和属性
这里我们去实现一下 AfterReturningAdvice 也就是 目标方法后执行
package com.wangjn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* Created by Administrator
*/
@Component
public class AdviceTest implements AfterReturningAdvice{
private static final Logger logger = LoggerFactory.getLogger(AdviceTest.class);
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
logger.info("使用advisor配置的AOP!");
}
}
改一下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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<context:component-scan base-package="com"/>
<!-- 使用aspect 实现aop 与下面 advisor 配置的两者选其一(事务配置一般用advisor的)
<aop:config>
<aop:aspect id="log" ref="aspectTest">
<aop:pointcut id="addAllMethod" expression="execution(* com.wangjn.MyAopDemo.*(..))" />
<aop:before method="doit" pointcut-ref="addAllMethod" />
<aop:after method="doit" pointcut-ref="addAllMethod" />
</aop:aspect>
</aop:config>
-->
<aop:config>
<aop:pointcut id="addAllMethod" expression="execution(* com.wangjn.MyAopDemo.*(..))" />
<aop:advisor advice-ref="adviceTest" pointcut-ref="addAllMethod" ></aop:advisor>
</aop:config>
</beans>
执行测试一下成功织入
image.png
网友评论