GVKun编程网logo

如何在Spring XML上下文中实现条件资源导入?(spring如何处理xml的编入编出)

3

在本文中,我们将详细介绍如何在SpringXML上下文中实现条件资源导入?的各个方面,并为您提供关于spring如何处理xml的编入编出的相关解答,同时,我们也将为您带来关于java–spring:即

在本文中,我们将详细介绍如何在Spring XML上下文中实现条件资源导入?的各个方面,并为您提供关于spring如何处理xml的编入编出的相关解答,同时,我们也将为您带来关于java – spring:即时添加xml上下文?、java – 在spring上下文中通过classpath引用文件、java – 如何在spring上下文中将null设置为Integer、java – 如何在Spring上下文中插入Maven配置文件属性的有用知识。

本文目录一览:

如何在Spring XML上下文中实现条件资源导入?(spring如何处理xml的编入编出)

如何在Spring XML上下文中实现条件资源导入?(spring如何处理xml的编入编出)

我想实现的功能是“动态”(即基于配置文件中定义的属性)启用/禁用子Spring XML上下文的导入的能力。

我想像这样:

<import condition="some.property.name" resource="some-context.xml"/>

解析属性的位置(为布尔值),如果为true,则导入上下文,否则不导入。

到目前为止,我的一些研究:

  • 编写自定义NamespaceHandler(和相关类),以便我可以在自己的名称空间中注册自己的自定义元素。例如:<myns:import condition="some.property.name" resource="some-context.xml"/>

这种方法的问题在于,我不想复制Spring中的整个资源导入逻辑,而且对我来说,执行此操作所需的委托并不明显。

  • 重写DefaultBeanDefinitionDocumentReader以扩展“ import”元素的解析和解释的行为(在importBeanDefinitionResource方法中发生)。但是我不确定在哪里可以注册此扩展名。

答案1

小编典典

现在,使用Spring 4完全可以做到这一点。

在您的主应用程序内容文件中

<bean/>

MyConditionalConfiguration看起来像

@Configuration@Conditional(MyConditionalConfiguration.Condition.class)@ImportResource("/com/example/context-fragment.xml")public class MyConditionalConfiguration {    static class Condition implements ConfigurationCondition {         @Override         public ConfigurationPhase getConfigurationPhase() {             return ConfigurationPhase.PARSE_CONFIGURATION;         }         @Override         public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {             // only load context-fragment.xml if the system property is defined             return System.getProperty("com.example.context-fragment") != null;         }    }}

最后,您将要包含的bean定义放在/com/example/context-fragment.xml中

参见JavaDoc中的@Conditional

java – spring:即时添加xml上下文?

java – spring:即时添加xml上下文?

我希望能够即时加载spring context.xml文件,以便它们与先前加载的上下文连接(意思是,在contextA.xml中我可以引用已加载的contextB.xml中定义的bean) ).我希望不会销毁现有的bean,然后在添加上下文时创建.

最佳答案
这很容易做到,大多数beanfactory和ApplicationContext实现都具有父上下文的概念.

beanfactory newFactory = new Xmlbeanfactory(xmlResource,yourParentbeanfactory);

// if you what an ApplicationContext

ApplicationContext newContext = new ClasspathXmlApplicationContext( new String[]{"newBeans.xml"},parent);

见http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/xml/XmlBeanFactory.html

在新的上下文中无法在其自身内部解析的引用将父对象传递给它的父对象.

请注意,您可以通过实现ApplicationContextAware来获取当前的应用程序上下文.

java – 在spring上下文中通过classpath引用文件

java – 在spring上下文中通过classpath引用文件

我正在尝试设置org. springframework.beans.factory.config.PropertyPlaceholderConfigurer spring bean,我在src / main / config中有jdbc.properties ..当我把这个文件放在src / main / resources中并把classpath:放在值中时我的应用程序成功部署.

这在jdbc.properties位于src / main / resources中时有效

<property name="location" value="classpath:jdbc.properties" />

但是我需要在src / main / config中放置任何配置,如何以正确的方式将弹簧指向这个位置?

解决方法

这是一个类路径问题,而不是Spring问题.将src / main / config添加到类路径中,它将起作用.在Eclipse中,这意味着将其添加到项目Build Path-> Source.

java – 如何在spring上下文中将null设置为Integer

java – 如何在spring上下文中将null设置为Integer

这只是我背景的一小部分:
<property name="a" value="1"/> where a is Integer.

如何将null设置为此值?

解决方法

您可以使用元素< null />表示空值:
<property name="a" value="1"/><null/></property>

编辑:官方spring 2.5文档中有更多信息:http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-null-element

java – 如何在Spring上下文中插入Maven配置文件属性

java – 如何在Spring上下文中插入Maven配置文件属性

我创建了两个maven配置文件,因为我想将我的应用程序部署到heroku,
所以我有一个配置文件dev,其db属性位于我的PC上,并且具有heroku db的属性. POM.xml如下

我在每个maven模块配置文件中创建了包含文件夹dev和prod的文件夹
并在本教程https://www.petrikainulainen.net/programming/tips-and-tricks/creating-profile-specific-configuration-files-with-maven/中编写了我的道具
最后用这个参数创建春天背景,见下文

sql.Driver" />
    

但是当我制作我的应用程序时,属性不会替换,我会得到类似的东西
无法为连接URL’${url.property}’创建类’org.postgresql.Driver’的JDBC驱动程序

最佳答案
我没有看到您在应用程序上下文中加载具体属性文件的位置.

我认为您在应用程序上下文中需要类似的东西:

sspath:profiles/${build.profile.id}/config.properties" />

控制器模块的过滤应该在其pom文件中,以便在其构建生命周期中应用它.这样,在构建根时应用过滤,而不是在构建控制器模块时应用过滤.

模块应将根pom设置为其父级,以便它们可以继承配置文件和属性.

今天关于如何在Spring XML上下文中实现条件资源导入?spring如何处理xml的编入编出的讲解已经结束,谢谢您的阅读,如果想了解更多关于java – spring:即时添加xml上下文?、java – 在spring上下文中通过classpath引用文件、java – 如何在spring上下文中将null设置为Integer、java – 如何在Spring上下文中插入Maven配置文件属性的相关知识,请在本站搜索。

本文标签: