3

有没有人有以下工作样本:

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

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

出于测试目的,WebApplicationInitializer 类并没有做太多的事情,它只在 onStartup 方法中打印一些内容以检查是否正在加载。

谢谢!

4

2 回答 2

1

你看到这个问题了吗: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>

春天的 WebMVC 配置:

/**
 * 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 {
    ...
}

库版本:

<spring.version>3.1.2.RELEASE</spring.version>
<jetty.version>8.1.5.v20120716</jetty.version>
于 2013-01-23T16:25:31.947 回答
0

这是在码头 9 上对我有用的东西。

    Server server = new Server(8080);

    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setResourceBase("src/main/webapp");
    webAppContext.setContextPath("/");
    webAppContext.setConfigurations(new Configuration[] {
            new WebXmlConfiguration(),
            new AnnotationConfiguration() {
                @Override
                public void preConfigure(WebAppContext context) {
                    ClassInheritanceMap map = new ClassInheritanceMap();
                    map.put(WebApplicationInitializer.class.getName(), 
                            new ConcurrentHashSet<String>() {{add(WebApplication.class.getName());}});
                    context.setAttribute(CLASS_INHERITANCE_MAP, map);
                    _classInheritanceHandler = new ClassInheritanceHandler(map);
                }
            }
        });
    server.setHandler(webAppContext);
    server.start();

对于 8 号码头,这可能有效:

webAppContext.setConfigurations(new Configuration[] {
    new WebXmlConfiguration(),
    new AnnotationConfiguration() {
        @Override
        public void preConfigure(WebAppContext context) throws Exception {
            MultiMap<String> map = new MultiMap<String>();
            map.add(WebApplicationInitializer.class.getName(), MyWebApplicationInitializerImpl.class.getName());
            context.setAttribute(CLASS_INHERITANCE_MAP, map);
            _classInheritanceHandler = new ClassInheritanceHandler(map);
        }
    }
});

MultiMap 是 org.eclipse.jetty.util 包的一种形式

于 2016-03-08T19:50:25.790 回答