从@Context注入的变量中初始化其他变量
Initialize other variables from @Context injected variable
我在实施 neo4j Graphaware 解决方案时遇到问题。以前我正在创建 GraphDatabaseService 对象,如下所示 -
public class TimetreeUtil {
public static GraphDatabaseService db;
public static SingleTimeTree st;
public static TimeTreeBackedEvents ttbe;
public static TimeTreeBusinessLogic ttbl;
public static TimedEventsBusinessLogic tebl;
public static List<Event> eventsFetchedFromTimetree;
public TimetreeUtil() {
db = new GraphDatabaseFactory( ).newEmbeddedDatabase(new File("..."));
st = new SingleTimeTree(db);
ttbl = new TimeTreeBusinessLogic(db);
ttbe = new TimeTreeBackedEvents(st);
tebl = new TimedEventsBusinessLogic(db, ttbe);
}
}
这工作正常。如您所见,GraphDatabaseService、SingleTimeTree、TimeTreeBackedEvents、TimeTreeBusinessLogic 和 TimedEventsBusinessLogic - 都是静态的,它们应该是静态的,因为 neo4j 要求这样做。
但现在我们的架构已经改变,我们通过 -
注入 GraphDatabaseService
@Context
public GraphDatabaseService db;
所以现在 class 看起来像 -
public class TimetreeUtil {
@Context
public GraphDatabaseService db;
public static SingleTimeTree st;
public static TimeTreeBackedEvents ttbe;
public static TimeTreeBusinessLogic ttbl;
public static TimedEventsBusinessLogic tebl;
public static List<Event> eventsFetchedFromTimetree;
public TimetreeUtil() {
st = new SingleTimeTree(db);
ttbl = new TimeTreeBusinessLogic(db);
ttbe = new TimeTreeBackedEvents(st);
tebl = new TimedEventsBusinessLogic(db, ttbe);
}
}
助手 class Timetree 只是通过 TimetreeUtil util = new TimetreeUtil();
创建 TimetreeUtil class 的一个对象,然后调用 TimetreeUtil 的一个方法。
我假设在调用构造函数时,db 已经初始化,但事实并非如此。 db 为空,因此 st = new SingleTimeTree(db);
给出 NPE。
怎样才能两全其美?谢谢。
依赖注入在创建对象后运行 - Java 必须在设置对象实例变量之前创建对象 - 因此 NPE。
您可能能够在构造函数中传递对象(值得测试,但我不确定它是否有效):
private GraphDatabaseService db;
public static SingleTimeTree st;
public static TimeTreeBackedEvents ttbe;
public static TimeTreeBusinessLogic ttbl;
public static TimedEventsBusinessLogic tebl;
public static List<Event> eventsFetchedFromTimetree;
public TimetreeUtil(@Context GraphDatabaseService db) {
this.db = db
st = new SingleTimeTree(db);
ttbl = new TimeTreeBusinessLogic(db);
ttbe = new TimeTreeBackedEvents(st);
tebl = new TimedEventsBusinessLogic(db, ttbe);
}
我在实施 neo4j Graphaware 解决方案时遇到问题。以前我正在创建 GraphDatabaseService 对象,如下所示 -
public class TimetreeUtil {
public static GraphDatabaseService db;
public static SingleTimeTree st;
public static TimeTreeBackedEvents ttbe;
public static TimeTreeBusinessLogic ttbl;
public static TimedEventsBusinessLogic tebl;
public static List<Event> eventsFetchedFromTimetree;
public TimetreeUtil() {
db = new GraphDatabaseFactory( ).newEmbeddedDatabase(new File("..."));
st = new SingleTimeTree(db);
ttbl = new TimeTreeBusinessLogic(db);
ttbe = new TimeTreeBackedEvents(st);
tebl = new TimedEventsBusinessLogic(db, ttbe);
}
}
这工作正常。如您所见,GraphDatabaseService、SingleTimeTree、TimeTreeBackedEvents、TimeTreeBusinessLogic 和 TimedEventsBusinessLogic - 都是静态的,它们应该是静态的,因为 neo4j 要求这样做。
但现在我们的架构已经改变,我们通过 -
注入 GraphDatabaseService@Context
public GraphDatabaseService db;
所以现在 class 看起来像 -
public class TimetreeUtil {
@Context
public GraphDatabaseService db;
public static SingleTimeTree st;
public static TimeTreeBackedEvents ttbe;
public static TimeTreeBusinessLogic ttbl;
public static TimedEventsBusinessLogic tebl;
public static List<Event> eventsFetchedFromTimetree;
public TimetreeUtil() {
st = new SingleTimeTree(db);
ttbl = new TimeTreeBusinessLogic(db);
ttbe = new TimeTreeBackedEvents(st);
tebl = new TimedEventsBusinessLogic(db, ttbe);
}
}
助手 class Timetree 只是通过 TimetreeUtil util = new TimetreeUtil();
创建 TimetreeUtil class 的一个对象,然后调用 TimetreeUtil 的一个方法。
我假设在调用构造函数时,db 已经初始化,但事实并非如此。 db 为空,因此 st = new SingleTimeTree(db);
给出 NPE。
怎样才能两全其美?谢谢。
依赖注入在创建对象后运行 - Java 必须在设置对象实例变量之前创建对象 - 因此 NPE。
您可能能够在构造函数中传递对象(值得测试,但我不确定它是否有效):
private GraphDatabaseService db;
public static SingleTimeTree st;
public static TimeTreeBackedEvents ttbe;
public static TimeTreeBusinessLogic ttbl;
public static TimedEventsBusinessLogic tebl;
public static List<Event> eventsFetchedFromTimetree;
public TimetreeUtil(@Context GraphDatabaseService db) {
this.db = db
st = new SingleTimeTree(db);
ttbl = new TimeTreeBusinessLogic(db);
ttbe = new TimeTreeBackedEvents(st);
tebl = new TimedEventsBusinessLogic(db, ttbe);
}