做网站需要什么书,iis php服务器搭建网站,网络营销平台的账号如何运营,视频网站 费用上一篇博客《java:aocache:基于aspectJ实现的方法缓存工具》介绍了aocache的基本使用#xff0c; 介绍AoCacheable注解时说过#xff0c;AoCacheable可以定义在构造方法上#xff0c;定义在构造方法#xff0c;该构建方法就成了单实例模式。
也就是说#xff0c;只要构建…上一篇博客《java:aocache:基于aspectJ实现的方法缓存工具》介绍了aocache的基本使用 介绍AoCacheable注解时说过AoCacheable可以定义在构造方法上定义在构造方法该构建方法就成了单实例模式。
也就是说只要构建方法参数相同new 返回的实例都是同一个示例如下 Testpublic void test7Constructor() {try {Date d new Date();TestUser user new TestUser(jerry,0,d);log(user:{},user);for(int i0;i5;i) {TestUser o new TestUser(jerry,0,d);log(u{}:{},i,o);/** 每次 new 返回的都是同一个实例 */assertTrue(o user);}} catch (Throwable e) {e.printStackTrace();fail(e.getMessage());}}private static class TestUser {String name;Integer age;Date birthdate;AoCacheableTestUser() {this(null, null, null);}AoCacheableprotected TestUser(String name, Integer age, Date birthdate) {this.name name;this.age age;this.birthdate birthdate;}}注意
AoCacheable定义在私有(private)构造方法上无效因为基于AspectJ的工作原理它不能拦截私有构造方法。对于是否将AoCacheable定义在构造方法上要认真考虑是否适合你的使用场景因为一旦定义了将AoCacheable注解定义在构造方法上该方法new操作就不能创建新实例。
所以AoCacheable定义在构造方法的使用方式是有限制的。因为就无法再创建新实例如果又希望保持构造方法创建新实例又能得到单实例缓存。建议不要在构造方法上定义AcCacheable注解注解而是定义一个有AcCacheable注解的静态方法用于获取单实例,示例如下 protected TestUser(String name, Integer age, Date birthdate) {this.name name;this.age age;this.birthdate birthdate;}AcCacheablepublic static TestUser getSingleton(String name, Integer age, Date birthdate){new TestUser(name,age,birthdate);}