Spring AOP入门
1.导入坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
<!-- <scope>compile</scope>-->
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
2.设置spring配置类
@Configuration
@ComponentScan("com_spring_aop")
//使用注解开发aop
@EnableAspectJAutoProxy
public class springConfig {
}
3.userDao接口和userDaoImp接口实现类
public interface userDao {
abstract public void add();
}
@Repository
public class userDaoImp implements userDao{
public void add(){
System.out.println("add......");
}
}
4.编写aop通知类
设置空的方法作为切入点
//声明作为注解的spring控件
@Component
//声明作为aop
@Aspect
public class aop_Class_01 {
// 切入点
@Pointcut("execution(void com_spring_aop.dao.userDao.add())")
private void fn(){}
// 前置通知
@Before("fn()")
public void method(){
System.out.println(System.currentTimeMillis());
}
// 环绕通知
@Around("fn()")
public void around(ProceedingJoinPoint joinPoint)throws Throwable{
System.out.println("执行前......");
// 固定写法,表示原方法执行
joinPoint.proceed();
System.out.println("执行后......");
}
}
5.测试类
ApplicationContext context = new AnnotationConfigApplicationContext(springConfig.class);
userDao userDao = context.getBean(userDao.class);
userDao.add();
标签:userDao,入门,spring,Spring,void,aop,class,AOP,public
From: https://www.cnblogs.com/cunshan-mf/p/17134375.html