问题描述
我正在跟踪一个教程,在该教程中,我试图使用一个Spring AOP切入点表达式方法,该方法接受0个或多个任何类型的参数,但是却遇到了java.lang.NullPointerException.我正在使用Spring MVC和Maven以及Eclipse.如果使用以下命令,一切都会正常运行:
I'm following a tutorial where I'm trying to use a Spring AOP pointcut expression method that accepts 0 or more arguments of any type but I'm getting a java.lang.NullPointerException.I am using Spring MVC and maven along with eclipse.Everything is working fine if I use:
@Before("execution(* add*())")
或
@Before("execution(* add*(*))")
或
@Before("execution(* add*(boolean,..))")
但是当我单独使用".."的那一刻
but the moment I use ".." alone
@Before("execution(* add*(..))")
表示当我尝试使用以下URL访问我的视图时,我希望切入点表达式与带有0个或多个任何类型参数的方法匹配: http://localhost:8080/springaopdemo/main/test 我收到HTTP状态500-内部服务器错误并且在我的eclipse控制台日志中,出现以下错误:
to mean that I want that the pointcut expression matches a method with 0 or more arguments of any type, when I try to access to my view using the following URL: http://localhost:8080/springaopdemo/main/testI get HTTP Status 500 - Internal Server Errorand in my eclipse console log, I get the following error:
在此先感谢您的帮助在需要时直接链接到项目: https://github.com/odamak/springaopdemo/提交/cfbfef8ac80cd1c88b9a9359bbba964503cc24c5
Thank you in advance for your helpDirect link to project in case you need it: https://github.com/odamak/springaopdemo/commit/cfbfef8ac80cd1c88b9a9359bbba964503cc24c5
INFO: Initializing Spring DispatcherServlet 'dispatcher'
Jan 05, 2020 8:31:51 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Initializing Servlet 'dispatcher'
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final org.springframework.web.servlet.HandlerExecutionChain org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(javax.servlet.http.HttpServletRequest) throws java.lang.Exception] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final void org.springframework.web.context.support.WebApplicationObjectSupport.setServletContext(javax.servlet.ServletContext)] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final void org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(org.springframework.context.ApplicationContext) throws org.springframework.beans.BeansException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final void org.springframework.http.converter.AbstractHttpMessageConverter.write(java.lang.Object,org.springframework.http.MediaType,org.springframework.http.HttpOutputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotWritableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final java.lang.Object org.springframework.http.converter.AbstractHttpMessageConverter.read(java.lang.Class,org.springframework.http.HttpInputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotReadableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final void org.springframework.http.converter.AbstractHttpMessageConverter.write(java.lang.Object,org.springframework.http.MediaType,org.springframework.http.HttpOutputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotWritableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final java.lang.Object org.springframework.http.converter.AbstractHttpMessageConverter.read(java.lang.Class,org.springframework.http.HttpInputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotReadableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final void org.springframework.http.converter.AbstractHttpMessageConverter.write(java.lang.Object,org.springframework.http.MediaType,org.springframework.http.HttpOutputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotWritableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final java.lang.Object org.springframework.http.converter.AbstractHttpMessageConverter.read(java.lang.Class,org.springframework.http.HttpInputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotReadableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final void org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(java.lang.Object,java.lang.reflect.Type,org.springframework.http.MediaType,org.springframework.http.HttpOutputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotWritableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final void org.springframework.http.converter.AbstractHttpMessageConverter.write(java.lang.Object,org.springframework.http.MediaType,org.springframework.http.HttpOutputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotWritableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final java.lang.Object org.springframework.http.converter.AbstractHttpMessageConverter.read(java.lang.Class,org.springframework.http.HttpInputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotReadableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final void org.springframework.http.converter.AbstractHttpMessageConverter.write(java.lang.Object,org.springframework.http.MediaType,org.springframework.http.HttpOutputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotWritableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:54 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final java.lang.Object org.springframework.http.converter.AbstractHttpMessageConverter.read(java.lang.Class,org.springframework.http.HttpInputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotReadableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:55 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final void org.springframework.http.converter.AbstractHttpMessageConverter.write(java.lang.Object,org.springframework.http.MediaType,org.springframework.http.HttpOutputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotWritableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:55 AM org.springframework.aop.framework.CglibAopProxy doValidateClass
INFO: Unable to proxy interface-implementing method [public final java.lang.Object org.springframework.http.converter.AbstractHttpMessageConverter.read(java.lang.Class,org.springframework.http.HttpInputMessage) throws java.io.IOException,org.springframework.http.converter.HttpMessageNotReadableException] because it is marked as final: Consider using interface-based JDK proxies instead!
Jan 05, 2020 8:31:55 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Completed initialization in 4385 ms
Jan 05, 2020 8:31:55 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Jan 05, 2020 8:31:55 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Jan 05, 2020 8:31:55 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 11176 ms
Jan 05, 2020 8:32:08 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/springaopdemo] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:408)
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1234)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1016)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
以下是方面的定义:
package com.luv2code.aopdemo.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyDemoLoggingAspect {
@Before("execution(* add*(..))")
public void beforeAddAccountAdvice() {
System.out.println("\n======>>> Executing @Before advice on method");
}
}
这是其中一个类的定义,该类具有应在其上应用@Before建议的方法:
Here is a definition of one of the classes that has a method on which @Before advice is supposed to be applied:
package com.luv2code.aopdemo.dao;
import org.springframework.stereotype.Component;
@Component
public class MembershipDAO {
public void addAccount() {
System.out.println(getClass() + ": DOING MY DB WORK: ADDING A MEMBERSHIP ACCOUNT");
}
}
这是我的配置类:
package com.luv2code.aopdemo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.luv2code.aopdemo")
public class DemoConfig {
}
以下是使用建议所应用的方法的控制器:
Here is the Controller that makes use of the method on which the advice is applied:
package com.luv2code.aopdemo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.luv2code.aopdemo.dao.AccountDAO;
import com.luv2code.aopdemo.dao.MembershipDAO;
@Controller
@RequestMapping("/main")
public class MainController {
@GetMapping("/test")
public String MainTest() {
System.out.println("hello from /main/test");
// read spring config java class
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);
// get the bean from spring container
theMembershipDAO = context.getBean("membershipDAO", MembershipDAO.class);
// call the membership business method
theMembershipDAO.addAccount();
// close the context
context.close();
return "main-test";
}
}
这是我的观点:
<html>
<body>
<h2>Hello World from main-test!</h2>
</body>
</html>
这是我的web.xml文件:
Here is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-aop-demo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-aop-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是spring-aop-demo-servlet.xml配置:
Here is spring-aop-demo-servlet.xml config:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Add AspectJ autoproxy support for AOP -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- Add support for component scanning -->
<context:component-scan
base-package="com.luv2code.aopdemo" />
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven />
<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Add support for reading web resources: css, images, js, etc ... -->
<mvc:resources location="/resources/"
mapping="/resources/**"></mvc:resources>
</beans>
这是我的pom.xml文件:
Here is my pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.luv2code</groupId>
<artifactId>springaopdemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springaopdemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>5.2.0.RELEASE</org.springframework-version>
<org.aspectj-version>1.7.4</org.aspectj-version>
<org.slf4j-version>1.7.5</org.slf4j-version>
<hibernate-version>5.4.7.Final</hibernate-version>
<aspectj-version>1.9.5</aspectj-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-version}</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springaopdemo</finalName>
</build>
</project>
推荐答案
您的切入点过于全局.您正在尝试编织太多的类,尤其是Spring核心类.尝试通过within(com.luv2code.aopdemo..*)
将切入点匹配限制为您自己的应用程序包.有关其他示例切入点,请参见我的其他答案.
Your pointcut is too global. You are trying to weave into too many classes, specifically Spring core classes. Try to limit pointcut matching to your own application packages via within(com.luv2code.aopdemo..*)
. See my other answer for other sample pointcuts.
这篇关于具有0个或多个任何类型参数的Spring AOP切入点表达式方法将引发java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!