[英]Spring boot: Jersey ResourceConfig needs annotating?
I am just getting started with Spring Boot and I wanted to implement a ResourceConfig and I finding some conflicting ideas. 我刚开始使用Spring Boot,我想实现ResourceConfig,但发现了一些矛盾的想法。
Take the following 采取以下
@Component
public class JerseyExampleConfig extends ResourceConfig {
The above is annotated with COMPONENT 上面用COMPONENT注释
@Configuration
public class JerseyExampleConfig extends ResourceConfig {
Which one is correct? 哪一个是正确的?
I thought annotating with Configuration would be the correct way but it appears that the Component is used in examples. 我认为用Configuration进行注释是正确的方法,但似乎在示例中使用了Component。
Any ideas ? 有任何想法吗 ?
Whats the difference? 有什么不同?
The documentation suggests @Component
: 该文档建议使用@Component
:
To get started with Jersey 2.x just include the
spring-boot-starter-jersey
as a dependency and then you need one@Bean
of typeResourceConfig
in which you register all the endpoints: 要开始使用2.x的球衣只是包括spring-boot-starter-jersey
作为一个依赖,那么你就需要一个@Bean
类型的ResourceConfig
在您注册的所有端点:@Component public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(Endpoint.class); } }
The documentation also says the following: 该文档还说明以下内容:
You can also register an arbitrary number of beans implementing
ResourceConfigCustomizer
for more advanced customizations. 您也可以注册任意数量的实现ResourceConfigCustomizer
的bean,以进行更高级的自定义。All the registered endpoints should be
@Component
s with HTTP resource annotations (@GET
etc.), eg 所有注册的端点都应该是带有HTTP资源注释的@Component
(@GET
等),例如@Component @Path("/hello") public class Endpoint { @GET public String message() { return "Hello"; } }
Since the
Endpoint
is a Spring@Component
its lifecycle is managed by Spring and you can@Autowired
dependencies and inject external configuration with@Value
. 由于Endpoint
是Spring@Component
其生命周期由Spring管理,您可以@Autowired
依赖项并使用@Value
注入外部配置。 The Jersey servlet will be registered and mapped to/*
by default. 默认情况下,Jersey servlet将被注册并映射到/*
。 You can change the mapping by adding@ApplicationPath
to yourResourceConfig
. 您可以通过将@ApplicationPath
添加到ResourceConfig
来更改映射。
So even if you can not decide which to use for JerseyConfig
you can determinate which is the better in a situation by reading what they actually mean: 因此,即使您不能决定将哪个用于JerseyConfig
也可以通过阅读实际含义来确定哪种情况更好:
@Configuration Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime @Configuration表示一个类声明了一个或多个@Bean方法,并且可以由Spring容器处理以在运行时为这些bean生成bean定义和服务请求
@Component Indicates that an annotated class is a "component". @Component表示带注释的类是“组件”。 Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning. 当使用基于注释的配置和类路径扫描时,此类会被视为自动检测的候选。
@Configuration is meta-annotated with @Component, therefore @Configuration classes are candidates for component scanning @Configuration用@Component进行元注释,因此@Configuration类是组件扫描的候选对象
So by the said above you can annotate your config class with @Configuration
as well but it will be an unnecessary overhead. 因此,通过上述方法,您也可以使用@Configuration
注释您的config类,但这将是不必要的开销。
They both make the ResourceConfig
a Spring Bean, which is all that's really required to make the Spring Boot-Jersey integration work. 它们都使ResourceConfig
成为Spring Bean,这是使Spring Boot-Jersey集成正常工作所需的全部。 You could even just do 你甚至可以做
@SpringBootApplication
class MyApplication {
public static void main(String... args) {}
@Bean
public ResourceConfig jerseyConfig() {
return new MyResourceConfig();
}
}
With this, you wouldn't need @Component
or @Configuration
. 这样,您就不需要@Component
或@Configuration
。 It simply makes the ResourceConfig
a Spring Bean, which like I said, is all that is required. 它只是使ResourceConfig
成为Spring Bean,就象我说的那样,这就是所需要的。
That being said, between the two annotations, @Configuration
is really meant for Spring configuration. 话虽这么说,在两个注释之间, @Configuration
Configuration实际上是用于Spring配置的。 You could place your Spring configuration in the ResourceConfig
subclass, but I would just put it in a separate configuration class, just to keep things separate. 您可以将Spring配置放在ResourceConfig
子类中,但是我只是将其放在单独的配置类中,只是为了使事情分开。 Note that configuration classes are also Spring Beans, so that's why the @Configuration
works. 请注意,配置类也是Spring Bean,所以这就是@Configuration
起作用的原因。
@Component
on the other hand, is a general purpose annotation that will make the class a Spring Bean. 另一方面, @Component
是通用注释,它将使该类成为Spring Bean。 This is the reason all the the examples show using the annotation, as the ResourceConfig
is generally not meant to be a Spring configuration class, and it is less verbose then the example above, not using any annotations. 这就是所有示例都使用注释显示的原因,因为ResourceConfig
通常并不意味着是Spring配置类,并且比上面的示例更冗长,没有使用任何注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.