spring jndi NamingException:名称[spring.liveBeansView.mbeanDomain]未绑定在此Context中

时间:2014-05-20 03:35:21

标签: java spring jndi

我的带有弹簧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注释。这些信息与此相关吗?但我不需要春天注入我的豆类。

2 个答案:

答案 0 :(得分:12)

如果您不使用任何个人资料 mbeans ,只需将以下 context-params 添加到网络即可。 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>  

答案 1 :(得分:7)

这是JIRA issue以及关于它为何在Spring 3.2中首次引入的简短解释。此外,您可以在initial commit中找到有关此功能的详细信息。

基本上,这个功能是通过JMX公开来自某个应用程序的应用程序上下文中存在的bean的实时列表的一种方式。例如,您在Tomcat中部署了一个Web应用程序,并在启动时将其作为一个名为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 Tool Suite的最新版本中(可能在某些早期版本中),有一个功能可以利用这个名为“Live Beans Graph”的活动JMX曝光,它采用JSON表示并创建一个基本的图形那些豆子的代表。

相关问题