如何将Singleton bean注入普 通bean

问题描述 投票:0回答:1

尝试在Wildfly16上部署我的应用程序时出错

我有一个无状态bean FileInfoService,在这个bean里面,我注入了一个单例bean FileSeqCounter。 Wildfly正在抱怨单例bean,错误如下

我试图将注释从@Inject更改为@EJB但是同样的错误。

我如何在普通bean中调用单例bean。

感谢帮助

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$"}}}}

我的代码

file info service.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++;
    }

}
dependency-injection singleton wildfly javabeans
1个回答
0
投票

@startup注释是问题,FileInfoService是在FileInfoService之前构造的。即使使用@DependsOn也无济于事。

删除@Startup注释后,不会抛出任何错误,并且在第一次调用后将构造Singleton bean

© www.soinside.com 2019 - 2024. All rights reserved.