如何将Singleton bean注入普 通bean
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将Singleton bean注入普 通bean相关的知识,希望对你有一定的参考价值。
尝试在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++;
}
}
答案
@startup注释是问题,FileInfoService是在FileInfoService之前构造的。即使使用@DependsOn也无济于事。
删除@Startup注释后,不会抛出任何错误,并且在第一次调用后将构造Singleton bean
以上是关于如何将Singleton bean注入普 通bean的主要内容,如果未能解决你的问题,请参考以下文章
Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea(
在 Spring 中将原型列表注入 Singleton bean
Spring文档苦读短生命周期的Bean注入长生命周期的Bean
将较短范围的 Bean 实例注入 CDI 中较大范围的 bean 实例 - 它是如何工作的?