【问题标题】:spring java configuration issue: Rejected bean name 'requestMappingHandlerMapping': no URL paths identifiedspring java配置问题:拒绝的bean名称'requestMappingHandlerMapping':没有识别出URL路径
【发布时间】:2012-06-08 19:03:07
【问题描述】:

我正在使用 Spring Java 配置来创建 Web 应用程序。

当我在 tomcat 上运行时,它说资源 /myapp/myapp 未找到

在控制台输出中,我注意到以下内容。我不确定这是否会导致任何问题。

我只显示那些看起来很奇怪的日志行。因为日志太大了

INFO: validateJarFile(C:\libraries\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myapp\WEB-INF\lib\javax.servlet-api-3.0.1.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class


Creating instance of bean 'beanNameHandlerMapping'
Returning cached instance of singleton bean 'delegatingWebMvcConfiguration'
Eagerly caching bean 'beanNameHandlerMapping' to allow for resolving potential circular     references
Looking for URL mappings in application context: Root WebApplicationContext: startup date [Mon J     un 04 13:28:33 EDT 2012]; root of context hierarchy
Rejected bean name  'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': no URL paths identified
Rejected bean name 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor': no URL paths identified
Rejected bean name 'org.springframework.context.annotation.internalRequiredAnnotationProcessor': no URL paths identified
Rejected bean name 'org.springframework.context.annotation.internalCommonAnnotationProcessor': no URL paths identified
Rejected bean name 'webAppContextConfig': no URL paths identified
Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0': no URL paths identified
Rejected bean name 'delegatingWebMvcConfiguration': no URL paths identified
Rejected bean name 'requestMappingHandlerMapping': no URL paths identified
Rejected bean name 'viewControllerHandlerMapping': no URL paths identified
Rejected bean name 'beanNameHandlerMapping': no URL paths identified
Rejected bean name 'resourceHandlerMapping': no URL paths identified
Rejected bean name 'defaultServletHandlerMapping': no URL paths identified
Rejected bean name 'requestMappingHandlerAdapter': no URL paths identified
Rejected bean name 'mvcConversionService': no URL paths identified
Rejected bean name 'mvcValidator': no URL paths identified
Rejected bean name 'httpRequestHandlerAdapter': no URL paths identified
Rejected bean name 'simpleControllerHandlerAdapter': no URL paths identified
Rejected bean name 'handlerExceptionResolver': no URL paths identified
Rejected bean name 'configureInternalResourceViewResolver': no URL paths identified
Rejected bean name 'environment': no URL paths identified
Rejected bean name 'systemProperties': no URL paths identified
Rejected bean name 'systemEnvironment': no URL paths identified
Rejected bean name 'servletContext': no URL paths identified
Rejected bean name 'contextParameters': no URL paths identified
Rejected bean name 'contextAttributes': no URL paths identified
Rejected bean name 'importRegistry': no URL paths identified
Rejected bean name 'messageSource': no URL paths identified
Rejected bean name 'applicationEventMulticaster': no URL paths identified
Finished creating instance of bean 'beanNameHandlerMapping'
Creating shared instance of singleton bean 'resourceHandlerMapping'
Creating instance of bean 'resourceHandlerMapping'
Returning cached instance of singleton bean 'delegatingWebMvcConfiguration'
Eagerly caching bean 'resourceHandlerMapping' to allow for resolving potential circular references
Mapped URL path [/css/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
Mapped URL path [/scripts/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
Mapped URL path [/images/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
Finished creating instance of bean 'resourceHandlerMapping'

这里是 WebAppContextConfig.java

package com.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example")
public class WebAppContextConfig extends WebMvcConfigurerAdapter{
//Resolve logical view names to .jsp resources in /WEB-INF/views directory

@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    return resolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/");
    registry.addResourceHandler("/css/**").addResourceLocations("/css/");
    registry.addResourceHandler("/images/**").addResourceLocations("/images/");
}
}

这里是 WebAppInitializer.java

 package com.example;

 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRegistration;

 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.web.WebApplicationInitializer;
 import org.springframework.web.context.ContextLoaderListener;
 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
 import org.springframework.web.servlet.DispatcherServlet;
 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;

 public class WebAppInitializer implements WebApplicationInitializer {

/* (non-Javadoc)
 * @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.ServletContext)
 */
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(WebAppContextConfig.class);
    servletContext.addListener(new ContextLoaderListener(rootContext));

      AnnotationConfigWebApplicationContext dispatcherContext = 
                new AnnotationConfigWebApplicationContext();
              dispatcherContext.register(WebAppContextConfig.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");


    DefaultServletHandlerConfigurer defaultConfig = new DefaultServletHandlerConfigurer(servletContext);
    defaultConfig.enable();

}

}

还有什么好地方可以找基于java的spring mvc配置吗?

谢谢

【问题讨论】:

  • 来自@ravi 的/myapp/myapp 请求在哪里,您是否明确调用它,是您的上下文名称myapp 和映射myapp 的控制器方法,请您确认一下
  • @Biju 我的项目名称是 myapp。在 webapp 文件夹中,我有 index.jsp 转发到 myapp.jsp,在控制器中我映射到 /myapp。

标签: spring jakarta-ee tomcat spring-mvc classloader


【解决方案1】:

这不是根本原因@ravi,但我确实看到了一些我想首先强调的问题:

一个。您似乎通过 ContextLoaderListener 和 Dispatcher servlet 两次加载 Spring Java Config 文件,仅通过 ContextLoaderListener 执行此操作,您可以通过这种方式定义 DispatcherServlet - 只加载一个空上下文(理想情况下有两个 java 配置 -一个用于核心应用程序,一个用于 Web 配置):

ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new GenericWebApplicationContext());
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");

b.您能否使用 log4j 启​​用调试模式并查看还记录了什么 - 如果信息不足,请在跟踪级别记录。

c。内部视图解析器路径看起来有点错误 - 您缺少正向“/”:

resolver.setPrefix("/WEB-INF/jsp/");

你能不能试试这些,看看你现在看到了什么。

【讨论】:

  • 当我阅读低于 7.0 的版本时,我更改了 tomcat 版本。`4 添加映射(“/”)有一个错误。我进行了您指出的更改以及更多更改。终于工作了。但我仍然看到这些东西拒绝了 bean 。我不明白为什么会这样
猜你喜欢
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
  • 1970-01-01
  • 2013-11-19
相关资源
最近更新 更多