切面類
package com.test.advice;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
//切面類
@Aspect
public class MyAdvice {
//定義切點表達(dá)式
@Pointcut("execution(* com.test.service.impl.*.add(..))")
public void pc()
{
}
//@Before("execution(* com.test.service.impl.*.add(..))")
@Before("MyAdvice.pc()")
//將這個增強方法切入到service層的add方法前
public void before()
{
System.out.println("添加用戶之前");
}
//目標(biāo)方法執(zhí)行后(不管是出異常還是成功執(zhí)行)
@After("MyAdvice.pc()")
public void after()
{
System.out.println("添加用戶之后");
}
//環(huán)繞通知,用這個增強代碼替換掉目標(biāo)方法
@Around("MyAdvice.pc()")
public void around(ProceedingJoinPoint point) throws Throwable {
System.out.println("執(zhí)行目標(biāo)方法之前");
point.proceed(); //放行切點處的方法(目標(biāo)方法)
}
//目標(biāo)方法成功執(zhí)行后
@AfterReturning("MyAdvice.pc()")
public void afterReturning()
{
System.out.println("目標(biāo)方法成功執(zhí)行后");
}
//目標(biāo)方法出異常
@AfterThrowing("MyAdvice.pc()")
public void afterThrowing()
{
System.out.println("目標(biāo)方法出異常以后才執(zhí)行");
}
}
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.test" />
<!-- 創(chuàng)建切面類對象 -->
<bean id="myAdvice" class="com.test.advice.MyAdvice" />
<!--開啟自動代理 -->
<aop:aspectj-autoproxy>
</aop:aspectj-autoproxy>
</beans>
依賴
<?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>org.example</groupId>
<artifactId>testSpring07</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>testSpring07 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</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-context</artifactId>
<version>4.3.18.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.18.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.18.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- aop -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>testSpring07</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
目標(biāo)類
package com.test.service.impl;
import com.test.service.IUsersService;
import org.springframework.stereotype.Service;
@Service
public class UsersService implements IUsersService {
@Override
public void add() {
System.out.println("添加用戶...");
}
@Override
public void update() {
System.out.println("修改用戶...");
}
@Override
public void delete() {
System.out.println("刪除用戶...");
}
}
測試類
package com.test.aop;
import com.test.service.IUsersService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
@Test
public void test()
{
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
IUsersService usersServiceProxy= applicationContext.getBean("usersService",IUsersService.class);
try {
usersServiceProxy.add();
} catch (Exception e) {
e.printStackTrace();
}
}
}
測試結(jié)果
文章來源地址http://www.zghlxwxcb.cn/news/detail-696664.html
文章來源:http://www.zghlxwxcb.cn/news/detail-696664.html
到了這里,關(guān)于javaee spring aop 注解實現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!