spring-boot-starter-webflux can not use org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
so how can spring-boot-starter-webflux use resolveLocale?In other words, what is there in reactor that is similar to resolveLocale?
Spring WebFlux is configuring and using by default a org.springframework.web.server.i18n. LocaleContextResolver (with org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver and org.springframework.web.server.i18n.FixedLocaleContextResolver as implementations).
You can inject the java.util.Locale as a Controller argument (if you're using the annotation variant of Spring WebFlux), or inject and use the configured LocaleContextResolver bean and use it directly.
Related
I am using spring boot with web and jersey (spring-boot-jersey-starter). I have a Jersey endpoint that needs to inject a request scope bean. However, at startup of the application I am getting a no bean found error.
#Component
#Path("blah")
#RequestScoped
public class JerseyController{
#Inject
private MyEntity entity;
}
#Component
public class JerseyConfiguration extends ResourceConfig{
public JeyseyConfiguration(){
register(JeyseyController.class);
registere(MyEntityProvider.class);
}
}
Is there a way, in a spring-boot web app, to prevent Spring from attempting to instantiate and inject my JerseyController until an HTTP request is received so that the injected dependency can be provided by my Jersey provider?
#Component is not required on Jersey resources. Having it will cause Spring to instantiate it (with default Singleton scope). I don't think Spring doesn't respect the #RequestScoped. This is a Jersey annotation. If you want to use the #Component, I think the Spring #Scope("request") might do the trick though.
You can also remove the #RequestScoped. This is the default scope for Jersey resources.
The only time I have ever found a need to use #Component on Jersey resources, is if I need to use the Spring #Value (maybe AOP also, but I don't do much AOP). Other than that, the Jersey-Spring integration already supports the most common used feature of Spring which is DI. And if you really want to make the Jersey resource a singleton, Jersey supports the #Singleton annotation.
Is it possible to intergrate Spring in JAX-RS-API? We wan't use no implementation of JAX-RS (no imports of Jersey, CXF, etc. in our Java code)
Yes it's possible.
You will define #Path and #Component with jax-rs import.
You will define #Controller with Spring annotation.
The link between both will be made by the applicationContext or by annotation.
So, in my opinion yes, you can.
What is the equivalent of Guice's #ImplementedBy annotation in Spring DI? (I googled it but with no results.)
There is not exist JIT default binding in Spring. You can set only one implementation to dependency or use naming qualifier #Named or #Qualifier annotation to specify implementation but this is static binding (not equivalent to #ImplementedBy Guice implementation).
I created a spring extension enabling the jit-binding. this library add #ImplementedBy annotation to Spring. See https://github.com/devacfr/spring-implementedby and give me feedback
You can use the annotation #Qualifier
http://static.springsource.org/spring-framework/docs/3.2.0.M2/api/org/springframework/beans/factory/annotation/Qualifier.html
Can I use #Inject annotation in my Spring application, when I will deploy my application in Appserver that doesn't support Java EE 6?
#Inject is introduced in Java EE6 and it doesn't supported by Java EE 5
When you use #Inject in components managed by Spring (Spring beans) its functionality is implemented by Spring, therefore you don't need anything else to make it work.
#Inject support in JavaEE 6 is about components managed by application server (EJBs, etc).
Spring has a synonymous #Autowired annotation that has the same effect. Since it's provided by Spring itself, it should be available on any version of Java that supports annotations.
JSR 330's #Inject annotation can be used in place of Spring's #Autowired in the examples below. #Inject does not have a required property unlike Spring's #Autowire annotation which has a required property to indicate if the value being injected is optional. This behavior is enabled automatically if you have the JSR 330 JAR on the classpath.
spring documentation......
http://static.springsource.org/spring/docs/3.0.0.RC2/reference/html/ch03s09.html
In short, you can. If you are using Spring (>= version 3), Spring will perform its dependency injection base on the #Inject annotations of its beans.
In long, it all depends on what do you mean by "Can be used". Annotation is nothing but a meta data. No one stop you from using #Inject to perform a totally irrelevant function, as long as you inspect the annotation and do whatever you want
I set up this binding with Guice
bindConstant().annotatedWith( SecurityCookie.class ).to("JSESSIONID");
I need to migrate to Spring.
What would be the equivalent code with Spring ?
Use FieldRetrievingFactoryBean.