正在将spring bean(服务层类)注入ResourceBundle

kmb7vmvb  于 2022-12-29  发布在  Spring
关注(0)|答案(2)|浏览(165)
    • bounty将在4天后过期**。回答此问题可获得+50声望奖励。Syed Iftekharuddin希望引起更多人关注此问题。

我使用ResourceBundle接口创建了一个类,如下所示。这个类依赖于另一个类。ResourceBundle的实现类(如下所示的问卷源)总是有null作为依赖项。无论我使用setter还是构造函数注入。有人能帮我解决这个问题吗?我这里缺少一些配置。

@Component
public class QuestionnaireSource extends ResourceBundle {

    private final QuestionnaireCache questionnaireCache;

    private static final Object lockObject = new Object();

    @Override
    protected Object handleGetObject(String key) {
//        Gets an object for the given key from this resource bundle.
//        Returns null if this resource bundle does not contain an object for the given key 0
        Object value = null;
        try {
            value = getString(key, LocaleContextHolder.getLocale());
        } catch (Exception ignored) {
        }
        return value;
    }

    public Questionnaire getString(String key, Locale locale) {
        Locale l = safeLocale(locale);
        return getResources(l).get(key);
    }

    private Locale safeLocale(Locale l) {
        if (l.getLanguage().equalsIgnoreCase("DE")) {
            return Locale.GERMAN;
        } else {
            return Locale.ENGLISH;
        }
    }

    protected Map<String, Questionnaire> getResources(Locale locale) {
        synchronized (lockObject) {
            return questionnaireCache.getQuestionnaireCache().get(locale.getLanguage().toUpperCase());
        }
    }

    @Override
    public Enumeration<String> getKeys() {
        return null;
    }

    public QuestionnaireSource(QuestionnaireCache questionnaireCache) {
        super();
        this.questionnaireCache = questionnaireCache;
    }
}
    • 更新:**我发现即使是resourceBundle中简单的依赖注入也会失败。
    • UPdate2:**我在主类中使用的方法如下:
//        ResourceBundle test here
    System.out.println("Test here for resource bundle");
    Locale locale = new Locale("de", "DE");
    ResourceBundle bundle = ResourceBundle.getBundle("com.app.util.QuestionnaireSource", locale);
    System.out.println(bundle.getString("some.test.string"));
    • 更新3**

我写了一个简单的例子来表达这种情况:

    • 某些服务类别**
@Service
public class SomeServiceTest {

    public String testMethod(){
        return "test here and complete";
    }
}
    • 资源包的一些示例实现**
@Component
public class MyResourceBundle extends ResourceBundle  {

    private final SomeServiceTest someServiceTest;

    @Autowired
    public MyResourceBundle(SomeServiceTest someServiceTest) {
        this.someServiceTest = someServiceTest;
    }

    @Override
    protected Object handleGetObject(String key) {
        if(key.equals("test"))
            return "test";
        return null;
    }

    @Override
    public Enumeration<String> getKeys() {
        return null;
    }
}
    • 主要. java**
main(){
//        ResourceBundle test here
        System.out.println("Test here for resource bundle");
        Locale locale = new Locale("de", "DE");
        ResourceBundle bundle = ResourceBundle.getBundle("com.app.util.MyResourceBundle", locale);
        System.out.println(bundle.getString("test"));

}
@Service
public class SomeServiceTest {

    public String testMethod(){
        return "test here and complete";
    }
}
    • 我的资源包. java**
@Configurable
public class MyResourceBundle extends ResourceBundle  {

    @Autowired
    private  SomeServiceTest someServiceTest;

    public MyResourceBundle() {
    }

    @Override
    protected Object handleGetObject(String key) {
        if(key.equals("test"))
            return someServiceTest.testMethod();
        return null;
    }

    @Override
    public Enumeration<String> getKeys() {
        return null;
    }
}

SomeServiceTest类仍为空。

1tuwyuhd

1tuwyuhd1#

你能举一个例子说明你是如何使用这个类的吗?是你(你的代码)还是spring示例化了它(在启动时)?
@Component只适用于Spring示例化的bean,如果你想在你示例化的类中注入一些东西,你可以用@Configurable注解这个类。
请参阅https://www.baeldung.com/spring-inject-bean-into-unmanaged-objects了解一些示例。

yi0zb3m4

yi0zb3m42#

确保已初始化Spring上下文
如果使用的是spring Boot ,则可以在应用程序启动后获取应用程序上下文,并使用它来获取所需的bean
例如

public static void main(String[] args) {
    ConfigurableApplicationContext run = SpringApplication.run(YouApplication.class, args);
    MyResourceBundle resConfig = run.getBean("myResourceBundle", MyResourceBundle .class);
    resConfig.handleGetObject("test");
}

不幸的是,ResourceBundle.getBundle没有初始化spring应用程序上下文

相关问题