问题
I have a developing a webservice where in i need to validate a particular httpheader sent in the request against the database.
I want to use RestEasy provider for doing the same as the same functionality needs to be applied to all the service.
@Provider
@ServerInterceptor
public class TestValidationInterceptor implements PreProcessInterceptor {
@Autowired
DetailsDelegate detailsDelegate;
@Override
public ServerResponse preProcess(HttpRequest request, ResourceMethod method) throws Failure, WebApplicationException {
//code to get data using detailsDelegate.
return null;
}
}
public interface DetailsDelegate {
String BEAN_ID = "DetailsDelegate";
/**
* @param appName
* @return
* @throws BaseException
*/
ServiceInfo getServiceDetails(String appName) throws BaseException;
}
@Service("detailsDelegate")
public class DetailsDelegateImpl implements DetailsDelegate {
@Override
public ServiceInfo getServiceDetails(String appName) throws BaseException {
return null;
}
}
The detailsDelegate instance is not getting autowired and is null.
Can someone explain why I am facing this issue.
回答1:
It's best to let spring chose it's bean names so change
@Service("detailsDelegate")
to
@Service
The autowire the interface :
@Auowired
DetailsDelegate
Finally make sure the package in which DetailsDelegate is defined is scanned in your config:
@ComponentScan("com.mypackage")
See http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06s02.html for examples
来源:https://stackoverflow.com/questions/41672758/autowiring-a-spring-managed-bean-in-the-resteasy-providers