SpringAOP之通过注解方式实现

时间:2022-10-06 13:57:53

通过注解的方式
1)导入jar包

<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.3.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.3.Release</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.3.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.3.Release</version>
</dependency>




<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>




<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>



<!-- 配置servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<!--配置jsp的依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>

<!-- 配置jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

<!-- 配置的 spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>

<!-- 事务相关的架包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.3.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.1.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>

2) 创建目标类

public interface IUserService {

public void add();

public void update();

public void delete();

public void select();


}

package com.test.service.impl;

import com.test.service.IUserService;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {

@Override
public void add() {
System.out.println("这是添加用户");
}

@Override
public void update() {
System.out.println("这是修改用户");
}

@Override
public void delete() {
System.out.println("这是删除用户");
}

@Override
public void select() {
System.out.println("这是查询用户");
}
}

3)创建配置文件 applicationContext-aop-user.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: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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">


<!-- 扫描注解所在的位置 -->
<context:component-scan base-package="com.test" />


<!-- 使用aop注解方式-->
<aop:aspectj-autoproxy ></aop:aspectj-autoproxy>

</beans>

4)创建切面类 (增强+切点)

//切面类 (增强类+切点)
@Aspect
public class UserAdvice {

//定义切点
@Pointcut("execution(* com..User*.*(..))")
public void pc()
{
}

//在所有包中以user开头的类中所有的方法 成功返回以后 切入 afterReturningMethod()方法
//@AfterReturning("execution(* com..User*.*(..))")
@AfterReturning("UserAdvice.pc()")
public void afterReturningMethod()
{
System.out.println("方法执行成功");
}

//@AfterThrowing("execution(* com..User*.*(..))")
@AfterThrowing("UserAdvice.pc()")
public void afterThrowingMethod()
{
System.out.println("出异常");
}

//环绕通知
@Around("execution(* com..UserDao.select(..))")
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("查询所有用户信息");
return point.proceed();// 等价于userDao.select()
}
}

修改配置文件

创建一个切面类对象

<?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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">


<!-- 扫描注解所在的位置 -->
<context:component-scan base-package="com.test" />


<!-- 创建切面类对象-->
<bean class="com.test.aspects.UserAdvice"></bean>

<!-- 使用aop注解方式-->
<aop:aspectj-autoproxy ></aop:aspectj-autoproxy>

</beans>

6)测试

@Test
public void fun()
{
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("/applicationContext-aop-user.xml");

IUserDao userDao= classPathXmlApplicationContext.getBean("userDao",IUserDao.class);

try {
userDao.add();
} catch (Exception e) {
e.printStackTrace();
}

userDao.select();
}