【问题标题】:Autowiring a spring managed bean in the resteasy providers在 resteasy 提供程序中自动装配一个 spring 管理的 bean
【发布时间】:2017-01-16 09:08:09
【问题描述】:

我正在开发一个 web 服务,我需要验证在请求中针对数据库发送的特定 httpheader。

我想使用 RestEasy 提供程序来做同样的事情,因为需要将相同的功能应用于所有服务。

@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;
   }
}

detailsDelegate 实例未自动装配并且为空。

谁能解释我为什么会遇到这个问题。

【问题讨论】:

  • 你能分享你的配置和 DetailsDelegate 实现或类吗?
  • 已在问题中更新。

标签: spring web-services resteasy jboss6.x


【解决方案1】:

最好让spring选择它的bean名称所以改变

@Service("detailsDelegate")

@Service

自动装配接口:

@Auowired
DetailsDelegate 

最后确保在您的配置中扫描了定义 DetailsDelegate 的包:

@ComponentScan("com.mypackage")

有关示例,请参阅 http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06s02.html

【讨论】:

  • 命名约定没有问题。在其他班级工作也是如此。我相信他们有不同的上下文,一个是 spring,另一个是 jax rs。
猜你喜欢
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 2012-07-14
  • 2020-07-09
  • 2011-01-23
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多