SpringAop的三种简单实现方式
文章目录
maven所需的依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<build>
<!--在进行模块化开发打jar包时,maven会将非java文件过滤掉,
xml,properties配置文件等,但是这些文件又是必需的,
使用此配置可以在打包时将不会过滤这些必需的配置文件。
-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<!--配置打包时不过滤非java文件结束 -->
</build>
示例类如下
UserService接口
public interface UserService {
void addUser();
void deleteUser();
void UpdateUser();
void selectUser();
}
UserServiceImpl.java
public class UserServiceImpl implements UserService{
@Override
public void addUser() {
System.out.println("调用增加方法");
}
@Override
public void deleteUser() {
System.out.println("调用删除方法");
}
@Override
public void UpdateUser() {
System.out.println("调用更改方法");
}
@Override
public void selectUser() {
System.out.println("调用删除方法");
}
第一种:切面类实现Spring定义好的接口
切面类
1.Beforelog.java
public class BeforeLog implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(method.getName());
}
}
2.AfterLog.java
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println(method.getName());
}
}
Spring配置文件
Application.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"
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">
<bean class="com.yan.service.UserServiceImpl" id="userService"/>
<bean class="com.yan.log.AfterLog" id="afterLog"/>
<bean class="com.yan.log.BeforeLog" id="beforeLog"/>
<!--config:声明一个Aop配置-->
<aop:config>
<!--声明一个插入点及插入点的位置-->
<aop:pointcut id="pointcut" expression="execution(* com.yan.service.UserServiceImpl.*(..))"/>
<!--为这个插入点 配置切面类-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
第二种:自定义切面类(xml实现)
切面类
method.java
public class MethodLog {
public void beforLog(){
System.out.println("==========这是方法执行之前==========");
}
public void afterLog(){
System.out.println("==========这是方法执行之后==========");
}
}
Spring配置文件
Application.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--将示例类和切面类都注入到IOC 容器中-->
<bean id="userService" class="com.yan.service.UserServiceImpl"/>
<bean id="methodLog" class="com.yan.log.MethodLog"/>
<!--声明Aop配置-->
<aop:config>
<!--声明切面 并指定切面类-->
<aop:aspect ref="methodLog">
<aop:pointcut id="poincut" expression="execution(* com.yan.service.UserServiceImpl.*(..))"/>
<!--为不同的方法配置前置增强,后置增强-->
<aop:before method="beforLog" pointcut-ref="poincut"/>
<aop:after method="afterLog" pointcut-ref="poincut"/>
</aop:aspect>
</aop:config>
</beans>
第三种:自定义切面类(注解实现)
切面类
log.java
@Component
//配置切面
@Aspect
public class log {
//使用配置前置挣钱Before
@Before("execution(* com.yan.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("调用方法之前");
}
//配置后置增强
@After("execution(* com.yan.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("调用方法之后");
}
}
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:contetx="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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注解 扫描-->
<contetx:component-scan base-package="com.yan"/>
<!--开启Aop 注解支持-->
<aop:aspectj-autoproxy/>
</beans>