0

I am trying to inject a property into my applicationContext.xml file in my spring boot project:

Application file :

@SpringBootApplication
@PropertySource("classpath:navi.properties")    // <== injection here
@ImportResource({"classpath*:applicationContext.xml"})   // <== load the xml setting file
public class CApplication {
    public static void main(String[] args) {
        SpringApplication.run(CApplication.class, args);
    }
}

As you can see, nothing fancy here... In my src/main/resources/navi.properties file :

USER_INFO_CLS=jp.co.xxx.yyy.fw.service.UserInfoService

in my applicationContext.xml, I am trying to inject this value :

<bean id="userInfoService" class="${USER_INFO_CLS}"/>

But ./mvnw spring-boot:run returns the error below :

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [${USER_INFO_CLS}] for bean with name 'userInfoService' defined in class path resource [applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: ${USER_INFO_CLS}

I read many articles about it (like this one) and it sounds good.... Thanks

johann
  • 1,115
  • 8
  • 34
  • 60
  • mmm, is there any particular reason behind mixing annotations and xml configurations? Couldn't it be a class annotated with @Bean? Besides, the spring context doesn't know that `contextApplication.xml` depends on`navi.properties` and also it doesn't add xml files as configuration. Check [this](https://stackoverflow.com/questions/25687754/spring-boot-automatically-import-applicationcontext-xml) and the linked documentation section in that question and maybe [this[(https://www.tutorialspoint.com/spring/spring_applicationcontext_container.htm) and close your question. – itwasntme Mar 16 '20 at 03:45
  • The particular reason should be asked to the original developer :-) I took over this project: it contains sometimes annotations, sometimes XML definitions. I know it sucks but it's the reality and I neither have the time nor the budget to refactor it.. I forgot to write my annotation `@ImportResource({"applicationContext.xml"})` , sorry about that – johann Mar 16 '20 at 05:55
  • 1
    Sry, but I don't have enough energy and time to go through docs and other sources on how the beans should be properly configured when dealing with such complicated situation. But [one thing I found here](https://www.java2novice.com/spring/read-property-file-xml-config/) is that the properties file should be included in XML file that usese it. I'm not sure if it'll work. And If mixing @ and XML works so fare, you really could try just to declare "userInfoService" bean in CApplication (from method) or from aother Configuration class in application context. – itwasntme Mar 18 '20 at 00:01

0 Answers0