NoSuchBeanDefinitionException:没有名为“metricFilter”的bean可用

时间:2018-04-23 23:21:27

标签: java spring spring-boot

我使用带有[spring-boot-legacy 1.1.0][1]的spring boot 1.4.2以部署到weblogic 10.3(java 6 / servlet 2.5).....当我评论度量过滤器时,每个thins都在tomcat 7上工作正常part ...当我部署到weblogic时我只遇到问题....所以我回到tomcat以运行它...我取消注释度量过滤器,当我尝试部署我的应用程序时它给出:

SEVERE: Exception starting filter metricFilter
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'metricFilter' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:680)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1183)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1087)
    at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)
    at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:235)
    at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:199)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:285)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:266)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4981)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5683)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1320)
    at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:683)
    at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:209)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:136)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:607)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2555)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2544)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:662)

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.my.Application</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>metricFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>metricFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

        <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextAttribute</param-name>
            <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>messageDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextAttribute</param-name>
            <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>messageDispatcherServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

    <display-name>IPOMS Online Channel</display-name>

    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

    <!-- The Usual Welcome File List -->
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

我有这是我的build.gradle:

compile('org.springframework.boot:spring-boot-legacy:1.1.0.RELEASE')

这是MetricFilterAutoConfiguration项目中spring-boot-legacy的代码,其中定义了度量过滤器bean:

/*
 * Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.legacy.context.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.boot.actuate.metrics.GaugeService;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.util.StopWatch;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.UrlPathHelper;

/**
 * {@link EnableAutoConfiguration Auto-configuration} that records Servlet interactions
 * with a {@link CounterService} and {@link GaugeService}.
 * 
 * @author Dave Syer
 * @author Phillip Webb
 */
@Configuration
@ConditionalOnBean({ CounterService.class, GaugeService.class })
@ConditionalOnClass({ Servlet.class, MetricRepositoryAutoConfiguration.class })
@ConditionalOnMissingClass("javax.servlet.ServletRegistration")
@AutoConfigureAfter({
        MetricRepositoryAutoConfiguration.class,
        org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration.class })
public class MetricFilterAutoConfiguration {

    private static final int UNDEFINED_HTTP_STATUS = 999;

    private static final String UNKNOWN_PATH_SUFFIX = "/unmapped";

    @Autowired
    private CounterService counterService;

    @Autowired
    private GaugeService gaugeService;

    @Bean
    public Filter metricFilter() {
        return new MetricsFilter();
    }

    /**
     * Filter that counts requests and measures processing times.
     */
    @Order(Ordered.HIGHEST_PRECEDENCE)
    private final class MetricsFilter extends OncePerRequestFilter {

        @Override
        protected void doFilterInternal(HttpServletRequest request,
                HttpServletResponse response, FilterChain chain) throws ServletException,
                IOException {
            UrlPathHelper helper = new UrlPathHelper();
            String suffix = helper.getPathWithinApplication(request);
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            MetricsFilterResponseWrapper wrapper = new MetricsFilterResponseWrapper(
                    response);
            try {
                chain.doFilter(request, wrapper);
            }
            finally {
                stopWatch.stop();
                int status = getStatus(wrapper);
                Object bestMatchingPattern = request
                        .getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
                if (bestMatchingPattern != null) {
                    suffix = bestMatchingPattern.toString().replaceAll("[{}]", "-");
                }
                else if (HttpStatus.valueOf(status).is4xxClientError()) {
                    suffix = UNKNOWN_PATH_SUFFIX;
                }
                String gaugeKey = getKey("response" + suffix);
                MetricFilterAutoConfiguration.this.gaugeService.submit(gaugeKey,
                        stopWatch.getTotalTimeMillis());
                String counterKey = getKey("status." + getStatus(wrapper) + suffix);
                MetricFilterAutoConfiguration.this.counterService.increment(counterKey);
            }
        }

        private int getStatus(MetricsFilterResponseWrapper response) {
            try {
                return response.getStatus();
            }
            catch (Exception ex) {
                return UNDEFINED_HTTP_STATUS;
            }
        }

        private String getKey(String string) {
            // graphite compatible metric names
            String value = string.replace("/", ".");
            value = value.replace("..", ".");
            if (value.endsWith(".")) {
                value = value + "root";
            }
            if (value.startsWith("_")) {
                value = value.substring(1);
            }
            return value;
        }
    }

    private class MetricsFilterResponseWrapper extends HttpServletResponseWrapper {

        private int status;

        public MetricsFilterResponseWrapper(HttpServletResponse response) {
            super(response);
        }

        public int getStatus() {
            return status;
        }

        @Override
        public void setStatus(int sc) {
            setStatus(sc, null);
        }

        @Override
        public void setStatus(int status, String sm) {
            this.status = status;
            super.setStatus(status, sm);
        }
    }

}

我还需要什么来避免此错误

2 个答案:

答案 0 :(得分:0)

最有可能我发现了问题... tomcat 7实际上是servlet 3.0容器而不是servlet 2.5 ...因此@ConditionalOnMissingClass("javax.servlet.ServletRegistration")不满意,因为这个类在servlet 3.0中...所以bean没有创建...我正要检查tomcat 6 ...但没有时间

答案 1 :(得分:0)

根据我对spring documents的理解,“MetricFilterAutoConfiguration”类不包含在配置中,因为至少有一个条件注释不符合。