参考博客
连接点:所有方法。
切入点:哪些方法需要追加功能的,匹配通知的方法,叫切入点。
通知:各个方法共用的功能,叫通知。通知存在于通知类中。
切面:切面描述的是通知共用的功能与所对应切入点的关系。在哪些切入点上执行哪些通知叫切面。
接口UserDao.java
public interface UserDao {
public void save();
public void update();
}
实现类UserDaoImpl.java
@Component("userDao")
public class UserDaoImpl implements UserDao {
private String username;
public void setName(String name){
this.username=name;
}
public void save() {
System.out.println(System.currentTimeMillis());
System.out.println("user dao save ...");
}
public void update(){
System.out.println("user dao update...");
}
}
APP6.java程序
public class APP6 {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
UserDao userDao=(UserDao) ctx.getBean("userDao");
userDao.update();
}
}
userDao.save结果
userDao.update结果
这里调用userDao.update无法显示时间,接下来用AOP的方法进一步实现。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
userDao接口和实现类在前面给了具体书写方法
public class MyAdvice {
public void method(){
System.out.println(System.currentTimeMillis());
}
}
public class MyAdvice {
@Pointcut("execution(void com.itheima.dao.UserDao.update())")
private void pt(){}
public void method(){
System.out.println(System.currentTimeMillis());
}
}
public class MyAdvice {
@Pointcut("execution(void com.itheima.dao.UserDao.update())")
private void pt(){}
@Before("pt()")
public void method(){
System.out.println(System.currentTimeMillis());
}
}
@Component
@Aspect
public class MyAdvice {
@Pointcut("execution(void com.itheima.dao.UserDao.update())")
private void pt(){}
@Before("pt()")
public void method(){
System.out.println(System.currentTimeMillis());
}
}
@Configuration
@Import({jdbcConfig.class})
@ComponentScan("com.itheima")
@EnableAspectJAutoProxy
public class SpringConfig {
// 1.定义方法获得要管理的对象
// 2.添加@Bean,表示当前返回值是一个Bean
}
APP6.java
public class APP6 {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
UserDao userDao=(UserDao) ctx.getBean("userDao");
userDao.update();
}
}
okk!
UserDaoImpl.java
@Component("userDao")
public class UserDaoImpl implements UserDao {
private String username;
public void setName(String name){
this.username=name;
}
public void save() {
System.out.println(System.currentTimeMillis());
System.out.println("user dao save ...");
}
public void update(){
System.out.println("user dao update...");
}
public int select(){
System.out.println("book dao select is running...");
return 666;
}
}
MyAdvice.java
@Component
@Aspect
public class MyAdvice {
@Pointcut("execution(int com.itheima.dao.UserDao.select())")
private void pt(){}
@Around("pt()")
public Object method(ProceedingJoinPoint pjp) throws Throwable {
// System.out.println(System.currentTimeMillis());
System.out.println("hello1");
Object tmp=pjp.proceed();
System.out.println("hello2");
return tmp;
}
}
//注解中的returing和形参需要保持一致
@AfterReturing(value = "pt()",returing="ret")
public void afterReturning(String ret){
}