dropwizard - 如何在 dropwizard 应用程序中组织、存储库和服务
问题描述
我有一个 dropwizard 应用程序,我正在注册我的资源,如下所示
@Override
public void run(final BackendConfiguration configuration, final Environment environment) {
try {
Stripe.apiKey = configuration.getSTRIPE_SECRET_KEY(); // Initializing
Datastore datastore = morphiaBundle.getDatastore();
ProductEntityDAO dao = new ProductEntityDAO(datastore);
CandidateEntityDao candidateEntityDao = new CandidateEntityDao(datastore);
PaymentHistoryEntityDao paymenthistory = new PaymentHistoryEntityDao(datastore);
PaymentFailedEntityDao paymentFailedEntityDao = new PaymentFailedEntityDao(datastore);
environment.jersey().register(new StripeResource(dao, candidateEntityDao, paymenthistory, client,
configuration.getApi(), configuration.getApiToken(), configuration.getCurrency()));
// Dependency injection
environment.jersey().register(new AbstractBinder() {
@Override
public void configure() {
bindAsContract(PaymentHistoryEntityDao.class).in(Singleton.class);
bindAsContract(PaymentService.class).in(Singleton.class);
}
});
final FilterRegistration.Dynamic cors = environment.servlets().addFilter("CORS", CrossOriginFilter.class);
// Configure CORS parameters
cors.setInitParameter("allowedOrigins", "*");
cors.setInitParameter("allowedHeaders", "X-Requested-With,Content-Type,Accept,Origin");
cors.setInitParameter("allowedMethods", "OPTIONS,GET,PUT,POST,DELETE,HEAD");
// Add URL mapping
cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
} catch (Exception e) {
System.out.println("Exception occurred");
}
}
}
当我试图在我的 StripeResource 中进行依赖注入时,如下所示
public class StripeResource {
private ProductEntityDAO dao;
private PaymentHistoryEntityDao paymentHistoryDao;
private String API_KEY;
private CandidateEntityDao candidateDao;
private Client client;
private String evaluationServiceApi;
private String currency;
@Inject
private PaymentService service;
public StripeResource(ProductEntityDAO dao, CandidateEntityDao candidateDao, PaymentHistoryEntityDao paymentHistoryDao, Client client, String evaluationServiceApi, String API_KEY,String currency) {
this.client = client;
this.evaluationServiceApi = evaluationServiceApi;
this.dao = dao;
this.candidateDao = candidateDao;
this.API_KEY = API_KEY;
this.paymentHistoryDao = paymentHistoryDao;
this.currency=currency;
}
@Inject
public StripeResource(PaymentService service)
{
this.service=service;// always null may be this constructor is never called
}
}
该服务始终为空,我正在寻找一种在这种情况下进行依赖注入的技术
对 PaymentService 的 dao 注入也不起作用
@Inject
public PaymentService(PaymentHistoryEntityDao dao){
this.dao = dao; // this is null in the app too
}
解决方案
推荐阅读
- mysql - MySQL select orderby date 返回重复值
- angular - angular cli中的代理设置
- c# - 如何将 Xamarin 编辑器文本转换为 xml 文件?
- php - 如何将 mysqli 结果从 php 页面传递到另一个页面?
- python - 我怎样才能为硒设置我的壁虎驱动程序?
- c - 添加到Redis的`dict`时如何解决地址边界错误?
- batch-file - 在批处理脚本中提取子字符串
- sql - 如何在 MS SQL Server 中每 5 分钟选择一次时间戳?
- javascript - WaitForTarget 上的 Puppeteer 超时
- apache-kafka - 什么是延迟处理事件的可行解决方案?