Reputation: 1407
I would like wrap UriInfo
into my custom Query object and make it available in other Spring
components. I was able to do so with HK2 like this
package test.service;
@Path("/test")
public class TestResource {
@Inject
private Query query;
@GET
public String test() {
return "found: " + query.toString();
}
}
with proper factory
package test.utils;
public class QueryFactory implements Factory<Query> {
@Inject
private UriInfo uriInfo;
@Override
public Query provide() {
Query query = new Query();
Iterator<String> it = uriInfo.getQueryParameters().keySet().iterator();
if(it.hasNext()) {
String key = it.next();
List<String> valueList = uriInfo.getQueryParameters().get(key);
String value;
if(valueList.isEmpty())
value = "no value";
else
value = valueList.get(0);
query.setKey(key);
query.setValue(value);
} else {
query.setKey("no query");
}
return query;
}
@Override
public void dispose(Query query) {
}
}
Now I would like to switch to spring. I tried by adding configuration
package test.config
@Configuration
@ComponentScan("test")
public class SpringConfiguration {
@Autowired
QueryFactory queryFactory;
@Bean
public Query query() throws Exception {
return queryFactory.getObject();
}
}
updating web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>test.config.SpringConfiguration</param-value>
</context-param>
changing TestResource
package test.service;
@Path("/test")
@Component
public class TestResource {
@Autowired
private Query query;
@GET
public String test() {
return "found: " + query.toString();
}
}
and adding annotation
to QueryFactory
@Component
@Scope("request")
public class QueryFactory implements FactoryBean<Query>
I understand that RequestContextListener
is required to work with request scope. However, when I run it I get exception that tells me
`java.lang.IllegalStateException: No thread-bound request found:`
Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of
DispatcherServlet/DispatcherPortlet:
In this case, use RequestContextListener or RequestContextFilter to expose the current request.
What is ridiculous since I already added RequestContextListener to web.xml.
If I run without ContextLoaderListener set, then I get NullPointerException in TestResource because query is null.
I cannot see what is wrong. Can you point it out to me how should be this configuration done properly?
Upvotes: 0
Views: 5111
Reputation: 8324
The problem is that you're telling Spring that QueryFactory has a request scope
@Component
@Scope("request")
public class QueryFactory
So, this objec only exists in the middle of a web request. But you're trying to use it outside a web request.
@Autowired
QueryFactory queryFactory;
You can see it in the Exception
Are you referring to request attributes outside of an actual web request
Upvotes: 2