Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于@Cached等注解name属性自定义问题 #317

Closed
nestingLee opened this issue Jun 15, 2019 · 2 comments
Closed

关于@Cached等注解name属性自定义问题 #317

nestingLee opened this issue Jun 15, 2019 · 2 comments

Comments

@nestingLee
Copy link

@cached等注解name属性 是否能够像 key属性一样使用SPEL表达式?
使用 spring data jpa 对Service层做了通用方法封装BaseService,目前key属性能根据SPSEL表达式从方法获取指定的泛型Entity类名,但name属性不行,导致所有BaseService子类都使用一个name(name = "BaseCache:getEntityById"),默认name使用的类名+方法名策略,类名指的是BaseService而不是子类UserServiceImpl,有没有办法把name属性根据不同的子类把BaseService中@cached的name属性区分开。

代码如下:
`
public class BaseService<E extends BaseEntity, PK extends Serializable>{

@Autowired
protected BaseRepository<E, PK> baseRepository;

@cached(name = "BaseCache:getEntityById", key = "targetObject.getEntityName():#id")
public E getEntityById(PK id) {
return baseRepository.findById(id).orElse(null);

}

@service
public class UserServiceImpl extends BaseService<User, Long>{

}

`

@areyouok
Copy link
Collaborator

现在不能的。

如果你只是想让cache的name不一样,可以把注解写到子类上面,然后指定不同的name。如果你非要写到父类上,可以定制自己的CacheNameGenerator。可以参考这里:#46

@areyouok
Copy link
Collaborator

没有进一步反馈我先关闭了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants