加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 嵌入式Jetty 8.x / Spring MVC / WebApplicationInitial

发布时间:2020-12-15 02:35:38 所属栏目:Java 来源:网络整理
导读:有没有人有以下的工作样本: 嵌入式Jetty 8.x应用程序 使用Spring MVC 零XML配置(即在Servlet端使用Spring WebApplicationInitializer,在Spring端使用annotations / java配置) 我已经尝试了所有可能的组合,但我不能让它工作.我发现的大多数嵌入式jetty示例都
有没有人有以下的工作样本:

>嵌入式Jetty 8.x应用程序
>使用Spring MVC
>零XML配置(即在Servlet端使用Spring WebApplicationInitializer,在Spring端使用annotations / java配置)

我已经尝试了所有可能的组合,但我不能让它工作.我发现的大多数嵌入式jetty示例都基于7.x或仍然使用XML配置文件.我现在获得的最佳设置是创建WebAppContext并将配置设置为AnnotationConfiguration.这在控制台上显示实际发生了某些事情,但它无法找到我的WebApplicationInitializer类,而它肯定在类路径上.这是基于Jetty 8.1.4和Spring 3.1.2.

出于测试目的,WebApplicationInitializer类没有做太多工作,它只在onStartup方法中打印一些内容来检查是否正在加载它.

谢谢!

解决方法

你看到这个问题: Spring 3.1 WebApplicationInitializer & Embedded Jetty 8 AnnotationConfiguration?

我不能分享我的代码,但这里有一些代码可以帮助你:

在web.xml

<!-- Java-based Spring container definition -->
<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<!-- Location of Java @Configuration classes that configure the components that makeup this application -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.workable.config</param-value>
</context-param>

清空application-config.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
</beans>

Spring WebMVCConfig:

/**
 * Spring MVC Configuration.
 *
 */
@Configuration
@EnableWebMvc
@EnableAsync
@EnableScheduling
public class WebMvcConfig extends WebMvcConfigurerAdapter {
}

/**
 * Main configuration class for the application.
 * Turns on @Component scanning,loads externalized application.properties.
 */
@Configuration
@ComponentScan(basePackages = {
    "com.workable"
},excludeFilters = {@Filter(Configuration.class)})
public class MainConfig {
    ...
}

Lib版本:

<spring.version>3.1.2.RELEASE</spring.version>
<jetty.version>8.1.5.v20120716</jetty.version>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读