将属性作为值注入到applicationContext.xml中(适用于Spring Boot 2.2)

huangapple 未分类评论47阅读模式
标题翻译

Inject property into applicationContext.xml as value (spring boot 2.2)

问题

我正尝试将一个属性注入到我的 Spring Boot 项目的 applicationContext.xml 文件中:

应用程序文件:

@SpringBootApplication
@PropertySource("classpath:navi.properties")    // <== 在此处进行注入
@ImportResource({"classpath*:applicationContext.xml"})   // <== 加载 xml 设置文件
public class CApplication {
    public static void main(String[] args) {
        SpringApplication.run(CApplication.class, args);
    }
}

如您所见,这里没有什么花哨的内容... 在我的 src/main/resources/navi.properties 文件中:

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

在我的 `applicationContext.xml` 中,我正在尝试注入这个值:

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

但是运行 `./mvnw spring-boot:run` 后返回以下错误:

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}

我阅读了许多相关文章(例如 [这篇][1]),听起来不错...
谢谢

  [1]: https://www.baeldung.com/configuration-properties-in-spring-boot
英文翻译

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

Application file :

@SpringBootApplication
@PropertySource(&quot;classpath:navi.properties&quot;)    // &lt;== injection here
@ImportResource({&quot;classpath*:applicationContext.xml&quot;})   // &lt;== 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 :

&lt;bean id=&quot;userInfoService&quot; class=&quot;${USER_INFO_CLS}&quot;/&gt;

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 &#39;userInfoService&#39; 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

huangapple
  • 本文由 发表于 2020年3月16日 10:54:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/60699788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定