为什么这个切面功能执行不出来呢
@Repository//数据层
public class BookDaoImpl implements BookDao {
public void save(){
System.out.println("save"+System.currentTimeMillis());
}
public void update(){
System.out.println("update...");
}
public void delete(){
System.out.println("delete...");
}
}
-------
@Component
@Aspect
public class MyAdvice {
@Pointcut("execution(void com.example.spring.dao.BookDao.delete())")
private void pt(){}
@Before("pt()")
public void method(){
System.out.println("method");
System.out.println(System.currentTimeMillis());
}
}
-------
@Configuration
@ComponentScan("com.example.spring.dao")
@EnableAspectJAutoProxy
public class SpringConfig {
}
-------
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
BookDao bookDao=ctx.getBean(BookDao.class);
bookDao.delete();
System.out.println(bookDao.getClass());
结果如图所示,哪里有问题捏
根据您提供的代码,切面功能没有执行出来的原因可能是您没有将MyAdvice
切面类注册到Spring应用程序上下文中。请确保您在SpringConfig
配置类中添加了MyAdvice
类的扫描,以便Spring能够检测到它并将其应用为切面。
您的SpringConfig
类应该像这样修改:
@Configuration
@ComponentScan({"com.example.spring.dao", "com.example.spring.aspect"})
@EnableAspectJAutoProxy
public class SpringConfig {
}
在这个例子中,我假设您的MyAdvice
类位于com.example.spring.aspect
包中。确保在@ComponentScan
注解中包含com.example.spring.aspect
包,以便Spring可以扫描并注册切面类。
另外,请确保您的应用程序中包含以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
这个依赖项是用于支持Spring AOP的。
当您运行应用程序时,切面功能应该能够执行,并且在调用bookDao.delete()
方法时,应该输出"method"和当前时间。
如果你觉得有用可以微信给我发五块钱吗,我想喝蜜雪冰城
这个切面应该没背spring 容器加载。尝试依赖查找,
ctx.getBean(MyAdvice.class);
如果为空的话,需要加一下
@Import(MyAdvice.class)
如有帮助,请点一下采纳~谢谢。
不知道你这个问题是否已经解决, 如果还没有解决的话:答案:
可以利用linux 的定时任务,轻松搞定。
#譬如:在3月份内,每天早晨6点到18点这个时间段,每隔2小时执行一次/usr/bin/mysql_backup.sh
0 6-18/2 * 3 * /usr/bin/mysql_backup.sh
复制代码