为什么这些application.properties不起作用?

esbemjvw  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(326)

我已经实现了一个简单的quarkus应用程序,用于创建cmisapi来与alfresco一起工作。遵循本教程(只是提供一点见解)
一切都很顺利,直到我决定使用属性来传递会话参数。我已将这些添加到main/resources/application.properties中的application.properties,如下所示:


# Session properties

session.host=localhost
session.port=80
session.url=http://localhost:80/alfresco/api/-default-/cmis/versions/1.1/atom
session.compression=true
session.cache_ttl_objects=0

然后我尝试在我使用它们的类中直接定义它们,但是由于得到的是空值,我四处寻找为什么会发生这种情况,并发现了这个问题。也就是说,我遵循了这个结构,这个结构应该可以解决这个问题。
我创建了sessionconfig.java类:

package com.c4pa.cmisservice;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.quarkus.arc.config.ConfigProperties;

@ConfigProperties(prefix = "session") // Already tried without this, using session.url, etc on each Property
public class SessionConfig {

    @Inject // Already tried without this
    @ConfigProperty(name = "url")
    private String url;

    @Inject // Already tried without this
    @ConfigProperty(name = "compression", defaultValue = "true")
    private String compression;

    @Inject // Already tried without this
    @ConfigProperty(name = "cache_ttl_objects", defaultValue = "0")
    private String cacheTTLObjects;

    public String getUrl(){
        return this.url;
    }

    public void setUrl(String url){
        this.url = url;
    }

    public String getCompression(){
        return this.compression;
    }

    public void setCompression(String compression){
        this.compression = compression;
    }

    public String getCacheTTLObjects(){
        return this.cacheTTLObjects;
    }

    public void setCacheTTLObjects(String cacheTTLObjects){
        this.cacheTTLObjects = cacheTTLObjects;
    }

}

我正在尝试使用这个类cmisclient.java的属性:

@ApplicationScoped
public class CMISClient {

    private static Map<String, Session> connections = new ConcurrentHashMap<String, Session>();

    public CMISClient() { }

    @Inject
    SessionConfig sessionConfig;

    public Session getSession(String connectionName, String username, String pwd) {
        Session session = connections.get(connectionName);
        System.out.println(sessionConfig.getUrl() + "|" + sessionConfig.getCompression() + "|" + sessionConfig.getCacheTTLObjects());
        if (session == null) {
            // No connection to Alfresco available, creating a new one
            SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put(SessionParameter.USER, username);
            parameters.put(SessionParameter.PASSWORD, pwd);
            parameters.put(SessionParameter.ATOMPUB_URL, sessionConfig.getUrl());
            parameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
            parameters.put(SessionParameter.COMPRESSION, sessionConfig.getCompression());
            parameters.put(SessionParameter.CACHE_TTL_OBJECTS, sessionConfig.getCacheTTLObjects());
            ...
        }
        return session;
    }
...
}

但是当我调用调用getsession方法的端点时,它会在getsession方法的println上产生org.jboss.resteasy.spi.unhandledexception:java.lang.nullpointerexception。当然,如果我对此进行注解,我将在parameters.put行上得到相同的异常。
反转堆栈跟踪(如果有用):

java.lang.NullPointerException
    at com.c4pa.cmisservice.CMISClient.getSession(CMISClient.java:46)
    at com.c4pa.cmisservice.CMISResource.createConnection(CMISResource.java:42)
    at com.c4pa.cmisservice.CMISResource.send(CMISResource.java:25)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:170)
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:130)
    at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:643)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:507)
    at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$2(ResourceMethodInvoker.java:457)
    at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:364)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:459)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:419)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:393)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:68)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:492)
    at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:261)
    at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:161)
    at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:364)
    at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:164)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:247)
    at io.quarkus.resteasy.runtime.standalone.RequestDispatcher.service(RequestDispatcher.java:73)
    at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatch(VertxRequestHandler.java:136)
    at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.access$000(VertxRequestHandler.java:40)
    at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler$1.run(VertxRequestHandler.java:97)
    at io.quarkus.runtime.CleanableExecutor$CleaningRunnable.run(CleanableExecutor.java:231)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2046)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1578)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
    at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
    at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
    at java.base/java.lang.Thread.run(Thread.java:834)
    at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Resulted in: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
    at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:106)
    at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:372)
    at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:218)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:519)
    ... 20 more

你知道为什么会这样吗?提前多谢了。
编辑:

@Path("/file")
public class CMISResource {

    private CMISClient cmisClient;
    private String connectionName;
    private Session session;

    @GET
    @Path("/send")
    @Produces(MediaType.TEXT_PLAIN)
    public String send() throws IOException {
        this.createConnection();
        Folder folder = this.cmisClient.createFolder(session);
        Document document = cmisClient.createDocument(session, folder);
        return document.getName() + " was succesfully created (at least I hope so)";
    }

    private void createConnection() {
        this.cmisClient = new CMISClient();
        this.connectionName = "c4paAlf01";
        this.session = cmisClient.getSession(connectionName, "username", "password");
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题