首页 > 技术文章 > 调用InitializingBean之后想要销毁,使用DisposableBean

jxtpro 2021-11-04 12:57 原文

java

@Log4j2
@Component
public class Neo4jStore implements InitializingBean, DisposableBean{
    private GraphDatabaseService graphDb;
    private final BoltConnector bolt = new BoltConnector("0");
    @Value("${neo4j.dataPath}")
    private String dataPath;
    @Value("${neo4j.ipPort}")
    private String ipPort;

    @Override
    public void destroy() throws Exception {
        graphDb.shutdown();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        graphDb = new GraphDatabaseFactory()
            .newEmbeddedDatabaseBuilder(new File(dataPath))
            .setConfig(bolt.type, "BOLT")
            .setConfig(bolt.enabled, "TRUE")
            .setConfig(bolt.listen_address, ipPort)
            .setConfig(GraphDatabaseSettings.cypher_lenient_create_relationship,"TRUE")
            .newGraphDatabase();
        Procedures proceduresService = ((GraphDatabaseAPI) graphDb).getDependencyResolver().resolveDependency(Procedures.class, DependencyResolver.SelectionStrategy.ONLY);
        try {
            proceduresService.registerFunction(Neo4jFunction.class);
            log.info("connect local neo4j database success");
        } catch (KernelException e) {
            log.info("connect local neo4j database error");
            throw new RuntimeException("register function error", e);
        }
    }
}

推荐阅读