Как ввести синглтон-боб в нормальный боб - PullRequest
0 голосов
/ 25 апреля 2019

Я получаю сообщение об ошибке при попытке развернуть мое приложение на Wildfly16

У меня есть bean-компонент без сохранения состояния FileInfoService , внутри этого бина я внедряю синглтон-компонент FileSeqCounter . Wildfly жалуется на синглтон-бин с ошибкой ниже

Я пытался изменить ввод аннотации с @Inject на @EJB, но та же ошибка.

Как я могу назвать синглтон-бин внутри обычного бина.

Ценю помощь

Failed to execute goal deploy: {"WFLYCTL0062: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-1" => {"WFLYCTL0080: Failed services" => {"jboss.deployment.subunit.\"Application1.ear\".\"APP-WEB-0.0.1-SNAPSHOT.war\".component.FileSeqCounterImpl.START" => "java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
[ERROR]     Caused by: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
[ERROR]     Caused by: javax.ejb.EJBException: java.lang.IllegalArgumentException:
 Can not set xxx.file.bean.FileInfoService field xx.file.bean.FileSeqCounterImpl.fileService to xxx.file.bean.FileInfoService$1277578869$Proxy$_$$_Weld$EnterpriseProxy$
[ERROR]     Caused by: java.lang.IllegalArgumentException: Can not set xxx.file.bean.FileInfoService field xxx.file.bean.FileSeqCounterImpl.fileService to xxx.file.bean.FileInfoService$1277578869$Proxy$_$$_Weld$EnterpriseProxy$"}}}}

мой код

FileInfoService.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.ejb.Local;
import xxx.model.file.FileInfo;

@Local
public interface FileInfoService {

    public Long getMaxFileSeq();

    public Long storeFile(FileInfo file) throws FileNotFoundException, IOException;

    public File getFile(Long UID);
}

FileInfoServiceImpl

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;

import org.un.sw.ee.jm.data.dao.file.FileDao;
import org.un.sw.ee.jm.model.file.FileInfo;

@Stateless
public class FileInfoServiceImpl implements FileInfoService {

    private static String tempPath = System.getProperty("jboss.server.temp.dir");

    @EJB
    FileSeqCounter counter;

    @Inject
    FileDao fileDao;

    @Inject
    FileStorageService storageSer;

    @Override
    public void saveFileInfo(FileInfo entity) {
        fileDao.insert(entity);

    }

    @Override
    public Long storeFile(FileInfo file) throws IOException {
        OutputStream out = null;

        InputStream data = file.getData();

        String filePath = FileInfoServiceImpl.tempPath + "/" + file.getFileName();

        try {
            OutputStream outpuStream = new FileOutputStream(new File(filePath));
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = data.read(bytes)) != -1) {
                outpuStream.write(bytes, 0, read);
            }
            outpuStream.flush();
            outpuStream.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
        // get the file seq
        Long count = counter.getFileCounter();
        file.setFileSeq(count);
        fileDao.insert(file);
        return count;

    }

FileSeqCounter

import javax.ejb.Local;

@Local
public interface FileSeqCounter {

    public Long getFileCounter();

}

FileSeqCounterImpl

import javax.annotation.PostConstruct;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;

@Singleton
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class FileSeqCounterImpl implements FileSeqCounter {

   private Long fileCounter = 0L;

   @Inject
   FileInfoService fileService;

   @PostConstruct
   private void setFilecounter() {
       Long counter=fileService.getMaxFileSeq();
       if(counter!=null) {
           this.fileCounter=counter;       
       }
   }

    @Override
    @Lock(LockType.WRITE)
    public Long getFileCounter() {
    return this.fileCounter++;
    }

}

1 Ответ

0 голосов
/ 26 апреля 2019

Проблема связана с аннотацией @startup, FileInfoService создается до FileInfoService.даже использование @DependsOn не помогло.

После удаления аннотации @Startup не выдается никакой ошибки, и бин Singleton будет создан после первого вызова

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...