我想在 spring 应用程序上下文中动态定义一个 MBeanServerConnection,所以我通过 prepareBeanFactory() 注册它的工厂。我可以看到 bean 存在于上下文中,但是当我执行 getBean() 时,它返回我 null!。
有什么建议么?
public static void main(String[] args) throws IOException, Exception, IntrospectionException, MalformedObjectNameException, ReflectionException {
final AbstractApplicationContext context = new ClassPathXmlApplicationContext() {
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
super.prepareBeanFactory(beanFactory);
final MBeanServerConnectionFactoryBean clientConnection = new MBeanServerConnectionFactoryBean();
try {
clientConnection.setServiceUrl("service:jmx:jmxmp://" + "localhost:7777");
beanFactory.registerSingleton("clientConn", clientConnection);
} catch (MalformedURLException e) {
}
}
};
context.refresh();
for (String name : context.getBeanNamesForType(Object.class)) {
System.out.println(name);
}
MBeanServerConnection mb = context.getBean("clientConn", MBeanServerConnection.class);
for (String s : mb.getDomains()) {
System.out.println(s);
}
}