Java Spring注释的mbeans错误--实例化两次
我正在尝试使用Spring注释注册MBean 我的测试类看起来像:Java Spring注释的mbeans错误--实例化两次,java,spring,spring-jmx,Java,Spring,Spring Jmx,我正在尝试使用Spring注释注册MBean 我的测试类看起来像: package examples.mbean; import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.stereotype.Component; @Compone
package examples.mbean;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;
@Component
@ManagedResource(objectName = "examples.mbean:name=MyMbean1")
public class MyMBean {
@ManagedOperation(description = "Returns something.")
public String getSomething() {
return "Something";
}
MyMBean () {
System.out.println("MyMBean constructor");
}
}
dispatcher-servlet.xml如下所示
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="examples.springmvc, examples.mbean" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<context:mbean-export />
</beans>
编辑
根据这些评论,我还发布了web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Web应用程序创建的原型
调度员
org.springframework.web.servlet.DispatcherServlet
1.
调度员
/
上下文配置位置
/WEB-INF/dispatcher-servlet.xml
org.springframework.web.context.ContextLoaderListener
请显示您的web.xml。最常见的情况是,这样的问题是由两次加载上下文或bean引起的。你有ContextLoaderListener吗?如果是,它是否也使用组件扫描?请显示您的web.xml。最常见的情况是,这样的问题是由两次加载上下文或bean引起的。你有ContextLoaderListener吗?如果是这样,它是否也使用组件扫描?多亏了Gary的回答,我才能够解决这个问题
我不得不从web.xml中注释掉以下几行
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
org.springframework.web.context.ContextLoaderListener
事实证明,ContextLoadListener还加载了带注释的bean,我认为这些bean将由dispatcher servlet.xml中的组件扫描获取。多亏了Gary的回答,我才能够解决这个问题 我不得不从web.xml中注释掉以下几行
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
org.springframework.web.context.ContextLoaderListener
事实证明,ContextLoadListener还加载了带注释的bean,我假设dispatcher-servlet.xml中的组件扫描会提取这些bean