当前位置: 首页 > 面试题库 >

Spring Java Config与Jboss 7

殷学
2023-03-14
问题内容

我正在尝试在jboss上使用基于spring
java的配置运行一个简单的应用程序,但没有成功。此应用程序在码头和tomcat上都可以正常工作。jboss日志看起来不错,因为它向我展示了一些成功的映射等,但是我尝试了404来访问URL。

这是我的代码:

初始化器

@Order(1)
public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer  {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] {RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] {WebAppConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setInitParameter("dispatchOptionsRequest", "true");
    }
}

根目录

@Configuration
@ComponentScan(value = "com.test.config", excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, value = RootConfig.class))
public class RootConfig {

}

WebAppConfig

@Configuration
@ComponentScan("com.test")
@EnableWebMvc
@EnableSpringDataWebSupport
public class WebAppConfig extends WebMvcConfigurerAdapter {

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

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

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

现在,jboss日志…

17:08:53,645 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/test]] (MSC service thread 1-8) Spring WebApplicationInitializers detected on classpath: [br.com.cleartech.config.Initializer@2f7e4dd2]
17:08:53,767 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/test]] (MSC service thread 1-8) Initializing Spring root WebApplicationContext
17:08:53,768 INFO  [org.springframework.web.context.ContextLoader] (MSC service thread 1-8) Root WebApplicationContext: initialization started
17:08:53,770 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Refreshing Root WebApplicationContext: startup date [Wed Dec 18 17:08:53 BRST 2013]; root of context hierarchy
17:08:53,843 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:53,846 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:53,847 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Registering annotated classes: [class br.com.cleartech.config.RootConfig]
17:08:53,915 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:53,916 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:53,977 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:53,978 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,130 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-8) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
17:08:54,153 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-8) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@52477602: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,rootConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,log4j,webAppConfig,homeController,consoleAppender,fileAppender,registerSpringLogger,org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration,mvcValidator,simpleControllerHandlerAdapter,beanNameHandlerMapping,httpRequestHandlerAdapter,mvcContentNegotiationManager,requestMappingHandlerMapping,resourceHandlerMapping,requestMappingHandlerAdapter,mvcConversionService,viewControllerHandlerMapping,defaultServletHandlerMapping,handlerExceptionResolver,org.springframework.data.web.config.SpringDataWebConfiguration,pageableResolver,sortResolver,setupViewResolver]; root of factory hierarchy
17:08:54,269 INFO  [org.hibernate.validator.util.Version] (MSC service thread 1-8) Hibernate Validator 4.2.0.Final
17:08:54,388 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/teste],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String br.com.cleartech.controller.HomeController.teste()
17:08:54,404 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
17:08:54,631 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
17:08:54,634 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
17:08:54,668 INFO  [org.springframework.web.context.ContextLoader] (MSC service thread 1-8) Root WebApplicationContext: initialization completed in 899 ms
17:08:54,675 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/test]] (MSC service thread 1-8) Initializing Spring FrameworkServlet 'dispatcher'
17:08:54,676 INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-8) FrameworkServlet 'dispatcher': initialization started
17:08:54,679 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Dec 18 17:08:54 BRST 2013]; parent: Root WebApplicationContext
17:08:54,681 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:54,682 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,683 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Registering annotated classes: [class br.com.cleartech.config.WebAppConfig]
17:08:54,685 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:54,686 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,698 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:54,699 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,759 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-8) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
17:08:54,786 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-8) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@12f882f3: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,webAppConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,log4j,rootConfig,homeController,consoleAppender,fileAppender,registerSpringLogger,org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration,mvcValidator,simpleControllerHandlerAdapter,beanNameHandlerMapping,httpRequestHandlerAdapter,mvcContentNegotiationManager,requestMappingHandlerMapping,resourceHandlerMapping,requestMappingHandlerAdapter,mvcConversionService,viewControllerHandlerMapping,defaultServletHandlerMapping,handlerExceptionResolver,org.springframework.data.web.config.SpringDataWebConfiguration,pageableResolver,sortResolver,setupViewResolver]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@52477602
17:08:54,885 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/teste],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String br.com.cleartech.controller.HomeController.teste()
17:08:54,891 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
17:08:54,927 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
17:08:54,931 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
17:08:54,962 INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-8) FrameworkServlet 'dispatcher': initialization completed in 285 ms
17:08:54,977 INFO  [org.jboss.web] (MSC service thread 1-8) JBAS018210: Registering web context: /test
17:08:54,985 INFO  [org.jboss.as] (MSC service thread 1-5) JBAS015951: Admin console listening on http://127.0.0.1:9990
17:08:54,986 INFO  [org.jboss.as] (MSC service thread 1-5) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 8333ms - Started 377 of 455 services (77 services are passive or on-demand)
17:08:55,175 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "test.war"

如您所见,这似乎还可以,因为我得到了一些:

Spring WebApplicationInitializers detected on classpath
Initializing Spring root WebApplicationContext
Root WebApplicationContext: initialization started
Mapped URL path [/resources/**]
Mapped URL path [/**]
Registering web context: /test
JBAS018559: Deployed "test.war"

但是,当尝试访问localhost:8080 / test或什至在控制器中声明一个简单的其余部分时,我得到了404。

编辑 只是为了说明,在撰写本文时,我没有使用spring boot。大多数答案都将其作为解决方案


问题答案:

我在没有web.xml的情况下将Spring MVC项目部署到JBoss 7.1时遇到了类似的问题。

根据Spring Javadocs for WebApplicationInitializer的介绍,Tomcat的旧版本(<=
7.0.14)无法以编程方式映射到“ /”。较旧的JBoss AS 7版本也有相同的缺陷。

这是我问题的根源。我正在通过“ /”注册servlet,但是JBoss EAP
6.4不以编程方式支持此映射。它仅通过web.xml起作用。我仍然想使用编程配置,因此我将映射更改为“ / *”而不是“ /”,它解决了我的问题。

public class WebApplicationInitializerImpl implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        WebApplicationContext context = getContext();

        Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context));
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");
    }

    private WebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(AppConfig.class.getName());
        return context;
    }

}

注意:此配置与JSP视图不兼容。“ / *”将取代Servlet容器的JSP
Servlet。如果您仍然依赖JSP视图,我建议您使用web.xml来配置DispatcherServlet, 而不要
通过编程方式进行配置。web.xml配置可正确使用“ /”。

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
</servlet>

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


 类似资料:
  • 在C语言中,假设每个算法被赋予完全相同的一组进程,那么先到先得、最短作业优先和循环之间的周转时间是否相等?还是调度算法不同?

  • 问题内容: 为了为 HTML5 Doctype 定义字符集,我应该使用哪种表示法? 短: 长: 问题答案: 在HTML5中,它们是等效的。使用较短的一个,更容易记住和键入。浏览器支持很好,因为它是为向后兼容而设计的。

  • 连接的多个输入都相当于Yes的时候才会输出Yes。 用法 Your browser does not support the video tag. 案例:小闹钟 功能:今天15:10:00,响起猫叫声小闹钟 工作原理 当所有的输入都是Yes的时候,与节点才输出Yes。

  • 问题内容: 似乎有三种 相同的 方法可以独立于平台获取依赖于平台的“文件分隔符”: 我们如何决定何时使用哪个? 它们之间甚至有什么区别吗? 问题答案: 可以通过调用命令行参数或使用命令行参数覆盖 获取默认文件系统的分隔符。 获取默认文件系统。 获取文件系统的分隔符。请注意,作为一种实例方法,在需要代码在一个JVM中对多个文件系统进行操作的情况下,可以使用该方法将不同的文件系统传递给代码(而不是默认

  • 问题内容: 我今天刚刚与一些同事讨论了python的db-api fetchone vs fetchmany vs fetchall。 我确定每个应用程序的用例都取决于我正在使用的db-api的实现,但是总的来说,fetchone,fetchmany,fetchall的用例是什么? 换句话说,以下等效项是什么?还是其中之一比其他人更受青睐?如果是这样,在哪些情况下? 问题答案: 我认为这确实取决于

  • 问题内容: 即时创建元素并能够移动元素的最佳方法是什么?例如,假设我要创建一个矩形,圆形和多边形,然后选择这些对象并四处移动。 我了解HTML5提供了三个使之成为可能的元素:svg,canvas和div。对于我想做什么,这些元素中的哪一个将提供最佳性能? 为了比较这些方法,我正在考虑创建三个视觉上相同的网页,每个网页中都有页眉,页脚,小部件和文本内容。第一页中的小部件将完全使用元素创建,第二页中的

  • 问题内容: 之间有什么区别,和? 问题答案: 将等待,直到所有资产完成下载为止,例如图像和脚本。 DOM准备就绪,直到您可以通过API访问DOM为止。 作为一个方面说明,在当今这个时代,你应该使用或较旧的IE。