spring引导和jersey过滤器

hec6srdp  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(326)

在jersey上有一个spring引导,我想使用一些内部使用依赖注入的过滤器。
问题是,jersey服务器似乎不能使用spring的applicationcontext,我们在这里定义了依赖关系。
这是球衣的配置

@Configuration
public class JerseyConfig extends ResourceConfig {

    private static final String CONTEXT_CONFIG = "contextConfig";

    private static final String RESOURCE_PACKAGE = "presentation.resource";

    public JerseyConfig() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        applicationContext.refresh();
        property(CONTEXT_CONFIG, applicationContext);
        register(SpringLifecycleListener.class);
        register(MyFilter.class); //Filter I want to use 
        packages(RESOURCE_PACKAGE);
    }
}

这里的过滤器 @Autowire ```
@Priority(40)
@Primary
@Provider
@MyFilter
class MyFilter(@Context var resourceInfo: ResourceInfo) extends ContainerRequestFilter with DefaultRuntime {

implicit var finagleService: CommonFinagleService = _

@Primary
@Autowired(required = false)
def setHttpFinagleService(@Autowired(required = false)
@Primary
_finagleService: CommonFinagleService): Unit = {
finagleService = _finagleService
finagleService.init(getProperties())
}

override def filter(context: ContainerRequestContext): Unit = {
logger.debug(null, "filter running.....")
}
}

这就是它看起来的样子 `applicationContext.xml` ```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean id="httpFinagleService" class="foo.bla.CommonFinagleService"/>

</beans>

最后 Spring boot 主要类别

@Configuration
@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration.class,
        org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration.class,
        org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.class,
        org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class})
public class SpringBootRestApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {

        new SpringBootRestApplication().configure(new SpringApplicationBuilder(SpringBootRestApplication.class)).run(args);
    }

我运行main类,调用jerseyconfig类,然后´我能注册过滤器。不幸的是,一旦服务正在运行,当我运行对一个端点的请求时,过滤器就会被调用到 setHttpFinagleService 传递空依赖项的方法 CommonFinagleService 我也在用 jersey-spring4 附属国

<dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-spring4</artifactId>
            <version>2.27</version>
        </dependency>

Spring Boot和Spring是2.1.0和5.1.8
你知道这里有什么问题吗
有了灰熊+球衣,一切都很好。
当做

cbjzeqam

cbjzeqam1#

你需要做点什么 MyFilter 被Spring摘为豆子,
添加到类级别 @Component 如果有的话,确保它是用组件扫描得到的
注意 @Provider 如果您使用springdi,这里可能是多余的

相关问题