【问题标题】:spring jndi NamingException: Name [spring.liveBeansView.mbeanDomain] is not bound in this Contextspring jndi NamingException:名称 [spring.liveBeansView.mbeanDomain] 未绑定在此上下文中
【发布时间】:2014-07-08 04:09:25
【问题描述】:

我的带有 spring 3.2.4 的 webapp 运行良好。但是当我启动它时,我会得到调试信息:

2014-05-20 11:11:47 DEBUG JndiTemplate:150 - Looking up JNDI object with name [java:comp/env/spring.liveBeansView.mbeanDomain]
2014-05-20 11:11:47 DEBUG JndiLocatorDelegate:101 - Converted JNDI name [java:comp/env/spring.liveBeansView.mbeanDomain] not found - trying original name [spring.liveBeansView.mbeanDomain]. javax.naming.NameNotFoundException: Name [spring.liveBeansView.mbeanDomain] is not bound in this Context. Unable to find [spring.liveBeansView.mbeanDomain].
2014-05-20 11:11:47 DEBUG JndiTemplate:150 - Looking up JNDI object with name [spring.liveBeansView.mbeanDomain]
2014-05-20 11:11:47 DEBUG JndiPropertySource:87 - JNDI lookup for name [spring.liveBeansView.mbeanDomain] threw NamingException with message: Name [spring.liveBeansView.mbeanDomain] is not bound in this Context. Unable to find [spring.liveBeansView.mbeanDomain].. Returning null.
2014-05-20 11:11:47 DEBUG PropertySourcesPropertyResolver:81 - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties]
2014-05-20 11:11:47 DEBUG PropertySourcesPropertyResolver:81 - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
2014-05-20 11:11:47 DEBUG PropertySourcesPropertyResolver:103 - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
2014-05-20 11:11:47 DEBUG DispatcherServlet:533 - Published WebApplicationContext of servlet 'spring' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring]

我不知道这些信息是什么意思。我用c3p0作为我的dataSource,配置是:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/config/jdbc.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="autoCommitOnClose" value="true"/>
        <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>
        <property name="initialPoolSize" value="${cpool.minPoolSize}"/>
        <property name="minPoolSize" value="${cpool.minPoolSize}"/>
        <property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
        <property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
        <property name="acquireIncrement" value="${cpool.acquireIncrement}"/>
        <property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <constructor-arg ref="jdbcTemplate" />
    </bean>

我找不到JNDI 的使用位置。我搜索了一些关于此异常的问题。但它们始终与@Profile@Configuration 相关联。我的代码中没有@Profile@Configuration

在我的 bean 类中,没有 @Bean 注释。信息与此有关吗?但我不需要 spring 注入我的 bean 类。

【问题讨论】:

    标签: java spring jndi


    【解决方案1】:

    如果您不使用任何 profilesmbeans,只需将以下 context-params 添加到 web.xml 作为解决方法(技巧),希望有人可以提供比这个丑陋的解决方案更好的解决方案。

    <context-param>  
        <param-name>spring.profiles.active</param-name>  
        <param-value>dev</param-value>  
    </context-param>  
    <context-param>  
        <param-name>spring.profiles.default</param-name>  
        <param-value>dev</param-value>  
    </context-param>
    <context-param>  
        <param-name>spring.liveBeansView.mbeanDomain</param-name>  
        <param-value>dev</param-value>  
    </context-param>  
    

    【讨论】:

    • 如何使用注释来做到这一点?
    • 工作正常。在 web.xml 中添加后,控制台显示 [artifact:mvn] DEBUG: org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [servletConfigInitParams] [artifact:mvn] DEBUG: org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [servletContextInitParams] [artifact:mvn] DEBUG: org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'spring.liveBeansView.mbeanDomain' in [servletContextInitParams] with type [String] and value 'dev'
    • 也谢谢你。这个答案是唯一可以正常工作的。我想我应该谷歌一些关于 liveBeansView 的知识。
    • 如果您使用此解决方法,请注意 param-value 对于您在容器中部署的每个 Web 应用程序都必须是唯一的,否则您最终会得到一个 javax.management.InstanceAlreadyExistsException 和 Web 应用程序将无法部署。
    【解决方案2】:

    这是JIRA issue,并简要解释了为什么它在 Spring 3.2 中首次引入。此外,您可以在initial commit 中找到有关此功能的更多详细信息。

    基本上,此功能是一种通过 JMX 公开来自某个应用程序的应用程序上下文中存在的 bean 的实时列表的方法。例如,您在 Tomcat 中部署了一个 webapp,并在启动它时将其作为一个名为 spring.liveBeansView.mbeanDomain 的环境变量传递给它。假设你没有给它任何值,或者只是一个空字符串。 Spring 搜索一长串可能的位置以查找此类属性,并在系统环境中找到它。如果找到它,它将知道通过 JMX 公开该活动 bean 列表(以 JSON 格式)。

    如果您使用 JConsole 连接到您的 Tomcat 实例,您将看到一个名为 DefaultDomain 的条目,并在其下方显示您的应用程序名称。如果你扩展它,应该有一个名为 SnapshotAsJson 的属性,这是来自你的 webapp 应用程序上下文的 bean 的实时列表。

    如果您要为系统环境变量赋值,比如“test_domain”,那么在 JMX 中,该条目将被称为 test_domain 而不是 DefaultDomain

    所以,基本上您会看到这些 DEBUG 消息,因为 Spring 在很长的位置列表中搜索 spring.liveBeansView.mbeanDomain 属性,JNDI(如果是 JEE 服务器)就是其中之一。

    在最新版本的 SpringSource 工具套件中(可能在一些早期版本中),有一个功能可以利用这种实时 bean JMX 暴露,称为“Live Beans Graph”,它采用 JSON 表示并创建一个基本的图形这些 bean 的表示。

    【讨论】:

      猜你喜欢
      • 2014-04-28
      • 2012-03-10
      • 2011-09-22
      • 1970-01-01
      • 2019-11-20
      • 2012-02-09
      • 2016-12-08
      • 2018-09-12
      • 1970-01-01
      相关资源
      最近更新 更多