简体   繁体   中英

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.

Take the following

@Component
public class JerseyExampleConfig extends ResourceConfig {

The above is annotated with 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.

Any ideas ?

Whats the difference?

The documentation suggests @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 type ResourceConfig in which you register all the endpoints:

 @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.

All the registered endpoints should be @Component s with HTTP resource annotations ( @GET etc.), eg

 @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 . The Jersey servlet will be registered and mapped to /* by default. You can change the mapping by adding @ApplicationPath to your 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:

@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

@Component Indicates that an annotated class is a "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

So by the said above you can annotate your config class with @Configuration as well but it will be an unnecessary overhead.

They both make the ResourceConfig a Spring Bean, which is all that's really required to make the Spring Boot-Jersey integration work. 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 . It simply makes the ResourceConfig a Spring Bean, which like I said, is all that is required.

That being said, between the two annotations, @Configuration is really meant for Spring configuration. 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. Note that configuration classes are also Spring Beans, so that's why the @Configuration works.

@Component on the other hand, is a general purpose annotation that will make the class a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM