我有以下内容:
@Service(DropboxService.NAME)
public class DropboxServiceBean implements DropboxService {
@Inject
private CustomConfig customConfig;
private final String ACCESS_TOKEN = customConfig.getDropboxAppToken();
DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial", "en_US");
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
有谁知道我如何才能首先加载customConfig.getDropboxAppToken();
的值。我一直收到以下错误:
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'myApp_DropboxService' defined in URL
[jar:file:/E:/Cuba/myApp/deploy/tomcat/webapps/app-core/WEB-INF/lib/app-core-0.1-SNAPSHOT.jar!/com/daryn/myApp/service/DropboxServiceBean.class]:
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [com.daryn.myapp.service.DropboxServiceBean]: Constructor
threw exception; nested exception is java.lang.NullPointerException
我正在尝试的当前代码
错误:使用名称' ecosmart_BackupService'创建bean时出错:通过字段< dropboxService'表达的不满意的依赖关系嵌套异常是org.springframework.beans.factory.BeanCreationException:使用名称' ecosmart_DropboxService创建bean时出错':init方法的调用失败;嵌套异常是java.lang.NullPointerException
@Service(DropboxService.NAME)
public class DropboxServiceBean implements DropboxService {
@Inject
private CustomConfig customConfig;
private String ACCESS_TOKEN = "";
DbxRequestConfig config;
DbxClientV2 client;
@PostConstruct
public void postConstruct() {
System.out.println("**************Running post construct");
ACCESS_TOKEN = customConfig.getDropboxAppToken();
config = new DbxRequestConfig("dropbox/java-tutorial", "en_US");
client = new DbxClientV2(config, ACCESS_TOKEN);
}
答案 0 :(得分:1)
Spring仅在构造对象后注入字段,并且在您的情况下public static void MyMethod<T>(this IDriver<T> driver) where T : IDriverConfiguration {
}
在此之前被初始化。
你需要创建一个构造函数并在构造函数中注入你的bean,如下所示:
ACCESS_TOKEN
答案 1 :(得分:-1)
嗯,经过多次捣乱,这是我的绝佳解决方案。我不知道为什么CustomConfig不会首先初始化......
@Service(DropboxService.NAME)
public class DropboxServiceBean implements DropboxService {
@Inject
private CustomConfig customConfig;
private String ACCESS_TOKEN = "";
DbxRequestConfig config =new DbxRequestConfig("dropbox/java-tutorial", "en_US");
DbxClientV2 client;
public static boolean isInitiated = false;
public void generateDbxClient(){
ACCESS_TOKEN = customConfig.getDropboxAppToken();
client = new DbxClientV2(config, ACCESS_TOKEN);
}
@Override
@Transactional
public void uploadFile(FileDescriptorExt file, String path) {
if(isInitiated==false){
System.out.println("generating client");
generateDbxClient();
isInitiated=true;
}