1.通过配置文件织入
1)导入jar包
<!-- 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> <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.3.18.RELEASE</version> </dependency> <!-- 事务相关的架包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.18.RELEASE</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> <!-- 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>
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)创建增强类
package com.test.aspects; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.Date; //创建一个增强类 public class MyAdvice { public void printTime() { String time= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()); System.out.println( "准备在目标方法的前面打印时间:"+time); } //无论是否成功 都会执行 public void printTimeAfter() { String time= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()); System.out.println( "准备在目标方法的后面打印时间:"+time); } //环绕通知 //环绕通知会替代原来的目标方法,如果没有放行目标方法 会导致目标方法只执行 public void round(ProceedingJoinPoint point) throws Throwable { //查询方法 System.out.println( "准备在目标方法中添加环绕通知"); if(false) point.proceed(); //放行目标方法 } //成功后以后才执行 //如果配置了after通知 则afterReturning不会被执行 public void afterReturning() { System.out.println("目标方法成功运行后"); } //出异常后以后才执行 //如果配置了after通知 则exception不会被执行 public void exception() { System.out.println("出异常了"); } }
4)在配置文件中织入
<?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 id="myAdvice" class="com.test.aspects.MyAdvice"></bean> <!-- 配置aop --> <aop:config> <!-- 创建切点 --> <aop:pointcut id="pc" expression="execution(* com.test.service.impl.*Service.*(..))"/> <!-- 织入 --> <aop:aspect ref="myAdvice"> <aop:before method="printTime" pointcut-ref="pc" /> <!--<aop:after method="printTimeAfter" pointcut-ref="pc" />--> <aop:around method="round" pointcut-ref="pc" /> <aop:after-returning method="afterReturning" pointcut-ref="pc" /> <aop:after-throwing method="exception" pointcut-ref="pc" /> </aop:aspect> </aop:config> </beans>
5)测试
@Test public void fun() { ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("/applicationContext-aop.xml"); IUserService userService= classPathXmlApplicationContext.getBean("userService",IUserService.class); userService.add(); userService.delete(); }
注意:
befor通知:在目标方法调用前执行,目标方法会执行
around通知:在目标方法调用的时候执行,用来替代目标方法,如果在通知中没有放行目标方法,目标方法不会执行 ,如果目标方法有返回值 则around方法也应该有返回值
public void round(ProceedingJoinPoint point) throws Throwable {
//查询方法
System.out.println( “准备在目标方法中添加环绕通知”);
if(false)
point.proceed(); //放行目标方法
}
//环绕通知
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println(“查询所有用户信息”);
return point.proceed();// 等价于userDao.select()
}
after:无论目标方式是成功返回还是出异常 都执行
afterReturning: 目标方法成功返回后执行
afterExeption :目标方法出异常后执行
标签:两种,方式,spring,void,springframework,println,SpringAOP,org,public From: https://www.cnblogs.com/daimenglaoshi/p/16756732.html