对于bean注入的一般过程,我还很陌生。我在Google上搜索了很多,但是没有找到解决我问题的方法。
运行Wildfly 9.0.1最终版 EJB版本。 :3.1 CDI版本。 :2.2.16(SP1) JSF版本。 :2.2
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.inject.Named;
@Named
@ViewScoped
public class UserEmailSettingsBean extends UserModuleSettingsBean {
private List<String> store;
private List<String> selectedStore;
//getters and setters, some fancy stuff...
@Override
public boolean saveProperties() {
LOG.info("Save called");
LOG.info(selectedStore.toString());
LOG.info(store.toString());
for(String prop : store) {
getProperties().setProperty(prop, String.valueOf(false));
}
for(String selectedProp : selectedStore){
LOG.info("selected: " + selectedProp + ":" + getProperties().getProperty(selectedProp) + " -> true");
getProperties().setProperty(selectedProp, String.valueOf(true));
}
super.saveProperties();
return true;
}
}
二等舱:
public abstract class UserModuleSettingsBean implements ModuleSettings {
private static final long serialVersionUID = 459417872482285085L;
protected abstract List<String> getPropertiesName();
@Inject
private SettingsRepository settingsRepository;
@Inject
private SettingsService settingsService;
private Properties properties = new Properties();
@Override
public boolean saveProperties() {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
settingsService.store(getProperties(), username);
return (true);
}
}
问题是,settingsService是构造的,但是在我的子类中其字段“ settingsRepository”为空。
在从UserEmailSettings调用我的save方法时,使用正确的值调用了getProperties()。setProperty(),但是它从未存储过,因为settingsRepository为null。我认为这是由于某种原因注射错误所致。
让我知道是否需要提供更多信息☺
这是SettingsRepository的必需部分:
@Stateless
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public class SettingsService implements Serializable {
private static final long serialVersionUID = 1695882717866085259L;
@Inject
SettingsRepository settingsRepository;
//...
}
这里是信息SettingsRepository
@Stateless
public class SettingsRepository extends AbstractBaseRepository<Settings, Long> {
/**
* Instantiates a new settings repository.
*/
public SettingsRepository() {
super(Settings.class);
}
}
答案 0 :(得分:0)
想说我的问题是我没有在settingsService上调用init()函数来创建属性,所以getProperties为空