springboot部署启动时报错,java.lang.InstantiationException解决方法

java.lang.InstantiationException:

是由于在实体类中未添加无参构造函数引起的

我的问题是部署时,总报java.lang.InstantiationException

 java.lang.InstantiationException: com.syfri.baseapi.filter.AccessFilter
    at java.lang.Class.newInstance(Class.java:427)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:119)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:258)
    at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:105)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4700)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5340)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:729)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1816)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: com.syfri.baseapi.filter.AccessFilter.()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 16 more

程序代码如下:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import javax.servlet.FilterChain;
@Component
@ServletComponentScan
@WebFilter(filterName = "accessFilter",urlPatterns = "/*")
@Order(value = 1)

public class AccessFilter implements Filter{

   private static final Logger logger = LoggerFactory.getLogger(AccessFilter.class);

   protected Environment environment;

   public AccessFilter(Environment environment){
      this.environment = environment;
   }

   public void init(FilterConfig filterConfig) throws ServletException {
      logger.info("--- AccessFilter Init ---");
   }

   public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
      filterChain.doFilter(servletRequest, servletResponse);
   }
   public void destroy() {
      logger.info("--- AccessFilter Destroy ---");
   }
}

报错总是找不到init方法,分析之后是由于在实体类中未添加无参构造函数引起的。
解决方法:
    在该类加入构造函数就可以啦
(ps:解决问题还是需要看log日志里报的错误信息) 
public AccessFilter(){};

  
  

你可能感兴趣的:(spring,springboot)