首页 > 解决方案 > 如何在 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
        
    }

标签: dropwizard

解决方案


推荐阅读