如何将Singleton bean注入普​​通bean

时间:2019-04-25 05:31:54

标签: dependency-injection singleton wildfly javabeans

尝试在Wildfly16上部署应用程序时出现错误

我有一个无状态豆 FileInfoService ,在该豆内部,我正在注入一个单例豆 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$"}}}}

我的代码

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 :(得分:0)

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

删除@Startup注释后,不会引发任何错误,并且在首次调用后将构造Singleton bean