java - WebService类中的Spring Autowired不起作用
问题描述
我正在尝试在此类中使用 Autowired,但变量 config 始终为 null ...在其他类中 Autowired 有效。
这个项目是jhipster生成的,不知道有没有关系
@Component
@WebService(endpointInterface = "SignaturePortTypeV2")
public class Signature extends SpringBeanAutowiringSupport implements SignaturePortTypeV2 {
@Autowired
ConfigServiceBean config;
@Override
public ExecuteTokenCmdRespType executeTokenCmd(ExecuteTokenCmdReqType tokenCmdReq) throws ICPMException {
config.getValue(CommonConfigKey.COMPANY_IDENTIFIER);
return null
}
}
@Service
public class ConfigServiceBean implements ConfigServiceLocal {
@Autowired
private Environment env;
@SuppressWarnings("unchecked")
@Override
public <T> T getValue(ConfigKey configKey) {
switch (configKey.getType()) {
case STRING:
return (T) env.getProperty(configKey.getKey(), String.class, configKey.getDefaultValue());
case INT:
return (T) env.getProperty(configKey.getKey(), Integer.class, configKey.getDefaultValue());
case LONG:
return (T) env.getProperty(configKey.getKey(), Long.class, configKey.getDefaultValue());
case DOUBLE:
return (T) env.getProperty(configKey.getKey(), Double.class, configKey.getDefaultValue());
case BOOLEAN:
return (T) env.getProperty(configKey.getKey(), Boolean.class, configKey.getDefaultValue());
default:
throw new IllegalStateException("Type not expected: " + configKey.getType());
}
}
}
解决方案
我看到一些奇怪的事情:
- 您说配置变量为空,但是,它使用@Autowired 进行了注释。它应该在 Spring Context Load 中失败,因为需要注入(@Autowired 默认具有属性 required = true)。所以第一个问题是:bean Signature 是否正在创建?可能是您的 @Configuration 注释类(或者您创建弹簧上下文)正在查看另一个包。
- 方法 executeTokenCmd 总是返回 null。它只是从环境中检索一个值,然后返回 null。这真的没有意义。这可能是您错误的原因吗?
如果您可以粘贴错误跟踪,这将有所帮助。
推荐阅读
- powershell - 在 Powershell 中使用正则表达式时,Leftpad 不起作用
- php - Composer 不在同名目录中自动加载类
- python - 具有多个参数的函数
- python - 从文件中读取行,然后仅将那些不包含特定短语的行写入文件
- php - 如何在浏览器中播放音频文件 (.wav) 而不是下载?
- reactjs - 在 Expo 上的 React Native 项目中如何更好地管理状态?
- c# - 遵循教程后的 AutoFac.Core.DependencyResolutionException
- ios - Swift Json 响应解码
- javascript - 使用 ReactDOM 后 JavaScript 代码不起作用
- docker - Aspnet.core - Docker 文件创建匿名图像