Apache 化学 session 与 spring

apache chemistry session with spring

我目前正在开发 java/jee 应用程序,使用 spring 作为框架,使用 alfresco 作为 ged。我正在使用 apache chemistry 连接到 alfresco 存储库。 这是我用来获取 session.

的代码

有没有办法用 spring bean 更改此代码,因为我将在不同的 类 中使用此 session 并且最好是单例。

Map<String, String> parameter = new HashMap<String, String>();

    // user credentials
    parameter.put(SessionParameter.USER, "admin");
    parameter.put(SessionParameter.PASSWORD, "admin");

    // connection settings
    parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:8080/alfresco/cmisatom");
    parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
    System.out.println(BindingType.ATOMPUB.value());
    // set the alfresco object factory
    parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");

    // create session
    SessionFactory factory = SessionFactoryImpl.newInstance();
    Session session = factory.getRepositories(parameter).get(0).createSession();

只需将其声明为 bean:

@Bean
public Session sessionBean() {
    Map<String, String> parameter = new HashMap<String, String>();
    // ...
    SessionFactory factory = SessionFactoryImpl.newInstance();
    Session session = factory.getRepositories(parameter).get(0).createSession();
    return session;
}

因此您可以在任何需要的地方注入这个会话 bean。