使用spring的aop实现权限拦截后出现依赖注入为空的问题,求高手解决,进来看问题详情
使用的是注解实现的,贴代码,
切面如下:
@Component @Aspect
public class VoucherIntercept {
//声明一个切入点
@Pointcut( "execution(* com.jboa.action.VoucherAction.examiaeVoucher(..)) ")
public void examiae(){}
//实现拦截的方法
@Around( "examiae() ")
public Object examiaeAround(ProceedingJoinPoint pjp) throws Throwable {
Employee employee = (Employee)ActionContext.getContext().getSession().get( "employee ");
String postion = employee.getDictionary().getPosition().getDicDes();
String dept = employee.getDictionary().getDepartment().getName();
Object result = null;
//判断当前的操作用户是不是经理或者是财务人员
if(postion.contains( "经理 ")||dept.contains( "财务 ")||
postion.contains( "总经理 ")) {
result = pjp.proceed();
}else {
ActionContext.getContext().put( "msg ", "您没有这个操作权限! ");
result = "error ";
}
return result;
}
}
Action如下:
public class VoucherAction {
@Resource private IVoucherService voucherService;
@Resource private IItemService itemService;
@Resource private IExamiaeService examiaeService;
public String seeVoucher() {
Employee employee = (Employee)ActionContext.getContext().getSession().get( "employee ");
List <Voucher> vouchers = voucherService.findVoucherByEmployee(employee.getEmId());
ActionContext.getContext().put( "empVouchers ", vouchers);
return "seeEmpVoucher ";
}
public String examiaeVoucher() {
Employee employee = (Employee)ActionContext.getContext().getSession().get( "employee ");
//获取当前用户的职位和部门
String postion = employee.getDictionary().getPosition().getDicDes();
List <Voucher> vouchers = null;
//根据具体的权限执行查询
if(postion.contains( "经理 ")&&!postion.contains( "总经理 ")) {
//查询经理部门的报销单
vouchers = voucherService.findVoucherByDept
(employee);
}else if(postion.contains( "财务 ")&&!postion.contains( "财务经理 ")) {
vouchers = voucherService.findVoucherByFinance(employee);
}else if(postion.contains( "总经理 ")){
vouchers = voucherService.findVoucherByHead();
}
ActionContext.getContext().put( "empVouchers ", vouchers);
return "examiaeVoucher ";
}
}
具体业务大家不用详细了解,请问的问题就是,当我在使用上面的aop之后是实现的权限的拦截(就是在执行action里面的examiaeVoucher方法时会被拦截),但是出现的问题是我用Resource注解依赖注入的对象一个也没有注入进来,全部是null,这是为什么?
贴出错误:
java.lang.NullPointerException com.jboa.action.VoucherAction.openAddVoucher(VoucherAction.java:70)
com.jboa.action.VoucherAction$$FastClassByCGLIB$$686e0ce3.invoke( <generated> )
net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
com.jboa.action.VoucherAction$$EnhancerByCGLIB$$d039573e.openAddVoucher( <generated> )
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
里面报的是在执行一个方法的时候调用方法(报错的方法没有贴)的对象(就是注入的对象)为空,这个错误是关于aop的,但是具体怎么引起的,可有大虾告知。
由于使用的注解,配.就不用贴了吧。
坐等大虾。
------解决方案--------------------@Pointcut( "execution(* com.jboa.action.VoucherAction.examiaeVoucher(..)) ")
除了examiaeVoucher方法,VoucherAction的其他方法是不会被拦截的
建议aop点为VoucherAction,不要切入到VoucherAction的方法中
------解决方案--------------------
我和你遇到了同样的错误了,能了好长时间才发现,
<constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true" />
在你的struts2文件里放入这个常量,这样就可以了,或者为你的service提供setter方法,大概意思是不论怎样,strutsw都要去考虑spring的自动装配