Autowiring a spring managed bean in the resteasy providers

假如想象 提交于 2019-12-23 04:55:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!