栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

结合Spring项目和Jersey

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

结合Spring项目和Jersey

你无需在需要的ApplicationContext位置创建服务。你应该能够配置一个全局的。为此,Jersey提供了一个模块,该模块集成了两个框架。这使你可以将

@Autowired
所有Spring服务简单地放入Jersey资源类中。

注意:

${spring3.version}
示例中的版本是3.2.3.RELEASE。可以在示例中使用Spring 4,但是你需要确保从jersey-spring3依赖项中排除所有Spring传递依赖项。

[ 1 ]-关于Java配置示例的一件事要注意的是它使用一个独立的应用程序。要在Web应用程序中使用Java配置,需要一些技巧。这是一个已知的错误,Jersey会在该错误中查找contextConfigLocation具有applicationContext.xml文件位置的参数,并且在找不到该参数时会引发异常。

我发现了一些解决方法。

  1. 提出问题的人提到了一个这样的例子。你可以创建一个Spring Web初始化程序,在其中配置Spring上下文并覆盖param属性。
@Order(1)public class SpringWebContainerInitializer implements WebApplicationInitializer {    @Override    public void onStartup(ServletContext servletContext) throws ServletException {        registerContextLoaderListener(servletContext);        // Set the Jersey used property to it won't load a ContextLoaderListener        servletContext.setInitParameter("contextConfigLocation", "");    }    private void registerContextLoaderListener(ServletContext servletContext) {        WebApplicationContext webContext;        webContext = createWebAplicationContext(SpringAnnotationConfig.class);        servletContext.addListener(new ContextLoaderListener(webContext));    }    public WebApplicationContext createWebAplicationContext(Class... configClasses) {        AnnotationConfigWebApplicationContext context;        context = new AnnotationConfigWebApplicationContext();        context.register(configClasses);        return context;    }}
  1. 你可以简单地将其添加
    applicationContext.xml
    到类路径中,然后将spring Java配置类注册为bean。
<beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context.xsd">    <context:annotation-config/>    <bean id="config" /></beans>
  1. 我可以想到另一种方法,但是我已经保存了一段时间,可以进行实际测试了。

更新
“无法读取候选组件类… ASM ClassReader无法解析类文件-可能是由于尚不支持新的Java类文件版本”

似乎与此有关,将Spring 3与Java 8一起使用。就像我说的那样,如果要使用Spring 4,则需要从中排除Spring传递依赖项,

jersey-spring3
而只更改显式声明的Spring依赖项的版本。这是我测试并工作的示例。

<dependency>    <groupId>org.glassfish.jersey.ext</groupId>    <artifactId>jersey-spring3</artifactId>    <exclusions>        <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId>         </exclusion>        <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId>         </exclusion>        <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId>         </exclusion>        <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId>         </exclusion>    </exclusions></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-web</artifactId>    <version>4.1.0.RELEASE</version>    <scope>compile</scope></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-aop</artifactId>    <version>4.1.0.RELEASE</version>    <scope>compile</scope></dependency><dependency>    <groupId>org.aspectj</groupId>    <artifactId>aspectjrt</artifactId>    <version>1.8.1</version></dependency><dependency>    <groupId>org.aspectj</groupId>    <artifactId>aspectjweaver</artifactId>    <version>1.8.1</version></dependency>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/400485.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号