【问题标题】:Activate spring profile in EJB在 EJB 中激活 spring 配置文件
【发布时间】:2013-07-18 07:07:48
【问题描述】:

我正在为我的 web 应用程序使用 Spring 配置文件,效果很好。

我在 web.xml 文件中激活这些配置文件,如下所示:

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>myProfile</param-value>
</context-param>

但我的 MDB 卡住了。

我使用 SpringBeanAutowiringInterceptor 从我的 MDB 引导 spring,它可以工作,但我不知道如何为这个 MDB 激活我的配置文件。

我尝试添加一个 env-entry,但它似乎不起作用:

<env-entry>
    <env-entry-name>spring.profiles.active</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>myProfile</env-entry-value>
</env-entry>

有人可以帮我吗?

有没有办法通过 beanRefContext.xml(被拦截器使用)激活配置文件?

谢谢

史蒂芬

【问题讨论】:

    标签: spring ejb profile interceptor


    【解决方案1】:

    您可以尝试从 JNDI 设置它,而不是从上下文参数中进行设置。 (默认情况下,Spring 在启动时会在 JNDI 中查找活动配置文件。)它会查找 String 数组(逗号分隔的字符串或数组)并在键 spring.profiles.active 下查找它

    【讨论】:

    • 您好,感谢您的回复!你能简单地告诉我怎么做吗?
    • 设置 JNDI 属性非常依赖于 JNDI 提供者。您为 MDB 使用哪个容器? (JBoss、WAS 等)
    • Weblogic 版本 10.3.3
    • 我个人不熟悉 WebLogic,但您需要了解如何将 Resource 对象添加到 String[] 的 JNDI 提供程序中
    【解决方案2】:

    我找到了 EJB 拦截器的解决方法:

    public class ActivateSpringProfileInterceptor {
    @Resource(name = "spring.profiles.active")
        private String springProfile;
    
        @PostConstruct
        @PostActivate
        public void activateProfile(InvocationContext ctx) throws Exception {
            if (springProfile == null || springProfile.isEmpty()) {
                throw new EJBException("No spring.profiles.active env-entry is found in DD for this EJB !");
            }
    
            System.setProperty("spring.profiles.active", springProfile);
    
            try {
                ctx.proceed();
            }
       }
    

    } 我把它放在我的 ejb-jar.xml 中

    <interceptors>
        <interceptor>
          <interceptor-class>ActivateSpringProfileInterceptor</interceptor-class>
          <env-entry>
            <env-entry-name>spring.profiles.active</env-entry-name>
            <env-entry-type>java.lang.String</env-entry-type>
            <env-entry-value>${spring.profiles.active}</env-entry-value>
          </env-entry>
        </interceptor>
      </interceptors>
    

    它可以工作,但我不想改变系统变量...

    还有其他解决方法吗?

    【讨论】:

    • 这个解决方案的问题是,如果它们共享同一个虚拟机,可能会导致两个应用程序之间发生冲突......还有其他解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 2019-04-27
    • 1970-01-01
    • 2011-11-12
    • 2014-10-29
    • 2011-07-17
    相关资源
    最近更新 更多