【问题标题】:Syntax for specifying spring xml files in ClassPathXmlApplicationContext在 ClassPathXmlApplicationContext 中指定 spring xml 文件的语法
【发布时间】:2023-04-11 12:20:01
【问题描述】:

我正在尝试找出在ClassPathXmlApplicationContext 的构造函数中指定spring 的XML 文件的语法。通过语法我不是指方法签名,而是实际的字符串

例如以下三个工作。

ApplicationContext context = new ClassPathXmlApplicationContext("com/anshbansal/alarm/alarm.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alarm.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("alarm.xml");

我也用谷歌搜索过Spring 3.1.0 源代码。但是我在 org.springframework.core.env.AbstractPropertyResolver Spring 类中的 doResolvePlaceholders 方法之后被卡住了。具体来说,我无法理解占位符如何解析路径。

谁能分享指定xml文件的字符串语法是什么?

编辑

我的意思是在构造函数中指定spring xml文件路径的语法。我不是指xml文件本身的语法。

【问题讨论】:

  • 恐怕人家没有回答你的问题,因为问题不清楚。您真正需要的是什么 - 指定 spring xml 文件路径的语法,或者您正在寻找 spring xml 配置文件(其内容)的语法,即 docs.spring.io/spring-framework/docs/current/…
  • @MichalWilkowski 更新问题后忘记@你了。

标签: java xml spring spring-3


【解决方案1】:

好的,我现在明白这个问题了:-)。 PropertyResolver 仅用于将环境值(或属性文件值)放入 spring XML 文件,即:

<context:property-placeholder location="file:///some/path/file.properties"/>

然后在这个spring xml中解决它们,即:

<bean id="mailInviteMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="from" value="${mail.from}"/>
    <property name="subject" value="${mail.subject}"/>
</bean>

最好将 spring XML 放在类路径中并以标准约定(即 beans.xml)命名。但是,您可以将其配置为将参数放入 java 调用,即

java -Dmy.parameter=/path/to/beans.xml ...

然后手动加载:

Context context = new FileSystemXmlApplicationContext(System.getProperty("my.parameter"));

【讨论】:

  • 我觉得还是有一些误解。你能看看我给出的三个例子吗?唯一的区别是作为参数传递给构造函数的字符串。我想找出编写此字符串的不同允许方式。也许它不是特定于 spring 的,因此会引起混乱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 2021-10-23
  • 2016-10-01
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
相关资源
最近更新 更多