gpt4 book ai didi

java - 当我编辑 jp.getArgs() 并运行 jp.Proceed() 时,ProceedingJoinPoint AspectJ 方面使我的 Web 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 06:15:22 25 4
gpt4 key购买 nike

我正在尝试更改传递给 JdbcOperation 查询方法的参数。基本上,传递给 jdbcTemplate.query() 方法的第一个参数(字符串)的 SQL 语句需要更改。我想将任何语句包含在附加的 SQL SELECT 中。当在数据库后端运行或当我对其进行硬编码时,SELECT 工作正常。我认为在编辑 jp.getArgs()[0] 后调用 jp.Proceed() 后,query() 会正确自动填充参数。您可以从导致 java.lang.NullPointerException 的日志条目中看到这一点。我认为 NullPointerException 是从查询返回零行的结果(猜测)。就好像在数据库上执行查询时,方面中更改的参数没有通过。

我的 SqlLoggerAspect 方面如下所示:

@Around("execution(* org.springframework.jdbc.core.JdbcOperations.*(String, ..))")
public void log(final ProceedingJoinPoint jp) throws Throwable {

Object[] methodArgs = jp.getArgs(), sqlArgs = null;
CustomUser customUser = (CustomUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

methodArgs[0] = new String ("SELECT * FROM (" + methodArgs[0] +
") AS domain_object WHERE (ktn = "+ customUser.getKtn() +") OR (ktn is NULL)");
jp.proceed(methodArgs);
}

以下是我查询数据库的方法:

sySectionsList.addAll(jdbcTemplate.query("SELECT * FROM sy_section WHERE stn_code=?", sySectionMapper, sySection.getStn_code()));

我的 Spring Bean 配置如下所示:

<aop:aspectj-autoproxy proxy-target-class="true">
<aop:include name = "sqlLogger"/>
</aop:aspectj-autoproxy>

<aop:config proxy-target-class="true">
</aop:config>

<bean id="sqlLogger" class="ie.cit.pro.aspects.SqlLoggerAspect" />

这一切都会产生一个完美形成的 SQL 语句(在调用 jp.Proceed() 后我可以在 TRACE 日志中看到该语句),当我在数据库后端执行或在应用程序中对其进行硬编码时,该语句可以正常工作。但是我得到一个 NullPointerException,我认为这可能是因为在运行时没有记录返回到我的 RowMapper。我的错误如下:

DEBUG 2014-02-04 06:42:14,769 org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL query
DEBUG 2014-02-04 06:42:14,769 org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT * FROM (SELECT * FROM sy_section WHERE stn_code=?) AS domain_object WHERE (ktn = 1) OR (ktn is NULL)]
DEBUG 2014-02-04 06:42:14,769 org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
DEBUG 2014-02-04 06:42:14,769 org.springframework.jdbc.datasource.SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:dataSource;DB_CLOSE_DELAY=-1]
TRACE 2014-02-04 06:42:14,771 org.springframework.jdbc.core.StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [-WFBWLD101], value class [java.lang.String], SQL type unknown
DEBUG 2014-02-04 06:42:14,773 org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
DEBUG 2014-02-04 06:42:14,774 org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public java.lang.String ie.cit.pro.web.FbController.weldtrack(org.springframework.ui.Model,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,java.lang.String)]: java.lang.NullPointerException
DEBUG 2014-02-04 06:42:14,777 org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public java.lang.String ie.cit.pro.web.FbController.weldtrack(org.springframework.ui.Model,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,java.lang.String)]: java.lang.NullPointerException
DEBUG 2014-02-04 06:42:14,777 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public java.lang.String ie.cit.pro.web.FbController.weldtrack(org.springframework.ui.Model,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,java.lang.String)]: java.lang.NullPointerException
TRACE 2014-02-04 06:42:14,777 org.springframework.web.servlet.DispatcherServlet - Cleared thread-bound request context: SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.savedrequest.SavedRequestAwareWrapper@4e57dc21]
DEBUG 2014-02-04 06:42:14,778 org.springframework.web.servlet.DispatcherServlet - Could not complete request
java.lang.NullPointerException
at ie.cit.pro.domain.dao.JdbcDataRepository.getSySectionsByCode(JdbcDataRepository.java:127)
at ie.cit.pro.domain.dao.JdbcDataRepository$$FastClassByCGLIB$$1a306ee2.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:51)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:64)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at ie.cit.pro.domain.dao.JdbcDataRepository$$EnhancerByCGLIB$$c8a61982.getSySectionsByCode(<generated>)
at ie.cit.pro.services.SyServiceImpl.getSySectionsByCode(SyServiceImpl.java:45)
at ie.cit.pro.services.SyServiceImpl$$FastClassByCGLIB$$4c45712.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:627)
at ie.cit.pro.services.SyServiceImpl$$EnhancerByCGLIB$$9e7f5ebf.getSySectionsByCode(<generated>)
at ie.cit.pro.web.FbController.weldtrack(FbController.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:695)

最佳答案

你的方面是错误的。您将从 @Around 方法返回 void,从而使该方面涉及的每个方法都返回 null

Around 方面必须始终返回 Object,并且您必须始终返回对 proceed() 的调用结果。如果你不这样做,你就会遇到麻烦。

@Around("execution(* org.springframework.jdbc.core.JdbcOperations.*(String, ..))")
public Object log(final ProceedingJoinPoint jp) throws Throwable {

Object[] methodArgs = jp.getArgs(), sqlArgs = null;
CustomUser customUser = (CustomUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

methodArgs[0] = new String ("SELECT * FROM (" + methodArgs[0] +
") AS domain_object WHERE (ktn = "+ customUser.getKtn() +") OR (ktn is NULL)");
return jp.proceed(methodArgs);

}

关于java - 当我编辑 jp.getArgs() 并运行 jp.Proceed() 时,ProceedingJoinPoint AspectJ 方面使我的 Web 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21546128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com