我想在我的应用程序中实现DI,但在我这样做后,我遇到了CRUD方法的问题。
我创建了界面:
library(tidyverse)
data.frame(A.max = map_dbl(A, max)) %>%
mutate(Lat = map2(B, map(A, which.max), `[[`),
Lon = map2(C, map(A, which.max), `[[`))
# A.max Lat Lon
# 1 46952.85 15.32102 -84.19034
# 2 125267.69 18.55998 -102.5255
# 3 26513.05 17.625 -77.625
# 4 39570.96 18.49643 -69.63036
# 5 52291.77 19.95336 -75.86194
这是appModule:
@Singleton
@Component(modules = {AppModule.class, DbModule.class, AuthGatewayModule.class})
public interface AppComponent {
Context getContext();
GymService getGymService();
KitchenService getKitchenService();
ActivitiesService getActivitiesService();
AuthGateway getAuthGateway();
LandingService getLandingService();
Logger getLogger();
void inject(Logger logger);
}
和dbModule:
@Module
public class AppModule {
private final Context context;
public AppModule(Context context) {
this.context = context;
}
@Provides
@Singleton
public Context provideContext() {
return context;
}
@Provides
@Singleton
public Logger logger() {
return new Logger(context);
}
}
这是我的KitchenService类,有一个示例方法:
@Module
public class DbModule {
@Provides
public DbService provideDbService(Logger logger) {
return new DatabaseController(Regions.EU_CENTRAL_1, logger);
}
}
我想在DB中添加一些产品,在我实现DI之前工作正常,但现在我有了java.lang.NullPointerException。
这是AddProductLambda类:
public class KitchenService {
private DatabaseController databaseController;
private LambdaLogger logger;
@Inject
public KitchenService(){}
public KitchenService(DatabaseController databaseController, LambdaLogger logger) {
this.databaseController = databaseController;
this.logger = logger;
}
//PRODUCT CRUD METHODS
public Product addProduct(Product product) {
databaseController.save(product);
logger.log("Product created");
return product;
}
AddProductLambda扩展的Lambda:
public class AddProductLambda extends Lambda {
@Override
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
super.handleRequest(input, context);
try {
Product product = RequestUtil.parseRequestBody(input, Product.class);
KitchenService kitchenService = appComponent.getKitchenService();
Product addedProduct = kitchenService.addProduct(product);
return ResponseUtil.generateResponse(HttpStatus.SC_CREATED, addedProduct);
} catch (IllegalArgumentException e) {
return ResponseUtil.generateResponse(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
}
}
我做错了什么?有人可以帮我解决这个问题吗?