[英]Do something in beginning of methods with injected spring beans and custom annotation
I want to check user permissions in a service-class before each methods execution. 我想在执行每个方法之前检查服务类中的用户权限。 this is my current approach: 这是我目前的方法:
appContext.xml: appContext.xml:
<bean id="requestSeviceImpl" class="services.impl.RequestSeviceImpl">
<property name="permissionService" ref="permissionServiceImpl" />
...
</bean>
implementation: 实施:
public class RequestServiceImpl implements RequestService
{
private PermissionService permissionService;
...
public void setPermissionService(PermissionService permissionService)
{
this.permissionService = permissionService;
}
@Override
@Transactional
public RequestResultModel submitRequest(Request request)
{
if(permissionService.isCurrentUserAuthorized(request,"submit"))
{
//submit request
}
//throw a permission exception
}
@Override
@Transactional
public void sendRequest(Request request)
{
if(permissionService.isCurrentUserAuthorized(request,"send"))
{
//send request
}
//throw a permission exception
}
...
}
All i want is handling permission checking in my custom annotation (@Secure); 我只需要在自定义批注(@Secure)中处理权限检查; It should check permission with permissionService and throws permission exception when its necessary. 它应该使用permissionService检查许可,并在必要时抛出许可异常。 And i don't want re-instantiate requestServiceImpl or permissionServiceImpl classes (it should use spring beans): 而且我不想重新实例化requestServiceImpl或permissionServiceImpl类(它应该使用spring bean):
@Override
@Transactional
@Secure(...)
public RequestResultModel submitRequest(Request request)
{
//submit request
}
How can i do this? 我怎样才能做到这一点?
About the only way I can think of is via Aspect Oriented Programming (AOP) . 我能想到的唯一方法是通过面向方面的编程(AOP) 。 You could use either Spring AOP or AspectJ. 您可以使用Spring AOP或AspectJ。 You may want to look at the book AspectJ in Action . 您可能需要看《 AspectJ in Action 》一书。 The first chapter is available as a sample and will explain what you can do with AspectJ. 第一章仅作为示例,将说明您可以使用AspectJ做什么。 Subsequent chapters use example code that does exactly what you are trying to do, secure a method. 随后的章节将使用示例代码,这些代码完全可以实现您想要做的事情,并保护方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.