java - 使用 bean 定义配置文件的 Spring 3.1 bean 可见性

标签 java spring maven applicationcontext

我一直在尝试使用 Spring 3.1's bean definition profiles和嵌套的 bean 。我曾希望我可以根据 Activity 配置文件定义不同的 bean。考虑以下大大简化的示例,以便我的 Spring 上下文包含类似

的内容
<bean id="say" class="test.Say" p:hello-ref="hello"/>

<beans profile="prod">
    <bean id="hello" class="test.Hello" p:subject="Production!"/>
</beans>

<beans profile="dev">
    <bean id="hello" class="test.Hello" p:subject="Development!"/>
</beans>

我收到以下错误:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'say' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'hello' while setting bean property 'hello'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'hello' is defined at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360) aJava Result: 1

我期望 hello bean 会根据 Activity 的 Maven 配置文件定义(在我的例子中是 proddev)。我开始认为 Spring Activity 配置文件 (spring.profiles.active) 可能与 Maven 配置文件完全无关。

有人可以解释一下我哪里出错了吗? (这甚至可以使用配置文件吗?)。

最佳答案

I was expecting that the hello bean would be defined according to the active Maven profile (in my case prod or dev). I'm starting to think that the Spring active profiles (spring.profiles.active) may be completely unrelated to Maven profiles.

没错,它们是不相关的。

修复方法如下:

确保 src/main/webapp/WEB-INF/ 文件夹中的 web.xml 具有以下上下文设置:

<context-param>
    <param-name>spring.profile.active</param-name>
    <param-value>${profileName}</param-value>
</context-param>

然后确保 maven-war-pluginweb.xml 启用了过滤:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
    </configuration>
</plugin>

最后在您的个人资料中:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profileName>dev</profileName>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <profileName>prod</profileName>
        </properties>
    </profile>
</profiles>

您还可以在普通属性部分添加一个默认值:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <profileName>dev</profileName>
</properties>

因此,如果您在没有使用 -P 选项的情况下运行,将使用 dev spring 配置文件。

当运行 mvn package 时,web.xml 将具有 spring.profile.active 的正确值。

关于java - 使用 bean 定义配置文件的 Spring 3.1 bean 可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13288691/

相关文章:

java - 重构: Removing flags

java - 软键盘覆盖底部工作表对话框

spring - Mockito 什么时候/然后不工作

java - 为什么Spring HandlerExecutionChain同时有拦截器和interceptorList?

java - 通过moditect maven插件在Java 13模块中添加ResourceBundle服务提供者指令

java - 主框架顶部重叠面板

java - 字符串中子字符串出现的次数 (Java)

spring - 拦截器或过滤器

java - 在 Maven 项目中与依赖项共享单元测试

java - 无法解决maven依赖关系