我有这个代码:
如何在 getAll 方法中强制使用 getService() 方法,这意味着如果我直接调用 service.findAll()
编译会失败?
是否可以创建一个注释来禁止直接使用该字段?
@Named
@Profile("default")
public class AlfrescoVersionServiceImpl {
@Inject
//Request Scope service
private AlfrescoService service;
@Resource
private WebServiceContext webServiceContext;
public List<Fichier> getAll(){
return getService().findAll();
}
public AlfrescoService getService(){
service.setEnPointAdress("--");
if(webServiceContext.isUserInRole(role)){
service.setRole("--");
}
}
}
预先感谢您的回复。
最佳答案
您可以将 service
字段移动到父类(super class)中,并将其声明为 private
以防止子类访问它,同时保留 getService()
protected
。
或者,您可以在 @Produces
方法中配置 AlfrescoService
,这样每个请求注入(inject) AlfrescoService
的人都会获得一个实例,该实例已经配置完毕。
关于java - 如何在同一个类中强制执行 getter 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54559787/