NutzCN Logo
问答 ioc初始化neo4j失败
发布于 1736天前 作者 qq_8c799e25 1775 次浏览 复制 上一个帖子 下一个帖子
标签:

准备将neo4j通过ioc来初始化,这样写的:

neo.js (和dao.js一个目录)

var ioc = {
        confneo : {
            type : "org.nutz.ioc.impl.PropertiesProxy",
            fields : {
                paths : ["neo4j/"]
            }
        },
        neo4jdb : {
            type : "org.neo4j.graphdb.GraphDatabaseService",
            factory : "com.neo4j.Neo4jDbFactory#getGraphDatabase",
            args : [{refer:"confneo"}] //引用neo4j的conf
        }
};

com.neo4j.Neo4jDbFactory的代码:

package com.neo4j;


public class Neo4jDbFactory {

    
	
	private static final Log log = Logs.get();
	
	public static GraphDatabaseService getGraphDatabase(org.nutz.ioc.impl.PropertiesProxy pp){
	
		String dbPath = pp.get("dbms.directories.data", "D:\\Run\\neo4j_db"); 
        File storeDir = new File(dbPath);

        ServerBootstrapper serverBootstrapper = new CommunityBootstrapper();
        String cfgFilePath = ResourcesReader.getClassRootPath(Neo4jDbFactory.class) + "/neo4j/neo4j_conf.properties";

        log.info("Neo4jDbFactory!!! - getGraphDatabase dbPath:" + dbPath + ", cfgFilePath:" + cfgFilePath);
        
        
        //社区版的bug,部分属性无法直接从配置文件写入(例如7474端口号),手动覆盖一下
        Optional<File> cfgFile = Optional.of(new File(cfgFilePath));
        Set<String>  cfgKeys = pp.keySet();
        Map<String, String> configOverrides = new HashMap<>(cfgKeys.size());
        for (String cfgKey : cfgKeys) {//把所有配置都读到map里,解决默认端口无法修改的问题
            configOverrides.put(cfgKey, pp.get(cfgKey));
        }

        //启动服务端模式
        serverBootstrapper.start(storeDir, cfgFile, configOverrides);
        NeoServer neoServer = serverBootstrapper.getServer();
        //获取内嵌模式db对象
        GraphDatabaseService graphDb = neoServer.getDatabase().getGraph(); 
        
        return graphDb;
	}

}

MainSetup.java:

package com.smart.project;


public class MainSetup implements Setup{


//	public static Dao dao = null;

	@Override
	public void init(NutConfig conf) {
		...
		
		Ioc ioc = conf.getIoc();
		Dao dao = ioc.get(Dao.class);
        

        
      GraphDatabaseService gdb = ioc.get(GraphDatabaseService.class); //异常发生的地方
        log.info("GraphDatabaseService - " + gdb);
	}
}

MainModule.java:

@Views({BeetlViewMaker.class})
@SessionBy(ShiroSessionProvider.class)
@Ok("json:full")
@Fail("jsp:jsp.500")
@ChainBy(args="mvc/smart-mvc-chain.js")
@SetupBy(value=MainSetup.class)
@IocBy(type=ComboIocProvider.class, args={"*js", "ioc/",
    "*anno", "com.smart.project",
    "*tx", "*org.nutz.integration.quartz.QuartzIocLoader"})
@Modules(scanPackage=true) 
@Localization(value="msg/", defaultLocalizationKey="zh-CN")
public class MainModule {

}

GraphDatabaseService是一个接口,不清楚得到的具体类名
public interface GraphDatabaseService{
}
错误日志:

2020-02-19 23:08:31,504 INFO  16 - Scheduler smartScheduler_$_NON_CLUSTERED started.
2020-02-19 23:08:31,600 ERROR 16 - Error happend during start serivce!
org.nutz.ioc.IocException: IocBean[graphDatabaseService] For object [graphDatabaseService] - type:[interface org.neo4j.graphdb.GraphDatabaseService]
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:250)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:271)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:161)
	at com.smart.project.MainSetup.init(MainSetup.java:82)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:278)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:91)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:69)
	at com.smart.project.mvc.SmartNutFilter.init(SmartNutFilter.java:23)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4700)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5340)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.nutz.ioc.ObjectLoadException: Object 'graphDatabaseService' without define!
	at org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:157)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:195)
	... 21 more
2020-02-19 23:08:31,605 ERROR 16 - try to depose ioc

我想在MainSetup里一次性初始化graphDatabaseService,但是失败,猜测必须指定graphDatabaseService的类名?但工厂模式返回的只有接口,不确定具体的类名,怎么解决呢?

4 回复
ioc.get(GraphDatabaseService.class, "neo4jdb");

如果版本不是太老, 可以考虑用@IocBean工厂方法, 写起来更顺畅一些

GraphDatabaseService gdb = ioc.get(GraphDatabaseService.class, "neo4jdb");

试过,可以!

@IocBean加到哪里? 我在MainSetup上加不行。

neo.js的neo4jdb定义删掉, 把类改一下

@IocBean
public class Neo4jDbFactory {

         @Inject
         PropertiesProxy confneo;
	
	private static final Log log = Logs.get();
	
        @IocBean(name="neo4jdb")
	public GraphDatabaseService getGraphDatabase(){
	
		String dbPath = confneo.get("dbms.directories.data", "D:\\Run\\neo4j_db"); 
        File storeDir = new File(dbPath);

        ServerBootstrapper serverBootstrapper = new CommunityBootstrapper();
        String cfgFilePath = ResourcesReader.getClassRootPath(Neo4jDbFactory.class) + "/neo4j/neo4j_conf.properties";

        log.info("Neo4jDbFactory!!! - getGraphDatabase dbPath:" + dbPath + ", cfgFilePath:" + cfgFilePath);
        
        
        //社区版的bug,部分属性无法直接从配置文件写入(例如7474端口号),手动覆盖一下
        Optional<File> cfgFile = Optional.of(new File(cfgFilePath));
        Set<String>  cfgKeys = confneo.keySet();
        Map<String, String> configOverrides = new HashMap<>(cfgKeys.size());
        for (String cfgKey : cfgKeys) {//把所有配置都读到map里,解决默认端口无法修改的问题
            configOverrides.put(cfgKey, confneo.get(cfgKey));
        }

        //启动服务端模式
        serverBootstrapper.start(storeDir, cfgFile, configOverrides);
        NeoServer neoServer = serverBootstrapper.getServer();
        //获取内嵌模式db对象
        GraphDatabaseService graphDb = neoServer.getDatabase().getGraph(); 
        
        return graphDb;
	}

}

添加回复
请先登陆
回到顶部