@DeclareParents声明对象的AOP
2024-09-18 23:02:34
今天在使用@DeclareParents时,使用AspectJ拓展对象的接口
public interface Encoreable {
void nextPerformance();
} @Component
public class DefaultEncoreablePerform implements Encoreable { @Override
public void nextPerformance() {
System.out.println("这是一场返场表演");
}
} public interface Performance {
public void perform(int num);
}
@Component
public class MusicPerformance implements Performance { @Override
public void perform(int num) {
if (num > 0) {
System.out.println(1 / num);
} else {
System.out.println("Concurred Exception ");
throw new NumberFormatException();
} System.out.println("精彩的表演");
}
}
@Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class ConfigAop {
} @Aspect
@Component
public class AddAspectAudience { @DeclareParents(value = "Performance+", defaultImpl = DefaultEncoreablePerform.class)
public static Encoreable encoreable;
} @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = com.aop.ConfigAop.class)
public class AopAspectjTest { @Autowired
private Performance performance; @Test
public void testAop() {
performance.perform(1);
Encoreable encoreable = (Encoreable) (performance);
encoreable.nextPerformance(); } }
测试时一直提示对象不能强转,找半天找不到原因不知道为什么,暂时记一下
最新文章
- BZOJ 1087 【SCOI2005】 互不侵犯King
- php 下载文件的函数
- NK3C 业务权限控制
- Web前端小白入门指迷
- Android 开发环境下载地址 -- 百度网盘 adt-bundle android-studio sdk adt 下载
- UILabel详解
- webkit和xcode
- Win7_关闭休眠文件hiberfil.sys
- CSS3:不可思议的border属性&;Web字体图标Font Awesome
- 为php安装memcached扩展连接memcached服务器
- ajaxfileupload
- MessageBox, MessageBoxBurttons, MessageBoxIcon 详细解析
- 关于LINUX各类系统资源整合
- 基于Spring Cloud、JWT 的微服务权限系统设计
- vue结合axios使用入门
- mysql插入中文报错的问题
- Jexus使用的相关记录
- JSON数据写入和解析
- 面试加分项---HashMap底层实现原理
- 第39章 ETH—Lwip以太网通信