4

我正在尝试编写一个示例 JMX 应用程序。这是我的 MBean 类:

package com.pramati.jmx;

@Component
@ManagedResource(objectName="modelMBean:type=simple-calculator",
        description="Calculator performing basic arithmetic on integers")
public class SimpleCalculator {

    private int operand1;
    private int operand2;

    public SimpleCalculator() {
        System.out.println("SimpleCalculator - MBean created!!");
    }

    @ManagedOperation(description="Addition operation")
    public int add() {
        return operand1 + operand2;
    }

    @ManagedOperation(description="Multiplication operation")
    public int multiply() {
        return operand1 * operand2;
    }

    @ManagedOperation(description="Division operation")
    @ManagedOperationParameters({
        @ManagedOperationParameter(name="operand1", description="Dividend"),
        @ManagedOperationParameter(name="operand2", description="Divisor")
    })
    public int divide(int operand1, int operand2) {
        if(operand2 == 0) {
            throw new IllegalArgumentException("Can not divide by zero");
        }
        return operand1 / operand2;
    }

    @ManagedAttribute
    public int getOperand1() {
        return operand1;
    }

    @ManagedAttribute
    public void setOperand1(int operand1) {
        this.operand1 = operand1;
    }

    @ManagedAttribute
    public int getOperand2() {
        return operand2;
    }

    @ManagedAttribute
    public void setOperand2(int operand2) {
        this.operand2 = operand2;
    }


}

这是来自 applicationContext 的 bean 声明:

<context:component-scan  base-package="com.pramati.jmx"/>
<bean id="mbeanExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter"/>

现在,当我运行 jconsole 时,我没有在其中看到我的 Mbean。但是当我如下明确声明 bean 时:

<bean id="calculator" class="com.pramati.jmx.SimpleCalculator">
    <property name="operand1" value="1000"/>
    <property name="operand2" value="50"/>
</bean>
 <bean id="mbeanExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter"/>

现在,如果我运行 jconsole,我看到的是 Mbean。为什么我在使用组件扫描时没有注册 MBean?

这是我完整的应用程序上下文:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="mbeanExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter"/>

<context:component-scan  base-package="com.pramati.model"/>
<context:component-scan  base-package="com.pramati.controller"/>
<context:component-scan  base-package="com.pramati.service"/>
<context:component-scan  base-package="com.pramati.validator"/>
<context:component-scan  base-package="com.pramati.type.converters"/>
<context:component-scan  base-package="com.pramati.jmx"/>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="com.pramati.spring.mvc.CustomWebBindingInitializer"/>
    </property>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

<mvc:interceptors>
    <bean class="com.pramati.spring.mvc.LoggingInterceptor"/>
    <mvc:interceptor>
        <mvc:mapping path="/reservationQuery/**"/>
        <bean class="com.pramati.spring.mvc.AuditTimeInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

<bean name="internalresourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="#{xmlViewResolver.order+1}"/>
</bean>

<bean name="xmlViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="location" value="/WEB-INF/court-views.xml"/>
    <property name="order" value="#{T(org.springframework.core.Ordered).HIGHEST_PRECEDENCE}"/>
</bean>

<bean name="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="fr_FR"></property>
</bean>

<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${connection.driverClassName}"/>
    <property name="url" value="${connection.url}"/>
    <property name="username" value="${connection.username}"/>
    <property name="password" value="${connection.password}"/>
    <property name="initialSize" value="${connection.initialSize}"/>
    <property name="maxActive" value="${connection.maxActive}"/>
</bean>

<context:property-placeholder location="classpath:spring/config.properties"/>

<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename">
        <value>properties/resourceBundle</value>
    </property>
</bean>

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="com.pramati.exception.ResourceNotFoundException">
                exceptions/resourceNotFound
            </prop>
        </props>
    </property>
    <property name="defaultErrorView" value="exceptions/error"/>
</bean>

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
    <property name="converters">
        <set>
            <bean class="com.pramati.type.converters.StringToSportTypeConverter"/>
            <bean class="com.pramati.type.converters.StringToDateConverter"/>
            <bean class="com.pramati.type.converters.StringToPlayerConverter"/>
        </set>
    </property>
</bean>

<bean id="defaultReservation" class="com.pramati.model.Reservation">
    <property name="courtName" value="Soccer Court #1"/>
    <property name="date" value="11-11-2011"/>
    <property name="hour" value="15"/>
    <property name="player" value="Prasanth,9010107771"/>
    <property name="sportType" value="2"></property>
</bean>

</beans>

这是安全上下文:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
    <beans:property name="decisionVoters">
        <beans:list>
            <beans:bean class="org.springframework.security.access.vote.RoleVoter">
                <beans:property name="rolePrefix" value="ROLE_"/>
            </beans:bean>
            <beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
            <beans:bean class="com.pramati.spring.mvc.LocalIpVoter"/>
        </beans:list>
    </beans:property>
</beans:bean>

<http access-decision-manager-ref="accessDecisionManager">
    <intercept-url pattern="/app/messageList*" access="ROLE_USER,ROLE_ANONYMOUS"/>
    <intercept-url pattern="/app/messagePost*" access="ROLE_USER"/>
    <intercept-url pattern="/app/messageDelete*" access="ROLE_ADMIN,IP_LOCAL_HOST"/>

    <form-login login-page="/login.jsp" default-target-url="/app/messagePost" 
        authentication-failure-url="/login.jsp?error=true"/>

    <logout logout-success-url="/login.jsp"/>

    <remember-me services-alias="rememberMeService" key="springRocks" data-source-ref="dataSource"/>
    <!-- <remember-me data-source-ref="dataSource" key="pramati"/> -->

    <session-management session-authentication-error-url="/login.jsp?error=alreadyLoggedin" >
        <concurrency-control max-sessions="1" error-if-maximum-exceeded="true"
            expired-url="/login.jsp?error=alreadyLoggedin"/>
    </session-management>

</http>

<beans:bean id="tokenRepository" class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl">
    <beans:property name="dataSource" ref="dataSource"/>
    <beans:property name="createTableOnStartup" value="false"/>
</beans:bean>

<beans:bean id="rememberMeService" class="org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices">
    <beans:property name="key" value="springRocks"/>
    <beans:property name="userDetailsService" ref="userDetailsService"/>
    <beans:property name="tokenRepository" ref="tokenRepository"/>
    <beans:property name="alwaysRemember" value="true"/>
</beans:bean>

<authentication-manager>
    <authentication-provider>

        <!-- TBD <password-encoder hash="md5"/> -->
        <jdbc-user-service id="userDetailsService" data-source-ref="dataSource" 
            users-by-username-query=
                "SELECT username, password, true as enabled
                 FROM MEMBER
                 WHERE username=?"
            authorities-by-username-query=
                "SELECT member.username, role.role as authorities
                 FROM ROLE role, MEMBER member
                 WHERE role.member_id=member.id and member.username=?"/>
        <!-- <user-service>
            <user name="admin" password="password" authorities="ROLE_ADMIN,ROLE_USER"/>
            <user name="user" password="password" authorities="ROLE_USER"/>
        </user-service> -->
    </authentication-provider>
</authentication-manager>

</beans:beans>
4

2 回答 2

2

据我了解,@Component 注释表明该类在 context:component-scan 运行时有资格成为 Spring bean。@ManagedResource 注释指示 bean 可以作为 JMX MBean 导出。它的objectName 属性是可选的,但可以用来为MBean 指定一个特定的名称。请按如下方式更改您的@ManagedResource

@ManagedResource(objectName="com.pramati.jmx:name=simplecalculator",
        description="Calculator performing basic arithmetic on integers")

我们只是给出了 Mbean 的名称。在这种情况下,它将在名为“simplecalculator”的“com.pramati.jmx”目录下公开。

添加这一行并检查

<context:mbean-export/>

您可以添加日志记录选项以进行调试

@ManagedResource(objectName="com.pramati.jmx:name=simplecalculator", description="Calculator performing basic arithmetic on integers", log=true,
    logFile="jmx.log")
于 2013-10-18T15:43:41.197 回答
2

启用日志,发现问题出在 bean 的声明上。在组件扫描之前定义了 mbeanexporter,因此 mbean 没有被注册。一旦我重新排序它,即首先声明组件扫描,然后定义 mbeanexporter,mbean 将显示在 jconsole 中。

于 2013-10-21T16:27:29.073 回答